Connection.close(timeout=) waits forever on a pending cancel when the server never acknowledges it
还没有人认领这个 Issue。
评估
调研方向
从 asyncpg/protocol/protocol.pyx 开始,阅读 close(self, timeout)、_request_cancel()、_handle_waiter_on_connection_lost() 和 _on_connection_lost()。复现 issue 中描述的服务器冻结场景,然后验证 close(timeout=2) 会返回,并且 transport 丢失不会让 cancel_waiter 保持 pending 状态,同时连接会按预期回退到 abort。
由索引模型根据 Issue 内容生成。
描述
Summary
When a statement times out (command_timeout) while the server, or a pooler in front of it, is frozen, asyncpg requests a cancel and then every later operation on that connection, including close(timeout=...), awaits the cancel acknowledgement with no bound. The timeout argument of close() does not cover that wait, and a subsequent transport loss does not resolve it either, so the connection can never be closed gracefully and any caller that awaits close() hangs indefinitely.
Versions
- asyncpg 0.30.0 and 0.31.0 (same code shape in both)
- Python 3.12.3, Linux
- Observed through SQLAlchemy 2.0.52's asyncpg dialect, which calls
Connection.close(timeout=2)when invalidating a connection after aTimeoutError, but the behaviour is asyncpg's.
Where in the source (0.31.0)
asyncpg/protocol/protocol.pyx,close(self, timeout): awaitsself.cancel_sent_waiterand thenif self.cancel_waiter is not None: await self.cancel_waiterbefore the part that is guarded bytimeout._request_cancel()(called from_on_timeout()) createscancel_waiter; it is resolved only by a ReadyForQuery arriving on the original socket._handle_waiter_on_connection_lost()and_on_connection_lost()resolveself.waiteronly;cancel_waiteris left pending when the transport is lost.abort()returns early whenself.closingis already set, so cancelling a stuckclose()from outside and then callingConnection._abort()does not close the transport.
Reproduction
- Run PostgreSQL behind pgbouncer (transaction pooling), or plain PostgreSQL.
- Open a connection with
command_timeout=5, runSELECT pg_sleep(40). - While it runs, freeze the server process (
podman pause/kill -STOPon postgres, or on pgbouncer). - The statement raises
asyncio.TimeoutErrorafter 5 s and asyncpg starts a cancel task. - Now
await conn.close(timeout=2): it never returns while the freeze lasts. If the frozen side is later closed by a pooler timeout (pgbouncerquery_timeoutcloses the client socket),close()still never returns because the transport loss resolves only the query waiter.
Observed with an asyncio task-stack watchdog: the caller sits in Connection.close → protocol.close → await self.cancel_waiter, and the _cancel task sits in connect_utils awaiting the cancel connection's on_disconnect, for as long as the server stays frozen (minutes; unbounded).
Expected
close(timeout=t)should bound the wait for the cancel acknowledgement byt(or by the connection'scommand_timeout) and fall back to aborting the transport._on_connection_lost()should resolvecancel_waiter(with the same connection-lost exception it uses forwaiter) so that a lost transport cannot leave a permanently pending cancel.
Workaround we use
An application-level guard that, on TimeoutError/CancelledError, runs asyncio.wait_for(conn.close(timeout=g), g) and on expiry calls conn.terminate() followed by an explicit conn._transport.abort(), because terminate() alone leaves the socket open once close() has marked the protocol as closing.
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 469
- 平均合并
- 4 小时 42 分钟
- 30 天内合并 PR
- 6
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MagicStack/asyncpg 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
MagicStack/asyncpg#1357 ·
-
难度 2/5 1-3 小时 新手友好度 65/100
MagicStack/asyncpg#997 ·
-
难度 4/5 3-5 天 新手友好度 48/100
MagicStack/asyncpg#1354 ·
-
难度 3/5 1-2 天 新手友好度 72/100
MagicStack/asyncpg#1342 ·
-
难度 3/5 1-2 天 新手友好度 56/100
MagicStack/asyncpg#1340 · 1 条评论 ·
查看 MagicStack/asyncpg 的全部 Issue
相似的 Issue
-
documentation help wanted
难度 2/5 1-3 小时 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 90/100
simonw/sqlite-utils#872 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 78/100