Skip to content

Commit

Permalink
Immediately unref() maxLifetimeSeconds Timeout object to prevent bloc…
Browse files Browse the repository at this point in the history
…king allowExitOnIdle (#2721)
  • Loading branch information
ChrisWritable committed May 13, 2022
1 parent c774364 commit 3ca5602
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/pg-pool/index.js
Expand Up @@ -252,7 +252,7 @@ class Pool extends EventEmitter {
this.log('new client connected')

if (this.options.maxLifetimeSeconds !== 0) {
setTimeout(() => {
const maxLifetimeTimeout = setTimeout(() => {
this.log('ending client due to expired lifetime')
this._expired.add(client)
const idleIndex = this._idle.findIndex((idleItem) => idleItem.client === client)
Expand All @@ -265,6 +265,9 @@ class Pool extends EventEmitter {
)
}
}, this.options.maxLifetimeSeconds * 1000)

maxLifetimeTimeout.unref()
client.once('end', () => clearTimeout(maxLifetimeTimeout))
}

return this._acquireClient(client, pendingItem, idleListener, true)
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-pool/test/idle-timeout-exit.js
Expand Up @@ -3,7 +3,7 @@ if (module === require.main) {
const allowExitOnIdle = process.env.ALLOW_EXIT_ON_IDLE === '1'
const Pool = require('../index')

const pool = new Pool({ idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
const pool = new Pool({ maxLifetimeSeconds: 2, idleTimeoutMillis: 200, ...(allowExitOnIdle ? { allowExitOnIdle: true } : {}) })
pool.query('SELECT NOW()', (err, res) => console.log('completed first'))
pool.on('remove', () => {
console.log('removed')
Expand Down

0 comments on commit 3ca5602

Please sign in to comment.