From: LeaderOFGnomes (@LeaderOFGnomes) <noreply+LeaderOFGnomes@github.com>
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Subject: [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 02:57:14 +0000
Message-ID: <gh-psycopg-psycopg-1502@github.com> (raw)
**Versions:** psycopg 3.3.9, psycopg-pool 3.3.3, Python 3.12.16, Linux. The same code is on master (`psycopg_pool/pool_async.py`).
**What happens**
If an exception escapes the main coroutine of `asyncio.run()` while an `AsyncConnectionPool` is still open and one of its workers is connecting to a server that accepted the TCP connection but has not answered yet, `asyncio.run()` never returns. The process stays alive, and the original exception is never raised or printed.
**Reproduction**
```python
import asyncio
import logging
import socket
import threading
from psycopg_pool import AsyncConnectionPool
logging.basicConfig(level=logging.WARNING)
def silent_server() -> int:
"""A TCP port that accepts connections and never answers (a server that hangs)."""
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
sock.listen()
held = []
def accept_forever():
while True:
held.append(sock.accept()[0])
threading.Thread(target=accept_forever, daemon=True).start()
return sock.getsockname()[1]
async def main():
port = silent_server()
pool = AsyncConnectionPool(f"host=127.0.0.1 port={port} dbname=x user=x", min_size=1, open=False)
await pool.open() # wait=False: a worker starts connecting in the background
await asyncio.sleep(0.1)
raise RuntimeError("startup failed elsewhere, before pool.close()")
asyncio.run(main()) # expected: RuntimeError; actual: never returns
```
Output, then nothing (the process has to be killed):
```text
WARNING:psycopg.pool:error connecting in 'pool-1':
```
**What we think happens**
`asyncio.run()` cancels the pending tasks and waits for them (`asyncio.runners._cancel_all_tasks`). The worker is in `_add_connection()`, awaiting `self._connect()`. In `pool_async.py`, `CLIENT_EXCEPTIONS = (Exception, asyncio.CancelledError)`, so `except CLIENT_EXCEPTIONS as ex:` catches the `CancelledError` and logs it as a connection error. That is why the message after the colon is empty: `str(CancelledError())` is `''`. A retry is scheduled, and the worker goes back to `await q.get()` in `worker()`. The task's only cancellation has been used up, so the `gather()` in `_cancel_all_tasks` waits forever.
A stack dump at that point shows the main thread in `runners._cancel_all_tasks`. The task `pool-1-worker-0` is in state "cancelling" at `pool_async.py:679` (`if isinstance((task := (await q.get())), StopWorker)`).
**Expected**
A worker task that is cancelled ends: it re-raises `CancelledError`, or at least does not go back to its loop. Then `asyncio.run()` can finish and raise the original exception.
**Workaround**
Close the pool on every exit path (`try/finally: await pool.close()`, or `async with`). After `close()` has queued a `StopWorker`, the worker ends once it is back at `q.get()`. `open(wait=True, timeout=...)` also helps, because `wait()` closes the pool on timeout.
We understand why `CancelledError` is caught on the client side, where it keeps the pool consistent when a caller is cancelled. The issue is only that a worker's own cancellation never ends the worker.