Repository navigation
Bump asyncpg to >=0.32.0: a server FATAL mid-transaction leaks a pool holder (#2540 part 1) - #2542
Merged
Merged
Conversation
…holder (#2540 part 1) asyncpg 0.31.0: an idle-in-transaction FATAL leaves the protocol in ERROR_CONSUME. The next statement fails _set_state ("cannot switch to state 15; another operation (2) is in progress", the prod fingerprint), then _coreproto_error() -> abort() sets closing=True. connection_lost then skips Connection._cleanup(), and PoolConnectionHolder.release() returns early on is_closed(), so the holder stays _in_use forever (in_use 1, idle_size == size). 0.32.0 (MagicStack/asyncpg#1324, 4a83cc2f) terminates the closed connection in release(), which re-queues the holder. New integration test: RED on 0.31.0 ("holder leaked: in_use=1 size=1 idle=1"), GREEN on 0.32.0. It routes the pool through a byte relay that delays propagating the server's close. On loopback the FIN arrives together with the FATAL, connection_lost runs first and nothing leaks, so a direct connection can't reproduce the bug. uv.lock regenerated with uv 0.11 (lock revision 3 kept; asyncpg-only diff). Scope: part 1 of #2540 only. The wake_workflow idle-in-txn holder (part 2) and the watchdog frame walk (part 3) are NOT addressed here.
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part 1 of #2540 only. Do not merge without review. Parts 2 (the wake_workflow idle-in-txn holder) and 3 (the watchdog
_task_for_connectionframe walk) are NOT in this PR.Mechanism (verified in the asyncpg 0.31.0 and 0.32.0 sdists)
_read_server_messageshitsunexpected stateand setsPROTOCOL_ERROR_CONSUME(2)._set_state(15)withcannot switch to state 15; another operation (2) is in progress, which is the prod fingerprint. That calls_coreproto_error()→abort(), which setsclosing=True._on_connection_lostthen takes the closing branch and skipscon._cleanup(). In 0.31.0,PoolConnectionHolder.release()returns early onis_closed(), so the holder stays_in_use(in_use 1, idle == size).release()callsself._con.terminate()on a closed connection. That runs_cleanup()→_release_on_close()and re-queues the holder.Red/green
tests/integration/test_pool_server_fatal_releases_holder.pybuilds the pool with aioscreate_pool, runsSET LOCAL idle_in_transaction_session_timeout='200ms'in a txn, sleeps 0.6s, then runsSELECT 1. It asserts in_use==0 and thatmax_sizeacquires succeed within 5s.holder leaked: in_use=1 size=1 idle=1, and the follow-up acquire times out.The test routes through a local TCP relay that delays forwarding the server's close by 1s. Over loopback the FIN arrives in the same read as the FATAL, so
connection_lostruns first, cleanup happens, and nothing leaks (verified: a direct connection does not reproduce the leak on 0.31.0). In prod the next query is sent before the FIN is seen.Breaking-change review (0.32.0 changelog)
min_sizesemantics: it is now a maintained floor, refilled in the background, and newinit_sizedefaults tomin_size. aios uses min_size=1 by default, so the pool now reconnects one idle connection in the background after it dies instead of draining to 0. That is harmless.max_inactive_connection_lifetimeno longer closes connections at or below min_size.postgresqlon its own contexts. aios passes no ssl args or sslmode in code, so whether this matters depends on the prod DSN (not checked).server_settings/init: no change to connect_utils handling.uv.lock was regenerated with uv 0.11.0 so the lock stays at
revision = 3and the diff touches asyncpg only. A newer uv bumps it to revision 5, and the Dockerfile's uv 0.5.18 rewrites the whole file.