From ad0f9e7bb3b5d8427172a858d1438ef616aded21 Mon Sep 17 00:00:00 2001 From: Toufiqul Islam Date: Wed, 18 Nov 2026 15:13:36 +0600 Subject: [PATCH] fix(pg-pool): wait for clients to close during shutdown --- packages/pg-pool/index.js | 17 ++++++++++++---- packages/pg-pool/test/ending.js | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index ab514fa88..301c312cb 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -103,6 +103,7 @@ class Pool extends EventEmitter { this._idle = [] this._expired = new WeakSet() this._pendingQueue = [] + this._closingClients = 0 this._endCallback = undefined this.ending = false this.ended = false @@ -137,10 +138,7 @@ class Pool extends EventEmitter { this._remove(item.client) }) } - if (!this._clients.length) { - this.ended = true - this._endCallback() - } + this._finishEnding() return } @@ -169,6 +167,13 @@ class Pool extends EventEmitter { throw new Error('unexpected condition') } + _finishEnding() { + if (this.ending && !this.ended && !this._clients.length && !this._closingClients) { + this.ended = true + this._endCallback() + } + } + _remove(client, callback) { const removed = removeWhere(this._idle, (item) => item.client === client) @@ -177,12 +182,16 @@ class Pool extends EventEmitter { } this._clients = this._clients.filter((c) => c !== client) + this._closingClients++ const context = this client.end(() => { context.emit('remove', client) + context._closingClients-- if (typeof callback === 'function') { callback() + } else { + context._finishEnding() } }) } diff --git a/packages/pg-pool/test/ending.js b/packages/pg-pool/test/ending.js index d3aa81f05..da2fe72ec 100644 --- a/packages/pg-pool/test/ending.js +++ b/packages/pg-pool/test/ending.js @@ -1,6 +1,7 @@ 'use strict' const co = require('co') const expect = require('expect.js') +const { EventEmitter } = require('events') const describe = require('mocha').describe const it = require('mocha').it @@ -17,6 +18,41 @@ describe('pool ending', () => { return new Pool().end() }) + for (const idle of [true, false]) { + it(`waits for both ${idle ? 'idle' : 'checked-out'} clients to close`, async () => { + class Client extends EventEmitter { + constructor() { + super() + this._queryable = true + } + + connect(cb) { + process.nextTick(cb) + } + + end(cb) { + this.finishClosing = cb + } + } + + const pool = new Pool({ Client, max: 2, idleTimeoutMillis: 0 }) + const clients = await Promise.all([pool.connect(), pool.connect()]) + const events = [] + pool.on('remove', (client) => events.push(client)) + + if (idle) clients.forEach((client) => client.release()) + const ending = pool.end().then(() => events.push('ended')) + if (!idle) clients.forEach((client) => client.release()) + + clients[0].finishClosing() + await new Promise(setImmediate) + clients[1].finishClosing() + await ending + + expect(events).to.eql([...clients, 'ended']) + }) + } + it( 'ends with clients', co.wrap(function* () {