From 6ae261ea7668f4c850874666efec6fef658b08c0 Mon Sep 17 00:00:00 2001 From: Jean Raby Date: Sun, 10 Nov 2024 07:06:57 -0500 Subject: [PATCH] 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. --- lib/sqlalchemy/dialects/postgresql/asyncpg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, -- 2.47.3