]> 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 18:05:53 +0000 (19:05 +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 d7169f1a1e6f0ee55f1b74110c28adb047405bed..c7bb625d870557616c853a05da9f09c8bca125ca 100644 (file)
@@ -7,6 +7,18 @@
 ``psycopg_pool`` release notes
 ==============================
 
+Future releases
+---------------
+
+psycopg_pool 3.2.5 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Fix spurious warning logging on pool shrinking (:ticket:`#1001`).
+
+
+Current release
+---------------
+
 psycopg_pool 3.2.4
 ^^^^^^^^^^^^^^^^^^
 
@@ -15,9 +27,6 @@ psycopg_pool 3.2.4
   on Python 3.13 (see :ticket:`#954`).
 
 
-Current release
----------------
-
 psycopg_pool 3.2.3
 ^^^^^^^^^^^^^^^^^^
 
index e93440e214baafe055189bd7ac649d1a1ec94329..8f479a7216ed8afcd24ff798b1756f4610bace03 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 08b39477e992b61b0b9a950da3fe0c9abea96cd5..f066a9223759a355ed5a23014427c026c99bb569 100644 (file)
@@ -856,7 +856,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