ConnectionError from _cancel() during CancelledError not caught, crashes callers
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 connect_utils.py 中的 _cancel 和 _create_ssl_connection 开始,然后检查 TLSUpgradeProto.connection_lost(),以了解取消失败是如何转换的。为 SSL 取消连接失败添加一个回归测试,防止原始的 ConnectionError 传递到调用方,然后运行相关的取消和连接测试。
由索引模型根据 Issue 内容生成。
描述
Environment
- asyncpg version: 0.31.0 (also reproduced on 0.30.x)
- PostgreSQL version: 16
- Python version: 3.11.14
- Platform: Linux (Kubernetes)
- pgbouncer: No
- SQLAlchemy: 2.0.23
Summary
When an asyncpg operation is cancelled via asyncio.CancelledError while mid-query, the cancellation mechanism in connect_utils._cancel can raise a built-in ConnectionError that escapes to the caller. This is problematic because:
- Callers (e.g. SQLAlchemy) expect asyncpg-specific exception types and don't handle built-in
ConnectionError - The cancel operation is inherently best-effort — if the cancel connection fails, the error should be suppressed or wrapped, not propagated
This is related to #1211 but occurs on non-direct_tls connections via the cancel request code path.
Reproduction flow
- An asyncpg connection is executing a query (e.g. inside SQLAlchemy's
session.execute()) - The asyncio task is cancelled (
task.cancel()) CancelledErrorpropagates intoprotocol.query()/bind_execute- asyncpg's cancellation handler tries to send a PostgreSQL cancel request by opening a new SSL connection via
connect_utils._cancel→_create_ssl_connection - The new connection fails (server already closed the original, or network issue)
TLSUpgradeProto.connection_lost()raises built-inConnectionError('unexpected connection_lost() call')- This escapes through
connect_utils._cancel(which has no error handling around_create_ssl_connection) - Caller receives
ConnectionErrorinstead ofCancelledError
Traceback
asyncio.exceptions.CancelledError (original exception)
During handling of the above exception, another exception occurred:
File "asyncpg/transaction.py", line 206, in __rollback
await self._connection.execute(query)
File "asyncpg/connection.py", line 350, in execute
result = await self._protocol.query(query, timeout)
File "asyncpg/connection.py", line 1584, in _cancel
await connect_utils._cancel(
File "asyncpg/connect_utils.py", line 1040, in _cancel
tr, pr = await _create_ssl_connection(
File "asyncpg/connect_utils.py", line 752, in _create_ssl_connection
do_ssl_upgrade = await pr.on_data
^^^^^^^^^^^^^^^^
ConnectionError: unexpected connection_lost() call
Root cause
Two issues in connect_utils.py:
1. _cancel() has no error handling around _create_ssl_connection
async def _cancel(*, loop, addr, params, backend_pid, backend_secret):
...
if params.ssl and params.sslmode != SSLMode.allow:
tr, pr = await _create_ssl_connection(...) # ← no try/except!
...
The cancel request is best-effort (we're telling PostgreSQL to cancel a query on a connection that may already be dead). If opening the cancel connection fails, the error should be suppressed or wrapped in asyncpg.InterfaceError, not propagated as a raw ConnectionError.
2. TLSUpgradeProto.connection_lost() raises built-in ConnectionError
def connection_lost(self, exc):
if not self.on_data.done():
if exc is None:
exc = ConnectionError('unexpected connection_lost() call')
self.on_data.set_exception(exc)
This raises a built-in Python ConnectionError, not an asyncpg exception type. Callers like SQLAlchemy check for asyncpg.InterfaceError or asyncpg.PostgresError to detect disconnects. A built-in ConnectionError bypasses all those checks, which means:
- SQLAlchemy's
is_disconnect()doesn't recognize it - SQLAlchemy's pool pre-ping handler (
_do_ping_w_event) only catchesself.loaded_dbapi.Error, soConnectionErrorescapes - The pool's retry logic (which would create a fresh connection) never triggers
Suggested fix
Option A (minimal): Catch OSError (parent of ConnectionError) in connect_utils._cancel() and suppress it — cancel is best-effort:
async def _cancel(*, loop, addr, params, backend_pid, backend_secret):
...
try:
if params.ssl and params.sslmode != SSLMode.allow:
tr, pr = await _create_ssl_connection(...)
...
except OSError:
# Cancel is best-effort. If we can't reach the server, the
# connection is dead anyway.
return
Option B (comprehensive): Also change TLSUpgradeProto.connection_lost() to raise asyncpg.InterfaceError instead of built-in ConnectionError, so callers can handle it consistently:
def connection_lost(self, exc):
if not self.on_data.done():
if exc is None:
exc = InterfaceError('unexpected connection_lost() call')
self.on_data.set_exception(exc)
Impact
This causes process crashes in production services. When a task is cancelled during a DB query, the ConnectionError escapes all exception handlers (which expect either CancelledError or asyncpg-specific exceptions) and terminates the process.
This is 100% correlated with CancelledError in our logs — every ConnectionError: unexpected connection_lost() we've seen is triggered by task cancellation.
Additional context
We use Google CloudSQL with SSL connections. The PostgreSQL server is accessed over SSL (non-direct_tls), which means the cancel code path goes through _create_ssl_connection to establish a new SSL connection for sending the cancel request.
- 主要语言
- Python
- 星标
- 8.1k
- 派生
- 474
- 平均合并
- 3 天 14 小时
- 30 天内合并 PR
- 17
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MagicStack/asyncpg 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 72/100
MagicStack/asyncpg#1342 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 56/100
MagicStack/asyncpg#1340 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 28/100
MagicStack/asyncpg#1337 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 42/100
MagicStack/asyncpg#1330 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 38/100
MagicStack/asyncpg#1322 ·
维护者通常 1 天内回复
查看 MagicStack/asyncpg 的全部 Issue
相似的 Issue
-
New Internship未关闭new_internship
难度 1/5 1 小时以内 新手友好度 70/100
-
[BUG] Reports tab: "Unban" button tooltip shows raw `{{ip}}` placeholder instead of the IP address未关闭bug javascript ui
难度 2/5 1-3 小时 新手友好度 68/100
bunkerity/bunkerweb#4001 · 1 条评论 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 92/100
PedestrianDynamics/pyFDS-Evac#476 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
google/differential-privacy#516 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
adobe-fonts/source-serif#153 ·