From: Daniele Varrazzo Date: Wed, 24 Jan 2024 19:03:24 +0000 (+0000) Subject: fix: only cancel query on interrupt if the connection is in active state X-Git-Tag: 3.2.0~87^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0f7e6076eeeca336f830d76d483b68a1f99f8307;p=thirdparty%2Fpsycopg.git fix: only cancel query on interrupt if the connection is in active state --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index c7f1519ee..2c503a126 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -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 diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 585888d9d..3a57df375 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -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