Repository navigation
Open
Labels
Description
Note
we are waiting for MagicStack/uvloop#646 to get fixed upstream
Describe the bug
Hello! After updating aiohttp to 3.11.13 we see new errors.
To Reproduce
I don't know how to reproduce these errors, but it seems they are somehow connected with cancellation.
Expected behavior
No errors
Logs/tracebacks
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1123, in _wrap_create_connection
connection = await self._loop.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "uvloop/loop.pyx", line 2076, in create_connection
File "uvloop/loop.pyx", line 2066, in uvloop.loop.Loop.create_connection
asyncio.exceptions.CancelledError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/cian_http/client.py", line 159, in request
async with self._get_session().request(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 1425, in __aenter__
self._resp: _RetType = await self._coro
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 703, in _request
conn = await self._connector.connect(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 548, in connect
proto = await self._create_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1056, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1380, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/connector.py", line 1141, in _wrap_create_connection
sock.close()
File "/usr/local/lib/python3.12/socket.py", line 505, in close
self._real_close()
File "/usr/local/lib/python3.12/socket.py", line 499, in _real_close
_ss.close(self)
OSError: [Errno 9] Bad file descriptorPython Version
3.13.1aiohttp Version
3.11.13multidict Version
6.1.0propcache Version
0.3.0yarl Version
1.18.3OS
Debian 12.9
Related component
Client
Additional context
No response
Code of Conduct
- I agree to follow the aio-libs Code of Conduct
Activity
mklokocka commentedon Mar 3, 2025
Also having trouble with file descriptors on the latest versions of
aiohttpandaiohappyeyeballs. Not usinguvloop.bdraco commentedon Mar 3, 2025
@mklokocka Are you using uvloop as well?
tkukushkin commentedon Mar 3, 2025
bdraco commentedon Mar 3, 2025
The trace back you posted has uvloop in it.
tkukushkin commentedon Mar 3, 2025
I'm talking about @mklokocka comment
bdraco commentedon Mar 3, 2025
@mklokocka Does the problem still happen if you disable the patching of the internals in https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/contrib/internal/aiohttp/patch.py ?
The previous issues thatw ere reported focused on the
aiohappyeyeballsstaggered race, however the trace you have posted looks like its using the non-staggered race path https://github.com/aio-libs/aiohappyeyeballs/blob/035d976dee1f5e731852649f3fffd4e1aca21825/src/aiohappyeyeballs/impl.py#L68Which version of
aiohappyeyeballsdo you have installed?fix: update staggered race implementation to use latest cpython version
bdraco commentedon Mar 4, 2025
Please update to
aiohappyeyeballs2.4.8 and check if the problem goes awaymklokocka commentedon Mar 4, 2025
@bdraco Thanks, the project ran for a few hours with the latest
aiohappyeyeballswithout any issues.bdraco commentedon Mar 4, 2025
Thanks. If the problem reoccurs, let me know and we can reopen
tkukushkin commentedon Mar 12, 2025
@bdraco Hello! We've updated aiohappyeyeballs to 2.6.1 and it haven't helped with errors from my original message.
bdraco commentedon Mar 12, 2025
I've dug though the aiohappyeyeballs code again, and can find no place where we can work around this. so for uvloop, we are waiting for MagicStack/uvloop#646 to get fixed upstream
In the mean time you might be able to get away with disabling happyeyeballs on the connector if you don't have any use cases where you have non working IPv6
https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.TCPConnector
32 remaining items
bdraco commentedon Apr 19, 2025
Looks like the uvloop PR might be moving along. 🤞
tkukushkin commentedon Apr 21, 2025
I hope so.
By the way, our monkey patched version of aiohttp still works great in production.
The only problem that I met, there is something wrong with IPv6 addresses.
We don't have IPv6 in production, but we use aiohttp for tests too, and
localhostresolves to IPv6 and IPv4 addresses. IPv6 address leads to an error:chore: pin aiohttp (#9)
tkukushkin commentedon Sep 24, 2025
@bdraco Hi! It's been 5 months since we monkey-patched aiohttp and we haven't had any problems like "Bad file descriptor" since then. What do you think about adding some workaround to aiohttp (like using
loop.create_connectiondirectly as you mentioned here)?Workaround uvloop losing track of sockets when passing a socket to cr…
Workaround uvloop losing track of sockets when passing a socket to cr…
bdraco commentedon Sep 24, 2025
@tkukushkin #11539 is a better version of the workaround I previously posted. It correctly pops the next address on retry as otherwise it will skip trying some addresses when using dual ipv6/ipv4
pakal commentedon Mar 4, 2026
We also have tons of such errors like "File descriptor 97 is used by transport <TCPTransport closed=False reading=True 0x5e75635f2120>'", or "ClientOSError(9, 'Bad file descriptor')", when using aiohttp (and uvloop) under some traffic load, on uvicorn.
jperezrealini commentedon Mar 4, 2026
I replaced
uvloopwith good ol'asyncio.runon python 3.12 and it's working as a charm, with no performance impact.pakal commentedon Mar 5, 2026
We did a quick benchmark without uvloop, alas we lost something like 10% max RPM, so it's not playable for now :(
Dreamsorcerer commentedon Mar 5, 2026
Unfortunately, if uvloop is not being maintained and is known to be broken, we can't recommend or support it. We tried workarounds and that caused problems for far more users than it helped. I would suggest taking the small performance penalty until someone is maintaining uvloop again.
pakal commentedon Mar 9, 2026
Update: we updated the codebase to properly cancel() some subtasks (making aiohttp requests) that were left running when the main request-handling Task was cancelled, and it magically made the file descriptor errors disappear...
So if anybody else encounters the issue, it seems that uvloop + improper resource cleanup (del() etc.) might be the culprit, in our case at least.
Erratum: these errors are back, the search continues...
Workaround uvloop losing track of sockets when passing a socket to cr…
Workaround uvloop losing track of sockets when passing a socket to cr…
Add data leak regression test and fix server handler abort