aio-libs / aiopg

aiopg is a library for accessing a PostgreSQL database from the asyncio
http://aiopg.readthedocs.io
BSD 2-Clause "Simplified" License
1.4k stars 160 forks source link

Calling aiopg.createPool() a 2nd time if first failed results in: RuntimeError: unable to perform operation on <UVPoll closed=True 0x105fd4270>; the handler is closed #909

Open maxnet opened 4 months ago

maxnet commented 4 months ago

Describe the bug

If aiopg.create_pool() fails because the PostgreSQL server is down it gives a psycopg2.OperationalError "Error connecting to database: Connection refused" If you subsequently want to retry and call aiopg.create_pool() a second time it results in a "RuntimeError: unable to perform operation on <UVPoll closed=True 0x1046082e0>; the handler is closed" error.

To Reproduce

1) make sure your database server is down, or just point DSN to an IP that has no database server for testing purposes 2) use code among the lines of:

db_pool = False
dsn = "dbname=mydb user=myuser password=mypassword host=127.0.0.1"

async def init():
    global db_pool
    while True:
        try:
            db_pool = await aiopg.create_pool(dsn, maxsize=4, timeout=10, pool_recycle=300)
            break
        except psycopg2.OperationalError as ex:
            print("Error connecting to database: %s" % ex)
            print("Trying again in 5 seconds...")
            await asyncio.sleep(5)

Expected behavior

Expect it to retry connecting to the database server.

Logs/tracebacks

File "MYFILE.py", line 93, in init
    db_pool = await aiopg.create_pool(dsn, maxsize=4, timeout=10, pool_recycle=300)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/aiopg/pool.py", line 300, in from_pool_fill
    await self._fill_free_pool(False)
  File "/opt/homebrew/lib/python3.12/site-packages/aiopg/pool.py", line 336, in _fill_free_pool
    conn = await connect(
                 ^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/aiopg/connection.py", line 65, in connect
    connection = Connection(
                 ^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.12/site-packages/aiopg/connection.py", line 772, in __init__
    self._loop.add_reader(
  File "uvloop/loop.pyx", line 2441, in uvloop.loop.Loop.add_reader
  File "uvloop/loop.pyx", line 780, in uvloop.loop.Loop._add_reader
  File "uvloop/handles/poll.pyx", line 104, in uvloop.loop.UVPoll.start_reading
  File "uvloop/handles/poll.pyx", line 39, in uvloop.loop.UVPoll._poll_start
  File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
RuntimeError: unable to perform operation on <UVPoll closed=True 0x1046082e0>; the handler is closed

Python Version

Python 3.12.4

aiopg Version

Version: 1.4.0

OS

macOS

Additional context

No response

Code of Conduct