]> 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:10:33 +0000 (23:10 +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 f7b760a9ee974f1b83e3fa8287988ec2433afab1..7cd5dccb89712e6336f6029eda5675e10cbb89f5 100644 (file)
@@ -15,6 +15,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 70d9e7d67557f6f492320f597583816c0364cabe..00e76dae50aee1bd1a9d4550622793e402735668 100644 (file)
@@ -107,6 +107,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 787a0e7e33c1283483ccb09915f7dcdc8b616610..81cf6ab962adf9c849446be8748e21fe5d1abcf5 100644 (file)
@@ -81,6 +81,8 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]):
         self.run_task(Schedule(self, ShrinkPool(self), self.max_idle))
 
     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 e33ad394e37c4ade9d465ba1049a0fd31719ff87..257d0e6a541cfd222c8357f03ad0d212ade29c09 100644 (file)
@@ -144,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):
index 2f6d1583cfa6021887a51e898d4a1696f1a92ec6..b94510cd4ed76c51e44498bc2cd52b3407309529 100644 (file)
@@ -145,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):