Skip to content

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

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

Open
wants to merge 7 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 @@ -161,16 +161,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 @@ -351,26 +357,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 && this._isAboveMin()) {
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)
})
})
14 changes: 11 additions & 3 deletions packages/pg-pool/test/idle-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ describe('idle timeout', () => {
const pool = new Pool({ idleTimeoutMillis: 10 })
const clientA = yield pool.connect()
const clientB = yield pool.connect()
clientA.release()
clientB.release(new Error())
clientA.release() // this will put clientA in the idle pool
clientB.release(new Error()) // an error will cause clientB to be removed immediately

const removal = new Promise((resolve) => {
pool.on('remove', () => {
pool.on('remove', (client) => {
// clientB's stream may take a while to close, so we may get a remove
// event for it
// we only want to handle the remove event for clientA when it times out
// due to being idle
if (client !== clientA) {
return
}

expect(pool.idleCount).to.equal(0)
expect(pool.totalCount).to.equal(0)
resolve()
Expand Down