From db4fdca65433f8787f69d341ba74ccf9d6163a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20=C4=B0nal?= <87021466+efeesrefinal@users.noreply.github.com> Date: Sat, 7 Nov 2026 05:46:54 +0300 Subject: [PATCH] fix(pg-pool): finish requests waiting for a client when the pool ends --- packages/pg-pool/index.js | 5 +++-- packages/pg-pool/test/ending.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index ab514fa88..d5a5959ce 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -130,7 +130,7 @@ class Pool extends EventEmitter { this.log('pulse queue ended') return } - if (this.ending) { + if (this.ending && !this._pendingQueue.length) { this.log('pulse queue on ending') if (this._idle.length) { this._idle.slice().map((item) => { @@ -389,7 +389,8 @@ class Pool extends EventEmitter { this.emit('release', err, client) // TODO(bmc): expose a proper, public interface _queryable and _ending - if (err || this.ending || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) { + const draining = this.ending && !this._pendingQueue.length + if (err || draining || !client._queryable || client._ending || client._poolUseCount >= this.options.maxUses) { if (client._poolUseCount >= this.options.maxUses) { this.log('remove expended client') } diff --git a/packages/pg-pool/test/ending.js b/packages/pg-pool/test/ending.js index d3aa81f05..93729754d 100644 --- a/packages/pg-pool/test/ending.js +++ b/packages/pg-pool/test/ending.js @@ -38,6 +38,21 @@ describe('pool ending', () => { }) ) + it('finishes queries waiting for a client when the pool ends', async () => { + const pool = new Pool({ max: 1 }) + const client = await pool.connect() + const queries = [1, 2, 3].map((n) => pool.query('SELECT $1::int AS n', [n])) + const ended = pool.end() + client.release() + + const results = await Promise.all(queries) + await ended + + expect(results.map((x) => x.rows[0].n)).to.eql([1, 2, 3]) + expect(pool.totalCount).to.equal(0) + expect(pool.ended).to.equal(true) + }) + it('pool.end() - finish pending queries', async () => { const pool = new Pool({ max: 20 }) let completed = 0