]> 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:37:26 +0000 (15:37 +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 666e95155eb7303d2b4aeb7307a2e77453636eef..1805798ab25140c475e1f18fdb9fa4df64619ab7 100644 (file)
@@ -529,6 +529,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 66721cf9f4f5e5802a2c652aae86b7eb3890f5ee..ab71b94b05cb39d7ba326f667282fd08e43e1f4a 100644 (file)
@@ -88,7 +88,7 @@ def send(pq.PGconn pgconn) -> PQGen[None]:
     cdef int cires
 
     while 1:
-        if libpq.PQflush(pgconn_ptr) == 0:
+        if pgconn.flush() == 0:
             break
 
         status = yield WAIT_RW