From: Jean Raby Date: Sun, 10 Nov 2024 12:06:57 +0000 (-0500) Subject: shield connection close in terminate X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F12076%2Fhead;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git shield connection close in terminate 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. --- diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index a362c616e1..02d2278456 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -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,