From: Daniele Varrazzo Date: Mon, 15 Aug 2022 00:46:19 +0000 (+0200) Subject: fix(psycopg_c): guard PQflush calls for invalid connections X-Git-Tag: 3.1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aad2c7ebe553927896a2233410f8a3f06562cf33;p=thirdparty%2Fpsycopg.git fix(psycopg_c): guard PQflush calls for invalid connections Unlike other libpq functions, PQflush() doesn't have a guard for invalid connections and will segfault if the connection gets closed. Also check the return value of the PQflush calls: as we are not using the wrapped version pgconn.flush(), the function doesn't automatically throw an exception on failure. --- diff --git a/psycopg/psycopg/pq/pq_ctypes.py b/psycopg/psycopg/pq/pq_ctypes.py index 33c607449..ae3deed78 100644 --- a/psycopg/psycopg/pq/pq_ctypes.py +++ b/psycopg/psycopg/pq/pq_ctypes.py @@ -539,6 +539,7 @@ class PGconn: ) def flush(self) -> int: + # PQflush segfaults if it receives a NULL connection if not self._pgconn_ptr: raise e.OperationalError("flushing failed: the connection is closed") rv: int = impl.PQflush(self._pgconn_ptr) diff --git a/psycopg_c/psycopg_c/_psycopg/generators.pyx b/psycopg_c/psycopg_c/_psycopg/generators.pyx index 85c3a0b00..f244f24bc 100644 --- a/psycopg_c/psycopg_c/_psycopg/generators.pyx +++ b/psycopg_c/psycopg_c/_psycopg/generators.pyx @@ -93,7 +93,7 @@ def send(pq.PGconn pgconn) -> PQGen[None]: cdef int cires while True: - if libpq.PQflush(pgconn_ptr) == 0: + if pgconn.flush() == 0: break status = yield WAIT_RW @@ -259,7 +259,7 @@ def pipeline_communicate( if ready & Ready.W: - libpq.PQflush(pgconn_ptr) + pgconn.flush() if not commands: break commands.popleft()()