From: Daniele Varrazzo Date: Fri, 21 Feb 2025 16:27:29 +0000 (+0100) Subject: fix(pool): check that there is some connection in the pool before shrinking X-Git-Tag: pool-3.2.5~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fcf843112c608d4ec398624ecd597daf7a69cf41;p=thirdparty%2Fpsycopg.git fix(pool): check that there is some connection in the pool before shrinking 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 --- diff --git a/docs/news_pool.rst b/docs/news_pool.rst index d7169f1a1..c7bb625d8 100644 --- a/docs/news_pool.rst +++ b/docs/news_pool.rst @@ -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 ^^^^^^^^^^^^^^^^^^ diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index e93440e21..8f479a721 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -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 diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index 08b39477e..f066a9223 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -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