From: Daniele Varrazzo Date: Mon, 22 Feb 2021 01:49:16 +0000 (+0100) Subject: Change state in critical section on pool.close() X-Git-Tag: 3.0.dev0~87^2~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1831b8f0fff198fc557b0a7c7af85dc8aa12ab0e;p=thirdparty%2Fpsycopg.git Change state in critical section on pool.close() Nothing bad should happen, because what maintains the state respect the _closed state. However because the logic is complex make it more defensive. --- diff --git a/psycopg3/psycopg3/pool.py b/psycopg3/psycopg3/pool.py index 20b3159d5..e3879afb0 100644 --- a/psycopg3/psycopg3/pool.py +++ b/psycopg3/psycopg3/pool.py @@ -332,6 +332,12 @@ class ConnectionPool: with self._lock: self._closed = True + # Take waiting client and pool connections out of the state + waiting = list(self._waiting) + self._waiting.clear() + pool = list(self._pool) + self._pool.clear() + # Now that the flag _closed is set, getconn will fail immediately, # putconn will just close the returned connection. @@ -343,13 +349,11 @@ class ConnectionPool: self.run_task(StopWorker(self)) # Signal to eventual clients in the queue that business is closed. - while self._waiting: - pos = self._waiting.popleft() + for pos in waiting: pos.fail(PoolClosed(f"the pool {self.name!r} is closed")) # Close the connections still in the pool - while self._pool: - conn = self._pool.pop()[0] + for conn, _ in pool: conn.close() # Wait for the worker threads to terminate