]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: only cancel query on interrupt if the connection is in active state
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 24 Jan 2024 19:03:24 +0000 (19:03 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 29 Jan 2024 02:25:32 +0000 (02:25 +0000)
psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py

index c7f1519eeb80279d753cfe23402d709cb355e401..2c503a1267de44779be26c20f1e10ce2d206e582 100644 (file)
@@ -43,6 +43,7 @@ TEXT = pq.Format.TEXT
 BINARY = pq.Format.BINARY
 
 IDLE = pq.TransactionStatus.IDLE
+ACTIVE = pq.TransactionStatus.ACTIVE
 INTRANS = pq.TransactionStatus.INTRANS
 
 _INTERRUPTED = KeyboardInterrupt
@@ -321,13 +322,14 @@ class Connection(BaseConnection[Row]):
         try:
             return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
         except _INTERRUPTED:
-            # On Ctrl-C, try to cancel the query in the server, otherwise
-            # the connection will remain stuck in ACTIVE state.
-            self._try_cancel(self.pgconn)
-            try:
-                waiting.wait(gen, self.pgconn.socket, timeout=timeout)
-            except e.QueryCanceled:
-                pass  # as expected
+            if self.pgconn.transaction_status == ACTIVE:
+                # On Ctrl-C, try to cancel the query in the server, otherwise
+                # the connection will remain stuck in ACTIVE state.
+                self._try_cancel(self.pgconn)
+                try:
+                    waiting.wait(gen, self.pgconn.socket, timeout=timeout)
+                except e.QueryCanceled:
+                    pass  # as expected
             raise
 
     @classmethod
index 585888d9d583daf3172ccf64904996b00a53c6ff..3a57df3750635a48d2143fb5538c2a8c4fd58b57 100644 (file)
@@ -45,6 +45,7 @@ TEXT = pq.Format.TEXT
 BINARY = pq.Format.BINARY
 
 IDLE = pq.TransactionStatus.IDLE
+ACTIVE = pq.TransactionStatus.ACTIVE
 INTRANS = pq.TransactionStatus.INTRANS
 
 if True:  # ASYNC
@@ -337,13 +338,14 @@ class AsyncConnection(BaseConnection[Row]):
         try:
             return await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
         except _INTERRUPTED:
-            # On Ctrl-C, try to cancel the query in the server, otherwise
-            # the connection will remain stuck in ACTIVE state.
-            self._try_cancel(self.pgconn)
-            try:
-                await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
-            except e.QueryCanceled:
-                pass  # as expected
+            if self.pgconn.transaction_status == ACTIVE:
+                # On Ctrl-C, try to cancel the query in the server, otherwise
+                # the connection will remain stuck in ACTIVE state.
+                self._try_cancel(self.pgconn)
+                try:
+                    await waiting.wait_async(gen, self.pgconn.socket, timeout=timeout)
+                except e.QueryCanceled:
+                    pass  # as expected
             raise
 
     @classmethod