From: aron-intframe (@aron-intframe) <noreply+aron-intframe@github.com>
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Subject: Re: [psycopg/psycopg] issue #1502: AsyncConnectionPool: a cancel that reaches a worker mid-connect is swallowed, so asyncio.run() never returns if the pool was not closed
Date: Thu, 19 Nov 2026 03:30:43 +0000
Message-ID: <gh-psycopg-psycopg-1502-c6998817802@github.com> (raw)
In-Reply-To: <gh-psycopg-psycopg-1502@github.com>
References: <gh-psycopg-psycopg-1502@github.com>
Your diagnosis is right, and there is a second place that needs the same change: `worker()` also catches `CLIENT_EXCEPTIONS` (which includes `asyncio.CancelledError`) around `task.run()`. So if `_add_connection()` re-raises, the worker swallows the cancellation one level up and goes back to `q.get()`.
I ran your script on psycopg 3.3.9 / psycopg-pool 3.3.3 (Python 3.12.3) and on master `bb75838`. I added `faulthandler.dump_traceback_later(5, exit=True)` so the hang prints a traceback instead of needing a kill:
```
$ python -I repro.py
WARNING:psycopg.pool:error connecting in 'pool-1':
Timeout (0:00:05)!
File "/usr/lib/python3.12/asyncio/runners.py", line 205 in _cancel_all_tasks
```
When only `_add_connection()` re-raises, it still hangs, and the log line moves to the worker:
```
WARNING:psycopg.pool:task run <AddConnection 'pool-1' at 0x7d66cb9e4140> failed: CancelledError:
Timeout (0:00:05)!
```
With both changes below:
```
$ python -I repro.py
asyncio.run() returned and raised: RuntimeError('startup failed elsewhere, before pool.close()')
```
Patch against master. Both changes sit inside `if True: # ASYNC` blocks, so the generated sync pool does not change (`tools/async_to_sync.py --check` passes):
```diff
inline patch 1: psycopg_pool/psycopg_pool/pool_async.py (+14/-0 lines) download inline-1.patch--- a/psycopg_pool/psycopg_pool/pool_async.py+++ b/psycopg_pool/psycopg_pool/pool_async.py@@ -686,6 +686,11 @@ class AsyncConnectionPool(Generic[ACT], BasePool):
try:
await task.run()
except CLIENT_EXCEPTIONS as ex:
+ if True: # ASYNC+ if isinstance(ex, asyncio.CancelledError):+ # The worker itself is being cancelled (e.g. by+ # asyncio.run() shutting down): terminate.+ raise
logger.warning(
"task run %s failed: %s: %s", task, ex.__class__.__name__, ex
)
@@ -757,6 +762,15 @@ class AsyncConnectionPool(Generic[ACT], BasePool):
try:
conn = await self._connect()
except CLIENT_EXCEPTIONS as ex:
+ if True: # ASYNC+ if isinstance(ex, asyncio.CancelledError):+ # Cancelled while connecting: don't schedule a retry,+ # let the cancellation reach the worker.+ async with self._lock:+ self._nconns -= 1+ if growing and self._growing:+ self._growing = False+ raise
logger.warning("error connecting in %r: %s", self.name, ex)
if attempt.time_to_give_up(now):
logger.warning(
```
The `_nconns` / `_growing` lines do the same bookkeeping as the give-up branch. That keeps the counters right if the pool is still used after a worker has been cancelled.
`pytest tests/pool` against a local PostgreSQL 18.7 with the patch: `351 passed, 8 skipped`.
One path this does not cover: `_reset_connection()` also catches `CancelledError` around `rollback()` and the reset callback. A cancel that lands while a returned connection is being reset would leave the worker running in the same way. I have not built a repro for that path. On Python 3.11+, one check at the end of the worker loop should cover every place that swallows the cancel, because `Task.cancelling()` stays non-zero after the `CancelledError` is caught (I only exercised it on the connect path):
```python
if True: # ASYNC
if (t := asyncio.current_task()) and t.cancelling():
raise asyncio.CancelledError
```
That variant alone (without the other two changes) also makes your script return. It is not available on 3.10, which psycopg_pool still supports, which is why the patch above re-raises explicitly.
Until a fix ships, the workaround you listed is the right one: `async with AsyncConnectionPool(...)` or `try/finally: await pool.close()`.