From: Daniele Varrazzo Date: Thu, 3 Nov 2022 17:15:07 +0000 (+0100) Subject: fix(pool): make sure to throw a RuntimeError opening async pool without a loop X-Git-Tag: pool-3.1.4~1^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=233cb4eef2a199c392d564e3df3ca7747735588a;p=thirdparty%2Fpsycopg.git fix(pool): make sure to throw a RuntimeError opening async pool without a loop 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. --- diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index b7bb52a58..d411d8bc0 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -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. diff --git a/tests/pool/test_pool_async_noasyncio.py b/tests/pool/test_pool_async_noasyncio.py index 56cd50b81..0f0813716 100644 --- a/tests/pool/test_pool_async_noasyncio.py +++ b/tests/pool/test_pool_async_noasyncio.py @@ -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)