]> 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:22:19 +0000 (00:22 +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

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 6b9bb0677daddcdba3c7cc3acaee93105e43d16e..a5a5dc08fa3ddb572c01052aa48ff25a3b821656 100644 (file)
@@ -956,13 +956,16 @@ class AsyncAdapt_asyncpg_connection(AsyncAdapt_dbapi_connection):
                 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()