]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Awake the pool client outside the critical section
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 13 Feb 2021 22:42:00 +0000 (23:42 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
psycopg3/psycopg3/pool.py

index 358203ec5e4d0ff385053753e0ba506af2ee429c..b4a9eaf17986c79c718dbba4a3bf6df2a48e7948 100644 (file)
@@ -148,14 +148,18 @@ class ConnectionPool:
         # Critical section: if there is a client waiting give it the connection
         # otherwise put it back into the pool.
         with self._lock:
+            pos: Optional[WaitingClient] = None
             if self._waiting:
-                # Give the connection to the client and notify it
+                # Extract the first client from the queue
                 pos = self._waiting.popleft()
-                pos.set(conn)
             else:
-                # No client waiting for a connection: put it back into the queue
+                # No client waiting for a connection: put it back into the pool
                 self._pool.append(conn)
 
+        # If we found a client in queue, give it the connection and notify it
+        if pos:
+            pos.set(conn)
+
     def _reset_transaction_status(self, conn: Connection) -> None:
         """
         Bring a connection to IDLE state or close it.