"cancel() cannot be used with a prepared two-phase transaction"
)
- c = self.pgconn.get_cancel()
- c.cancel()
+ self._try_cancel(self.pgconn)
+
+ @classmethod
+ def _try_cancel(cls, pgconn: "PGconn") -> None:
+ try:
+ # Can fail if the connection is closed
+ c = pgconn.get_cancel()
+ except Exception as ex:
+ logger.warning("couldn't try to cancel query: %s", ex)
+ else:
+ c.cancel()
def add_notice_handler(self, callback: NoticeHandler) -> None:
"""
except KeyboardInterrupt:
# On Ctrl-C, try to cancel the query in the server, otherwise
# the connection will remain stuck in ACTIVE state.
- c = self.pgconn.get_cancel()
- c.cancel()
+ self._try_cancel(self.pgconn)
try:
waiting.wait(gen, self.pgconn.socket, timeout=timeout)
except e.QueryCanceled:
except (asyncio.CancelledError, KeyboardInterrupt):
# On Ctrl-C, try to cancel the query in the server, otherwise
# the connection will remain stuck in ACTIVE state.
- c = self.pgconn.get_cancel()
- c.cancel()
+ self._try_cancel(self.pgconn)
try:
await waiting.wait_async(gen, self.pgconn.socket)
except e.QueryCanceled: