]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(pool): reinforce handling of errors in queued clients
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 16 Mar 2023 21:15:51 +0000 (22:15 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 16 Mar 2023 21:21:41 +0000 (22:21 +0100)
Unlike the async counterpart, I have no idea how to test this condition,
because I don't think you can cancel a Python thread. I could put the
pool and the first client in threads and use the main thread as queued
client, but it seems pretty convoluted.

However, an error handling in the same place that fixed #509 in the
async pools doesn't hurt.

psycopg_pool/psycopg_pool/pool.py

index dd50d73a6de4aea2c8bc0bef7b001a81694a9d5d..498ef7020f8521a6c07807460863263c54cfe248 100644 (file)
@@ -714,7 +714,7 @@ class WaitingClient:
 
     def __init__(self) -> None:
         self.conn: Optional[Connection[Any]] = None
-        self.error: Optional[Exception] = None
+        self.error: Optional[BaseException] = None
 
         # The WaitingClient behaves in a way similar to an Event, but we need
         # to notify reliably the flagger that the waiter has "accepted" the
@@ -730,10 +730,13 @@ class WaitingClient:
         """
         with self._cond:
             if not (self.conn or self.error):
-                if not self._cond.wait(timeout):
-                    self.error = PoolTimeout(
-                        f"couldn't get a connection after {timeout} sec"
-                    )
+                try:
+                    if not self._cond.wait(timeout):
+                        self.error = PoolTimeout(
+                            f"couldn't get a connection after {timeout} sec"
+                        )
+                except BaseException as ex:
+                    self.error = ex
 
         if self.conn:
             return self.conn