]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(pool): check that there is some connection in the pool before shrinking
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 21 Feb 2025 16:27:29 +0000 (17:27 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 21 Feb 2025 17:11:47 +0000 (18:11 +0100)
There is a race condition between shrinking the pool and closing an
expired connection which might result in attempting to pop from the
empty _pool deque. Add a check to prevent that.

The bug is not serious as it doesn't compromise the pool working but it causes
noise in the logging.

Close #1001

docs/news_pool.rst
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py

index 848beda9a27969f13caec5c580284ddd11aedeb1..b3ee8b57a76a72db51a2d6b5a3417939c1714ff0 100644 (file)
@@ -7,6 +7,12 @@
 ``psycopg_pool`` release notes
 ==============================
 
+psycopg_pool 3.2.5 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Fix spurious warning logging on pool shrinking (:ticket:`#1001`).
+
+
 Current release
 ---------------
 
index a679ca58a0c38ca29b9536f2fbae7ac1578f9231..9da5890f86eb8d4a322db1ec0838e22d03137ff3 100644 (file)
@@ -797,7 +797,7 @@ class ConnectionPool(Generic[CT], BasePool):
             self._nconns_min = len(self._pool)
 
             # If the pool can shrink and connections were unused, drop one
-            if self._nconns > self._min_size and nconns_min > 0:
+            if self._nconns > self._min_size and nconns_min > 0 and self._pool:
                 to_close = self._pool.popleft()
                 self._nconns -= 1
                 self._nconns_min -= 1
index 9d3432796e31cab62c240b11b08bc66535ee55ba..c17f5f54264d83093fd32e4dd8fa9e547a5037e4 100644 (file)
@@ -857,7 +857,7 @@ class AsyncConnectionPool(Generic[ACT], BasePool):
             self._nconns_min = len(self._pool)
 
             # If the pool can shrink and connections were unused, drop one
-            if self._nconns > self._min_size and nconns_min > 0:
+            if self._nconns > self._min_size and nconns_min > 0 and self._pool:
                 to_close = self._pool.popleft()
                 self._nconns -= 1
                 self._nconns_min -= 1