Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool.end() resolves before the last pool.query() #3254

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/pg-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,22 @@ class Pool extends EventEmitter {
throw new Error('unexpected condition')
}

_remove(client) {
_remove(client, callback) {
const removed = removeWhere(this._idle, (item) => item.client === client)

if (removed !== undefined) {
clearTimeout(removed.timeoutId)
}

this._clients = this._clients.filter((c) => c !== client)
client.end()
this.emit('remove', client)
const context = this
client.end(() => {
context.emit('remove', client)

if (typeof callback === 'function') {
callback()
}
})
}

connect(cb) {
Expand Down Expand Up @@ -342,26 +348,23 @@ class Pool extends EventEmitter {
if (client._poolUseCount >= this.options.maxUses) {
this.log('remove expended client')
}
this._remove(client)
this._pulseQueue()
return

return this._remove(client, this._pulseQueue.bind(this))
}

const isExpired = this._expired.has(client)
if (isExpired) {
this.log('remove expired client')
this._expired.delete(client)
this._remove(client)
this._pulseQueue()
return
return this._remove(client, this._pulseQueue.bind(this))
}

// idle timeout
let tid
if (this.options.idleTimeoutMillis) {
tid = setTimeout(() => {
this.log('remove idle client')
this._remove(client)
this._remove(client, this._pulseQueue.bind(this))
}, this.options.idleTimeoutMillis)

if (this.options.allowExitOnIdle) {
Expand Down
10 changes: 10 additions & 0 deletions packages/pg-pool/test/ending.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ describe('pool ending', () => {
expect(res.rows[0].name).to.equal('brianc')
})
)

it('pool.end() - finish pending queries', async () => {
const pool = new Pool({ max: 20 })
let completed = 0
for (let x = 1; x <= 20; x++) {
pool.query('SELECT $1::text as name', ['brianc']).then(() => completed++)
}
await pool.end()
expect(completed).to.equal(20)
})
})
10 changes: 8 additions & 2 deletions packages/pg-pool/test/idle-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('idle timeout', () => {
it(
'times out and removes clients when others are also removed',
co.wrap(function* () {
let currentClient = 1
const pool = new Pool({ idleTimeoutMillis: 10 })
const clientA = yield pool.connect()
const clientB = yield pool.connect()
Expand All @@ -35,7 +36,12 @@ describe('idle timeout', () => {
pool.on('remove', () => {
expect(pool.idleCount).to.equal(0)
expect(pool.totalCount).to.equal(0)
resolve()

if (currentClient >= 2) {
resolve()
} else {
currentClient++
}
})
})

Expand All @@ -44,7 +50,7 @@ describe('idle timeout', () => {
try {
yield Promise.race([removal, timeout])
} finally {
pool.end()
yield pool.end()
}
})
)
Expand Down
Loading