]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(pool): make sure to throw a RuntimeError opening async pool without a loop
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 3 Nov 2022 17:15:07 +0000 (18:15 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 4 Nov 2022 16:31:55 +0000 (17:31 +0100)
The error would be thrown anyway downstream, when tasks are created, but
other spurious warning might be raised too because certain tasks are not
awaited.

psycopg_pool/psycopg_pool/pool_async.py
tests/pool/test_pool_async_noasyncio.py

index b7bb52a58fe7bfcfb159eb7179cb92bc0036c236..d411d8bc091a5f783925fe33aaf7bd2273338787 100644 (file)
@@ -204,6 +204,9 @@ class AsyncConnectionPool(BasePool[AsyncConnection[Any]]):
         if not self._closed:
             return
 
+        # Throw a RuntimeError if the pool is open outside a running loop.
+        asyncio.get_running_loop()
+
         self._check_open()
 
         # Create these objects now to attach them to the right loop.
index 56cd50b81f0663261a9d0011e09ca61582797f7f..0f0813716b18a953074c6a8819d1a0c8900163e2 100644 (file)
@@ -51,3 +51,8 @@ def test_working_created_before_loop(dsn):
             await p.close()
 
     asyncio.run(asyncio.wait_for(test(), timeout=2.0))
+
+
+def test_cant_create_open_outside_loop(dsn):
+    with pytest.raises(RuntimeError):
+        pool.AsyncConnectionPool(dsn, open=True)