MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.91k stars 400 forks source link

getaddrinfo() error / exception not re-raised? #825

Open bsense-rius opened 3 years ago

bsense-rius commented 3 years ago

Potential slight bug? I have an asyncio loop with a task that sequentially pops queries from an asyncio queue to execute, one at a time, in a remote timescaleDB (postgresql) database. Everything works like a charm. And I'd rather say quite performant!

But, whenever I unplug the ethernet cable of query requester (to simulate sudden connection losses), an uncatched exception error pops up related to the impossibility to resolve the name address of the DSN : 2021-09-10 10:10:44.846 ERROR: Future exception was never retrieved : future: <Future finished exception=gaierror(-3, 'Temporary failure in name resolution')> : Traceback (most recent call last): : File "/home/venv/lib/python3.8/site-packages/asyncpg/connection.py", line 1393, in _cancel : await connect_utils._cancel( : File "/home/venv/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 696, in _cancel : tr, pr = await _create_ssl_connection( : File "/home/venv/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 544, in _create_ssl_connection : tr, pr = await loop.create_connection( : File "/usr/lib/python3.8/asyncio/base_events.py", line 986, in create_connection : infos = await self._ensure_resolved( : File "/usr/lib/python3.8/asyncio/base_events.py", line 1365, in _ensure_resolved : return await loop.getaddrinfo(host, port, family=family, type=type, : File "/usr/lib/python3.8/asyncio/base_events.py", line 825, in getaddrinfo : return await self.run_in_executor( : File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run : result = self.fn(*self.args, **self.kwargs) : File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo : for res in _socket.getaddrinfo(host, port, family, type, proto, flags): : socket.gaierror: [Errno -3] Temporary failure in name resolution

I have the execute() code within a try: / exception: block. Amongst the several exceptions i trap i have the generic exception Exception. But this is of no use because this error is not re raised and keeps within the asyncpg library.

Shouldn't it be re raised so that the app deals with that issue?

Thank you for this fantastic library!

elprans commented 2 years ago

This is one of the unfortunate aspects of Python asyncio. There is no way to surface exceptions in background tasks (which _cancel is) other than logging them.