Skip to content

Bump asyncpg to >=0.32.0: a server FATAL mid-transaction leaks a pool holder (#2540 part 1) - #2542

Merged
eumemic merged 1 commit into
masterfrom
seat/issue-2540-asyncpg-032
Oct 9, 2026
Merged

eumemic merged 1 commit into
masterfrom
seat/issue-2540-asyncpg-032

Conversation

@eumemic

@eumemic eumemic commented Oct 9, 2026

Copy link
Copy Markdown
Owner

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_connection frame walk) are NOT in this PR.

Mechanism (verified in the asyncpg 0.31.0 and 0.32.0 sdists)

  • An idle-in-transaction FATAL arrives while the protocol is idle, so _read_server_messages hits unexpected state and sets PROTOCOL_ERROR_CONSUME (2).
  • The next statement fails _set_state(15) with cannot switch to state 15; another operation (2) is in progress, which is the prod fingerprint. That calls _coreproto_error() → abort(), which sets closing=True.
  • _on_connection_lost then takes the closing branch and skips con._cleanup(). In 0.31.0, PoolConnectionHolder.release() returns early on is_closed(), so the holder stays _in_use (in_use 1, idle == size).
  • In 0.32.0 (Fix connection pool deadlock MagicStack/asyncpg#1324, 4a83cc2f), release() calls self._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.py builds the pool with aios create_pool, runs SET LOCAL idle_in_transaction_session_timeout='200ms' in a txn, sleeps 0.6s, then runs SELECT 1. It asserts in_use==0 and that max_size acquires succeed within 5s.

  • asyncpg 0.31.0: FAILED holder leaked: in_use=1 size=1 idle=1, and the follow-up acquire times out.
  • asyncpg 0.32.0: passed (5 of 5 runs).

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_lost runs 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)

  • Pool min_size semantics: it is now a maintained floor, refilled in the background, and new init_size defaults to min_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_lifetime no longer closes connections at or below min_size.
  • SSL: direct TLS now requires sslmode=require/verify-*, and asyncpg sets ALPN postgresql on its own contexts. aios passes no ssl args or sslmode in code, so whether this matters depends on the prod DSN (not checked).
  • Codecs: only new OIDs (oid8, regdatabase). No change to set_type_codec or the jsonb path.
  • Python: requires_python >=3.9 is unchanged, and a cp313 manylinux wheel is in the lock.
  • server_settings / init: no change to connect_utils handling.

uv.lock was regenerated with uv 0.11.0 so the lock stays at revision = 3 and 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.

…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.
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.

1 participant