From: Federico Caselli Date: Fri, 17 Jan 2025 20:16:51 +0000 (+0100) Subject: asyncpg: shield connection close in terminate to avoid connection leak X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2f6ca6554ddd725849dd6b2d32bf495391087bec;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git asyncpg: shield connection close in terminate to avoid connection leak Added an additional ``shield()`` call within the connection terminate process of the asyncpg driver, to mitigate an issue where terminate would be prevented from completing under the anyio concurrency library. Fixes #12077 Closes: #12076 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12076 Pull-request-sha: 6ae261ea7668f4c850874666efec6fef658b08c0 Change-Id: Iaec63d02b620201dc60ffdad76ba9d61d427cac1 --- diff --git a/doc/build/changelog/unreleased_20/12077.rst b/doc/build/changelog/unreleased_20/12077.rst new file mode 100644 index 0000000000..ac1c5a95e5 --- /dev/null +++ b/doc/build/changelog/unreleased_20/12077.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: postgresql, usecase, asyncio + :tickets: 12077 + + Added an additional ``shield()`` call within the connection terminate + process of the asyncpg driver, to mitigate an issue where terminate would + be prevented from completing under the anyio concurrency library. diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index 3a1c7b3f71..4461c9c204 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -930,7 +930,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,