]> 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:11 +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 ad39a5c5602587bcde0b5cdcbd7ee29a6512cc2f..67447cac57c6256660fc228f1b05b21cdfc9febc 100644 (file)
@@ -719,7 +719,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
@@ -735,10 +735,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