Avoid catching NotSupported, just check for the libpq version.
Also avoid the half exception handler in `_cancel_gen()`: as in the
legacy branch, warn and ignore any error happening in the outermost
method, without adding an ignore exception policy in an implementation
method.
Close #778.
return True
def _cancel_gen(self) -> PQGenConn[None]:
- try:
- cancel_conn = self.pgconn.cancel_conn()
- except e.OperationalError as ex: # if the connection is closed
- logger.warning("couldn't create a cancel connection: %s", ex)
- else:
- cancel_conn.start()
- yield from generators.cancel(cancel_conn)
+ cancel_conn = self.pgconn.cancel_conn()
+ cancel_conn.start()
+ yield from generators.cancel(cancel_conn)
def add_notice_handler(self, callback: NoticeHandler) -> None:
"""
If the underlying libpq is older than version 17, the method will fall
back to using the same implementation of `!cancel()`.
"""
- if self._should_cancel():
+ if not self._should_cancel():
+ return
+
+ # TODO: replace with capabilities.has_safe_cancel after merging #782
+ if pq.__build_version__ >= 170000:
try:
waiting.wait_conn(self._cancel_gen(), interval=_WAIT_INTERVAL)
- except e.NotSupportedError:
- self.cancel()
+ except Exception as ex:
+ logger.warning("couldn't try to cancel query: %s", str(ex))
+ else:
+ self.cancel()
@contextmanager
def transaction(
If the underlying libpq is older than version 17, the method will fall
back to using the same implementation of `!cancel()`.
"""
- if self._should_cancel():
+ if not self._should_cancel():
+ return
+
+ # TODO: replace with capabilities.has_safe_cancel after merging #782
+ if pq.__build_version__ >= 170000:
try:
await waiting.wait_conn_async(
self._cancel_gen(), interval=_WAIT_INTERVAL
)
- except e.NotSupportedError:
- if True: # ASYNC
- await to_thread(self.cancel)
- else:
- self.cancel()
+ except Exception as ex:
+ logger.warning("couldn't try to cancel query: %s", str(ex))
+
+ else:
+ if True: # ASYNC
+ await to_thread(self.cancel)
+ else:
+ self.cancel()
@asynccontextmanager
async def transaction(