Skip to content

Keep the server error when the connection closes mid-operation - #1369

Merged
elprans merged 1 commit into
MagicStack:masterfrom
RitiGrover:fix/surface-server-error-on-connection-lost
Sep 23, 2026
Merged

elprans merged 1 commit into
MagicStack:masterfrom
RitiGrover:fix/surface-server-error-on-connection-lost

Conversation

@RitiGrover

Copy link
Copy Markdown
Contributor

When the server sends an ErrorResponse and then closes the connection without a ReadyForQuery, the error was dropped. The waiter only got a generic ConnectionDoesNotExistError. This happens with pgbouncer's query_wait_timeout and with any FATAL such as pg_terminate_backend().

_handle_waiter_on_connection_lost now checks for a pending failed result. The exception type stays ConnectionDoesNotExistError so existing handlers keep working. The server message is appended and the PostgresError is set as __cause__:

asyncpg.exceptions.ProtocolViolationError: query_wait_timeout

The above exception was the direct cause of the following exception:

asyncpg.exceptions.ConnectionDoesNotExistError: connection was closed in the middle of operation: query_wait_timeout

Tested with a new test that terminates a backend mid-query. It fails on master and passes with the fix. I also checked the pgbouncer case against a small fake server that sends FATAL 08P01 query_wait_timeout and closes. The full suite passes on PostgreSQL 15 apart from the GSSAPI tests, which need Kerberos binaries I didn't have locally.

Fixes #1299

@elprans elprans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@elprans
elprans merged commit 6c8f1a7 into MagicStack:master Sep 23, 2026
53 checks passed
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.

Query Wait Timout Error from Pgbouncer Not Propogating to Client

2 participants