From 24cbbe5472d9d68de4ae8fd3a3849134804ab9a8 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 13 Feb 2021 23:42:00 +0100 Subject: [PATCH] Awake the pool client outside the critical section --- psycopg3/psycopg3/pool.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/psycopg3/psycopg3/pool.py b/psycopg3/psycopg3/pool.py index 358203ec5..b4a9eaf17 100644 --- a/psycopg3/psycopg3/pool.py +++ b/psycopg3/psycopg3/pool.py @@ -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. -- 2.47.3