]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
shield connection close in terminate 12076/head
authorJean Raby <jean@raby.sh>
Sun, 10 Nov 2024 12:06:57 +0000 (07:06 -0500)
committerJean Raby <jean@raby.sh>
Fri, 15 Nov 2024 13:35:42 +0000 (08:35 -0500)
If terminate is called from an asyncio.CancelledError exception handler,
under an anyio task group, await will bail with asyncio.CancelledError
right away, leading to a connection leak.

Shielding the close() call allows the connection to be properly closed
in those cases.

lib/sqlalchemy/dialects/postgresql/asyncpg.py

index a362c616e1d0d110cfa28137a13e1c860a955784..02d22784566cb18c471a91e0ca7c21add732d5f0 100644 (file)
@@ -915,7 +915,7 @@ class AsyncAdapt_asyncpg_connection(AsyncAdapt_dbapi_connection):
             try:
                 # try to gracefully close; see #10717
                 # timeout added in asyncpg 0.14.0 December 2017
-                await_(self._connection.close(timeout=2))
+                await_(asyncio.shield(self._connection.close(timeout=2)))
             except (
                 asyncio.TimeoutError,
                 asyncio.CancelledError,