Skip to content

fix: reject transactions with the FATAL error the server sent before closing - #1284

Merged
porsager merged 3 commits into
porsager:masterfrom
RagedPanda:fix/fatal-before-close
Nov 1, 2026
Merged

porsager merged 3 commits into
porsager:masterfrom
RagedPanda:fix/fatal-before-close

Conversation

@RagedPanda

Copy link
Copy Markdown
Contributor

Fixes #1283

When the server ends a session with a FATAL ErrorResponse (25P04 transaction_timeout, 25P03 idle_in_transaction_session_timeout, 57P01 pg_terminate_backend), sql.begin rejected with a generic CONNECTION_CLOSED. That made it impossible to tell a configured timeout or an operator termination from a network drop.

Two things caused this:

  • transaction() races the callback against lease.closed. lease.closed rejects synchronously in the pool's onclose, so it always wins against the in-flight query, which had already been rejected with the FATAL error.
  • closed.cause only picked up connection.fatal, which is set only when the FATAL arrives with no query in flight. A FATAL that arrived for an in-flight query was not attached anywhere else.

Changes:

  • src/connection.js: when the in-flight query received a FATAL, it now also becomes the cause of the CONNECTION_CLOSED error. A FATAL received while idle was already the cause.
  • src/pool.js: a lease (sql.begin) whose connection closed because of a server FATAL now rejects with that FATAL error instead of CONNECTION_CLOSED.

Queries made on the dead connection afterwards still reject with CONNECTION_CLOSED, now with the FATAL as cause in every case. Plain queries that were in flight already rejected with the FATAL error, so they are unchanged.

Behaviour change: sql.begin now rejects with the server's SQLSTATE (25P03, 57P01, ...) where it used to reject with CONNECTION_CLOSED. Two existing tests that asserted CONNECTION_CLOSED for exactly this case are updated: idle_in_transaction_session_timeout rejects the transaction and Disconnect rejects queued transaction queries....

Tests added in tests/transactions.js:

  • transaction_timeout (25P04) during an in-flight query (PostgreSQL 17+, skipped on older servers)
  • pg_terminate_backend (57P01) during an in-flight query in a transaction
  • pg_terminate_backend between statements: sql.begin rejects with 57P01, and the next query rejects with CONNECTION_CLOSED that has cause.code === '57P01'

All three new tests fail on master and pass with this change.

This PR was prepared with AI assistance (Claude). I reviewed and tested the change myself.

porsager added a commit that referenced this pull request Nov 1, 2026
With #1284 a transaction whose connection the server ends rejects with
the server's FATAL error, while #1282 made a savepoint or nested begin
left running reject with the connection's CONNECTION_CLOSED. Both now
reject with the error the transaction's connection closed with.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CDChydjdmWh3wYY7rqYz5D
@porsager
porsager merged commit bfa7e77 into porsager:master Nov 1, 2026
0 of 36 checks passed
@porsager

porsager commented Nov 1, 2026

Copy link
Copy Markdown
Owner

Thanks a lot!

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.

4.0.0: a FATAL the server sends before closing (25P04 transaction_timeout, 25P03, 57P01) is reported as CONNECTION_CLOSED

2 participants