]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Re-raise ``CancelledError`` in asyncpg
authorFederico Caselli <cfederico87@gmail.com>
Thu, 10 Jul 2025 22:21:54 +0000 (00:21 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 10 Jul 2025 22:24:06 +0000 (00:24 +0200)
Re-raise catched ``CancelledError`` in the terminate method of the
asyncpg dialect to avoid possible hangs of the code execution.

Fixes: #12728
Change-Id: Ia9a353ac7504592be00355001ef40b13ab51375c
(cherry picked from commit 36da2eaf3e23269f2cf28420ae73674beafd0661)

doc/build/changelog/unreleased_20/12728.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/asyncpg.py

diff --git a/doc/build/changelog/unreleased_20/12728.rst b/doc/build/changelog/unreleased_20/12728.rst
new file mode 100644 (file)
index 0000000..f710370
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 12728
+
+    Re-raise catched ``CancelledError`` in the terminate method of the
+    asyncpg dialect to avoid possible hangs of the code execution.
+
index 096892127ba1d33ab28b5ab2a5e444270b255a02..36566b67409fb92739f335960f7298908274a6ed 100644 (file)
@@ -910,13 +910,16 @@ class AsyncAdapt_asyncpg_connection(AdaptedConnection):
                 asyncio.CancelledError,
                 OSError,
                 self.dbapi.asyncpg.PostgresError,
-            ):
+            ) as e:
                 # in the case where we are recycling an old connection
                 # that may have already been disconnected, close() will
                 # fail with the above timeout.  in this case, terminate
                 # the connection without any further waiting.
                 # see issue #8419
                 self._connection.terminate()
+                if isinstance(e, asyncio.CancelledError):
+                    # re-raise CancelledError if we were cancelled
+                    raise
         else:
             # not in a greenlet; this is the gc cleanup case
             self._connection.terminate()