]> 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>
Sat, 18 Oct 2025 23:57:46 +0000 (01:57 +0200)
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..35478de4bb6c8f9ac1756d7acedccca1113f3520 100644 (file)
@@ -7,6 +7,15 @@
 ``psycopg_pool`` release notes
 ==============================
 
+Future releases
+---------------
+
+psycopg_pool 3.2.5 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Fix spurious warning logging on pool shrinking (:ticket:`#1001`).
+
+
 Current release
 ---------------
 
index 24090ca9793d7e4c723e4c463c5264ca240cc077..64906e60dd904ababef55bdf58ca9b220fd9d5b9 100644 (file)
@@ -789,7 +789,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 f6c06df12f915db05f06dca189082e9e016a6c1e..f39605e918547e744c09214de4530d7180519a3e 100644 (file)
@@ -849,7 +849,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