]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(psycopg_c): guard PQflush calls for invalid connections
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 15 Aug 2022 00:46:19 +0000 (02:46 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 15 Aug 2022 13:40:59 +0000 (15:40 +0200)
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.

psycopg/psycopg/pq/pq_ctypes.py
psycopg_c/psycopg_c/_psycopg/generators.pyx

index 33c607449dc208e0b5392958bea017fbf124d11d..ae3deed78933d7b60b666a462e5bfde2e4373583 100644 (file)
@@ -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)
index 85c3a0b00b700142d9407fe9357583dc243559ea..f244f24bc7b8749a65e79134acc6317e6e58a190 100644 (file)
@@ -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()()