From caa454b179eeab31d475b8939f350a1568f7242b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 27 Aug 2022 21:29:56 +0100 Subject: [PATCH] fix: use a "finally" to clear the result pointer after handling notifies The chance for this to fail failed is slim (an error in the logger? A ctrl-c just there?) but a bit of paranoia has never hurt anyone. --- psycopg/psycopg/pq/pq_ctypes.py | 4 ++-- psycopg_c/psycopg_c/pq/pgconn.pyx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/psycopg/psycopg/pq/pq_ctypes.py b/psycopg/psycopg/pq/pq_ctypes.py index f01a0d231..a2c86dc40 100644 --- a/psycopg/psycopg/pq/pq_ctypes.py +++ b/psycopg/psycopg/pq/pq_ctypes.py @@ -56,8 +56,8 @@ def notice_receiver(arg: c_void_p, result_ptr: impl.PGresult_struct) -> None: pgconn.notice_handler(res) except Exception as exc: logger.exception("error in notice receiver: %s", exc) - - res._pgresult_ptr = None # avoid destroying the pgresult_ptr + finally: + res._pgresult_ptr = None # avoid destroying the pgresult_ptr class PGconn: diff --git a/psycopg_c/psycopg_c/pq/pgconn.pyx b/psycopg_c/psycopg_c/pq/pgconn.pyx index c96dd3b26..513e2f9f4 100644 --- a/psycopg_c/psycopg_c/pq/pgconn.pyx +++ b/psycopg_c/psycopg_c/pq/pgconn.pyx @@ -661,8 +661,8 @@ cdef void notice_receiver(void *arg, const libpq.PGresult *res_ptr) with gil: pgconn.notice_handler(res) except Exception as e: logger.exception("error in notice receiver: %s", e) - - res._pgresult_ptr = NULL # avoid destroying the pgresult_ptr + finally: + res._pgresult_ptr = NULL # avoid destroying the pgresult_ptr cdef (Py_ssize_t, libpq.Oid *, char * const*, int *, int *) _query_params_args( -- 2.47.3