]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Raise PoolClosed on wait() on a closed pool
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 Jan 2022 02:24:52 +0000 (03:24 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 Jan 2022 22:13:43 +0000 (23:13 +0100)
Previously it might have raised an assert, if, for instance, wait would
have failed and retried.

docs/news_pool.rst
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index c7ad91298e20e6b328f6f684c3a7ca5fb88d0362..dd055345ee99470c23e376af2c3d63caf7ce612a 100644 (file)
@@ -22,6 +22,7 @@ psycopg_pool 3.0.3 (unreleased)
 
 - Throw `!ValueError` if the pool `!min_size` is set to 0 (instead of
   hanging).
+- Throw `!PoolClosed` calling `~ConnectionPool.wait()` on a closed pool.
 
 
 Current release
index 895067c56982b13783ee81d7882669626c69076b..7e0b88bcec8f20b1822b80422f27baf6d77e3e5e 100644 (file)
@@ -78,6 +78,8 @@ class ConnectionPool(BasePool[Connection[Any]]):
         program to terminate in case the environment is not configured
         properly, rather than trying to stay up the hardest it can.
         """
+        self._check_open_getconn()
+
         with self._lock:
             assert not self._pool_full_event
             if len(self._pool) >= self._nconns:
index 297505e057d54b7d28472bfc255c247d06da5aae..e92209733ac22c8feac92104447a638f95344df4 100644 (file)
@@ -68,6 +68,8 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]):
             self._open()
 
     async def wait(self, timeout: float = 30.0) -> None:
+        self._check_open_getconn()
+
         async with self._lock:
             assert not self._pool_full_event
             if len(self._pool) >= self._nconns:
index f4d249e7a3a373bd222d53835fb8df1c30ca88f6..1a57367bf91da9c189b4e65fae1d3adfccb347ca 100644 (file)
@@ -8,7 +8,6 @@ from typing import Any, List, Tuple
 import pytest
 
 import psycopg
-from psycopg.errors import OperationalError
 from psycopg.pq import TransactionStatus
 from psycopg._compat import Counter
 
@@ -145,6 +144,14 @@ def test_wait_ready(dsn, monkeypatch):
         p.wait(0.0001)  # idempotent
 
 
+def test_wait_closed(dsn):
+    with pool.ConnectionPool(dsn) as p:
+        pass
+
+    with pytest.raises(pool.PoolClosed):
+        p.wait()
+
+
 @pytest.mark.slow
 def test_setup_no_timeout(dsn, proxy):
     with pytest.raises(pool.PoolTimeout):
@@ -749,7 +756,7 @@ def test_reopen(dsn):
     assert p._sched_runner is None
     assert not p._workers
 
-    with pytest.raises(OperationalError, match="cannot be reused"):
+    with pytest.raises(psycopg.OperationalError, match="cannot be reused"):
         p.open()
 
 
index 9a0a6d5a46f4d9da3dc107e11e890e818b023cdb..4d37bef77c9675528bf14b60c7b0c853a7c9aadf 100644 (file)
@@ -7,7 +7,6 @@ from typing import Any, List, Tuple
 import pytest
 
 import psycopg
-from psycopg.errors import OperationalError
 from psycopg.pq import TransactionStatus
 from psycopg._compat import create_task, Counter
 
@@ -146,6 +145,14 @@ async def test_wait_ready(dsn, monkeypatch):
         await p.wait(0.0001)  # idempotent
 
 
+async def test_wait_closed(dsn):
+    async with pool.AsyncConnectionPool(dsn) as p:
+        pass
+
+    with pytest.raises(pool.PoolClosed):
+        await p.wait()
+
+
 @pytest.mark.slow
 async def test_setup_no_timeout(dsn, proxy):
     with pytest.raises(pool.PoolTimeout):
@@ -725,7 +732,7 @@ async def test_reopen(dsn):
     await p.close()
     assert p._sched_runner is None
 
-    with pytest.raises(OperationalError, match="cannot be reused"):
+    with pytest.raises(psycopg.OperationalError, match="cannot be reused"):
         await p.open()