MagicStack / MagicStack/asyncpg
Connection.close(timeout=) waits forever on a pending cancel when the server never acknowledges it
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 8.1k
- Forks
- 468
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
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.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez dans asyncpg/protocol/protocol.pyx en lisant close(self, timeout), _request_cancel(), _handle_waiter_on_connection_lost() et _on_connection_lost(). Reproduisez le cas de serveur figé décrit dans l’issue, puis vérifiez que close(timeout=2) retourne et que la perte du transport ne laisse pas cancel_waiter dans l’état pending, tandis que la connexion revient à l’abandon comme prévu.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- postgresql, python
- Domaine
- databases
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Active
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 68/100