Skip to content

fix(pg-pool): finish requests waiting for a client when the pool ends - #3854

Open
efeesrefinal wants to merge 1 commit into
brianc:masterfrom
efeesrefinal:fix/pool-end-pending
Open

efeesrefinal wants to merge 1 commit into
brianc:masterfrom
efeesrefinal:fix/pool-end-pending

Conversation

@efeesrefinal

Copy link
Copy Markdown

A pool.query() or pool.connect() waiting in the queue never settles if pool.end() is called, and end() still resolves. Once the pool is ending a released client is removed and _pulseQueue returns early, so the queue is never read. This was reported in #3819, which its author closed without a fix.

const pool = new Pool({ max: 1 })
const client = await pool.connect()
const queued = pool.query('SELECT 1') // never settles
const ended = pool.end()
client.release()

Requests queued before end() now run, in line with the existing test pool.end() - finish pending queries, and the clients are removed once the queue is empty. New checkouts during the drain are still rejected. #3750, a draft, does this too as part of reusing a pool. This is only the fix for the hang, if that helps.

Added a test to ending.js that times out without the change. On PostgreSQL 16 and Node 22 the pg-pool tests give 88 passed and 1 failed. The failure is a test that needs pg-native, which I didn't build, and it fails the same way without the change. I did not run the pg package tests.

@Cdineshreddy12

Copy link
Copy Markdown

Ran this branch against the #3819 repro and some variations, since I'd independently hit the same hang. Findings:

The fix works for the reported case. On master the queued waiter never settles and end() resolves anyway; on this branch the waiter is served and then the pool ends. Draining rather than rejecting also matches the existing "pool.end() - finish pending queries" test's semantics, so the direction seems right to me.

One consequence worth a maintainer decision: the drain path can open a brand-new connection after end() was called. With the ending branch now skipped while waiters exist, _pulseQueue falls through to newClient(pendingItem) when there's no idle client and the pool isn't full. That's reachable whenever the held client can't be reused:

const pool = new Pool({ max: 1 })
const held = await pool.connect()
const waiter = pool.connect() // queued
const ended = pool.end()
held.release(new Error('broken')) // destroyed, not reused
await waiter // served — by a connection created after end()

Instrumenting pool.on('connect') shows 2 connections created, the second after end(). It does terminate correctly in every variation I tried (if the post-end connect fails — say the DB is already gone, which is common in shutdown ordering — the waiter gets the connect error, the queue empties, and end() resolves), so there's no new hang. But "pool.end() may dial new connections" is a behavior change someone should explicitly sign off on. The alternative would be to reject a waiter that can't be served without a new connection, with the same error connect() gives after ending.

Either way, the error-release path above isn't covered by the test in this PR (which reuses one healthy client for all three queries) — happy to contribute it as a test case if useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants