Skip to content

fix(pg): mark client not queryable when a FATAL ends the session - #3836

Open
princejain756 wants to merge 1 commit into
brianc:masterfrom
princejain756:fix-fatal-session-not-queryable
Open

princejain756 wants to merge 1 commit into
brianc:masterfrom
princejain756:fix-fatal-session-not-queryable

Conversation

@princejain756

Copy link
Copy Markdown

Fixes #3826

When the server ends a session mid-query with a FATAL ErrorResponse (57P01 from pg_terminate_backend, a restart, or a failover), the error goes to the active query and the client stays _queryable. A pool user that handles the query error and calls release() returns the client to the idle list, so the next pool.connect() hands out a client whose session no longer exists. The reporter's repro shows the same client again: true and the next query failing with "Connection terminated unexpectedly".

The client now marks itself not queryable before reporting the error. The flag is set before handleError because the repro's release() runs inside the query error callback, and pg-pool's _release already destroys clients with _queryable === false.

Validation: new unit tests cover both directions: _queryable === false after a FATAL during an active query, and unchanged for a regular ERROR. The FATAL test fails against the old code. make test-unit passes 290 tests and eslint is clean.

An ErrorResponse with severity FATAL (pg_terminate_backend, restart, failover) terminates the session, but the client stayed queryable. A pool user that handles the query error and calls release() returned the dead client to the idle list, and the next connect() handed it out again.

Mark the client not queryable before reporting the error so release() inside the error callback sees the dead client and the pool destroys it instead of reusing it.

Fixes brianc#3826
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.

pg-pool hands a client out again after the server ended its session with a FATAL during a query

1 participant