Repository navigation
fix(pg-cache): keep the server alive when a checked-out connection drops - #2085
Conversation
pg-pool listens for errors only on idle clients; while a client is checked out, the caller is expected to listen. PostGraphile does that for pools it creates and leaves it to callers that pass their own pool, which is how the GraphQL server uses pg-cache's pools. When PostgreSQL ended a checked-out connection (a restart, a failover, pg_terminate_backend), pg emitted 'error' on the client with no listener, and the whole process exited. defaultPgPoolFactory now gives every client a permanent error listener when the pool connects it. The owner still sees the rejected query, and the pool discards the client when it is released. Idle-client errors still reach the pool handler, so the new listener logs at debug level. Adds a pg-cache unit test, and a graphql-server-test integration test that terminates the backend of an in-flight request. Both fail without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Review complete. No blocking issues — approved ✅; 1 nitpick below. 🧹 Nitpicks (1) — 🟢 1 low
This PR hardens
Reviewed commit: 5ae8789 |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
This comment has been minimized.
This comment has been minimized.
|
Responses to the automated reviews, plus the one CI failure:
|
…2101) * fix(pgsql-test): keep a lost connection from ending the test process node-postgres emits 'error' on a client whose connection the server ends or drops (pg_terminate_backend, a restart, a crash). PgClient and the pools that PgTestConnector.getPool creates had no listener for it, so the event ended the whole test process instead of failing the test that used the connection. PgClient now listens on its underlying client. getPool's pools listen on the pool, for idle connections, and on each client, for checked-out connections. The listeners log and keep the process alive. Queries on the broken connection still reject, and the pool discards it on release. pg-cache's pools got the same listeners in #2085. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(pgsql-test): isolate the connection-loss cases and always release pooled clients Run each case between the pg and db clients' beforeEach/afterEach hooks, like the other pgsql-test suites. Release the pooled connections in finally, so a failed check can't leave one checked out when the suite ends its pools. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Zetazzz <1872387+Zetazzz@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Problem
pg-pool listens for errors only on idle clients. While a client is checked out, pg-pool removes its own listener and expects the caller to add one. PostGraphile's
makePgServiceadds that listener to the pools it creates, and its source notes that a caller passing its own pool is responsible for doing the same ("If you pass your own pool, you're responsible for doing this yourself"). The GraphQL server passes pools from pg-cache, and pg-cache only handles the pool-levelerrorevent, which fires for idle clients.PostgreSQL can end a checked-out connection at any time: a restart, a failover, a crashed backend,
pg_terminate_backend. When it does, pg rejects the query in flight and also emitserroron the client. Nothing listens, so Node treats the event as fatal and the process exits. Every other in-flight request is dropped, and every cached handler is lost.Change
defaultPgPoolFactorygives every client a permanenterrorlistener when the pool connects it (pool.on('connect', …)). PostGraphile does the same for its own pools.graphile:formatError).getPgPoolwith the default factory, which includes the GraphQL server, the explorer and the dev server. Factories registered withregisterPgPoolFactoryare unchanged.Tests
errorlistener, and anerrorevent on it does not throw. It fails without the change.connection-loss.integration.test.tsterminates the backend serving an in-flight request through the real server. That request fails and the next one succeeds. Without the change, the suite fails withUnhandled error. (Error: Connection terminated unexpectedly …). In a running server, that unhandled event exits the process.Evidence
The real server was run outside this repository, from main as of 2026-11-23 (
postgres/pg-cacheis unchanged since). It ran in production mode against a local PostgreSQL 18, and was tested in three server states. For each state, the connection was dropped three ways, twice each:pg_terminate_backendon the server's sessions;SIGKILLof the postmaster.On main, the unhandled errors came from clients whose last statement was a request's
rollback, the query itself, or the scoped introspection query. With the change, in-flight requests failed with an error, and all nine follow-up requests in each run succeeded without restarting the server.Not changed
The observability-only diagnostics pool in
graphql/server/src/diagnostics/debug-db-snapshot.tscreates its ownpg.Poolwithout these handlers. It is only enabled in development on a loopback host, so it is left for a separate change.Verification
notify-listener,subscriber-pool-lifecycle,server,request-protectionandhandler-heappass 58/58 locally against PostgreSQL 18.7.🤖 Generated with Claude Code
Summary by CodeRabbit