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-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=cf3ca5098cab53ef15262e9879f6a8d838310e0c;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 848beda9a..b3ee8b57a 100644 --- a/docs/news_pool.rst +++ b/docs/news_pool.rst @@ -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 --------------- diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index a679ca58a..9da5890f8 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 9d3432796..c17f5f542 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -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