You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RuntimeError: File descriptor 2877 is used by transport <TCPTransport closed=False reading=True 0x55b8dc9baa90> #653
In our service we had aiohttp==3.8.5 and upgraded aiohttp to 3.10.5 version. That is all. In production now we have issues like issue below.
Maybe i should to write issue to another libs, tell me about it if so, or maybe you have your own fix then tell me uvloop version i need to upgrade in my service.
traceback:
RuntimeError: File descriptor 2877 is used by transport <TCPTransport closed=False reading=True 0x55b8dc9baa90>
File "our_service/app/client.py", line 204, in get_recommendations
async with self._session.get(
File "aiohttp/client.py", line 1353, in __aenter__
self._resp = await self._coro
File "aiohttp/client.py", line 657, in _request
conn = await self._connector.connect(
File "aiohttp/connector.py", line 564, in connect
proto = await self._create_connection(req, traces, timeout)
File "aiohttp/connector.py", line 975, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "aiohttp/connector.py", line 1319, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "aiohttp/connector.py", line 1073, in _wrap_create_connection
sock = await aiohappyeyeballs.start_connection(
File "aiohappyeyeballs/impl.py", line 104, in start_connection
raise first_exception
File "aiohappyeyeballs/impl.py", line 82, in start_connection
sock = await _connect_sock(
File "aiohappyeyeballs/impl.py", line 174, in _connect_sock
await loop.sock_connect(sock, address)
File "uvloop/loop.pyx", line 2631, in sock_connect
fut = self._sock_connect(sock, address)
File "uvloop/loop.pyx", line 1089, in uvloop.loop.Loop._sock_connect
self._add_writer(sock, handle)
File "uvloop/loop.pyx", line 837, in uvloop.loop.Loop._add_writer
self._ensure_fd_no_transport(fd)
File "uvloop/loop.pyx", line 764, in uvloop.loop.Loop._ensure_fd_no_transport
raise RuntimeError(
I have also encountered this issue when upgrading from aiohttp 3.9.5 -> 3.11.11.
I was able to reproduce with this test script (gist) on Ubuntu 24 x86.
After some experimentation I found that aio-libs/aiohttp#10464 resolved the issue for my test script... however it does feel like there's something funny on uvloop's side here, in that when an exception is thrown on this line:
We hit this in production (uvicorn + aiohttp client, uvloop 0.22.1, aiohttp 3.14.5, aiohappyeyeballs 2.6.1, Python 3.11) and can confirm it is the create_connection(sock=...) cancellation double-close described in #738, which #740 fixed in 0.23.0.
Mechanism we confirmed: aiohttp connects the socket via aiohappyeyeballs, then calls loop.create_connection(sock=sock). If the task is cancelled while parked on await waiter (any wait_for / client timeout landing in that window), 0.22.1 calls tr._close() before _attach_fileobj(sock), so libuv closes the fd but the Python socket keeps the fd number. The OS reuses the number for a new connection, the stale socket is garbage-collected and closes it again, and from then on every new socket that gets that number fails with File descriptor N is used by transport <TCPTransport closed=False reading=True ...>. A blocked event loop makes the window much easier to hit.
Minimal repro:
importasyncio, socket, gc, uvloopclassP(asyncio.Protocol): passasyncdefmain():
loop=asyncio.get_running_loop()
srv=awaitloop.create_server(P, "127.0.0.1", 0)
port=srv.sockets[0].getsockname()[1]
sock=socket.socket(); sock.setblocking(False)
awaitloop.sock_connect(sock, ("127.0.0.1", port))
t=loop.create_task(loop.create_connection(P, sock=sock))
awaitasyncio.sleep(0) # transport opened, task parked on `await waiter`t.cancel()
try: awaittexceptasyncio.CancelledError: passprint("stale sock fd:", sock.fileno()) # 0.22.1: still a number; 0.23.0: -1tr2, _=awaitloop.create_connection(P, "127.0.0.1", port) # reuses the fd numberdelt, sock; gc.collect() # stale socket closes tr2's fds3=socket.socket(); s3.setblocking(False)
awaitloop.sock_connect(s3, ("127.0.0.1", port)) # 0.22.1: RuntimeError ... is used by transportuvloop.run(main())
Results (same machine, same aiohttp/aiohappyeyeballs; a stress script with many aiohttp requests under short wait_for timeouts while the loop is periodically blocked):
uvloop
minimal repro
stress run
0.22.1
RuntimeError: File descriptor 12 is used by transport ...
5-7 errors within seconds
0.23.0
socket detached, no error
0 errors in ~190k requests (2 runs)
stock asyncio loop
n/a
0 errors in ~135k requests (2 runs)
So for anyone landing here: upgrading to uvloop >= 0.23.0 resolves the cancellation path, and this issue can probably be closed as a duplicate of #738. Note that #785 describes a second, still-open path to the same double-close (connection reset before the TLS handshake, Linux only), which 0.23.0 does not cover.
(Investigation and repro done with AI assistance; the outputs above are from actual runs.)
Activity
top-oai commentedon Feb 14, 2025
I have also encountered this issue when upgrading from aiohttp 3.9.5 -> 3.11.11.
I was able to reproduce with this test script (gist) on Ubuntu 24 x86.
After some experimentation I found that aio-libs/aiohttp#10464 resolved the issue for my test script... however it does feel like there's something funny on uvloop's side here, in that when an exception is thrown on this line:
uvloop/uvloop/loop.pyx
Line 2066 in 7bb12a1
then it is possible for the transport to linger on in
uvloop/uvloop/loop.pyx
Line 150 in 7bb12a1
despite the file descriptor being relinquished to the OS.
top-oai commentedon Feb 16, 2025
Also, it looks like #646 would be a uvloop-side fix for this issue.
azisso commentedon Oct 28, 2026
We hit this in production (uvicorn + aiohttp client, uvloop 0.22.1, aiohttp 3.14.5, aiohappyeyeballs 2.6.1, Python 3.11) and can confirm it is the
create_connection(sock=...)cancellation double-close described in #738, which #740 fixed in 0.23.0.Mechanism we confirmed: aiohttp connects the socket via aiohappyeyeballs, then calls
loop.create_connection(sock=sock). If the task is cancelled while parked onawait waiter(anywait_for/ client timeout landing in that window), 0.22.1 callstr._close()before_attach_fileobj(sock), so libuv closes the fd but the Python socket keeps the fd number. The OS reuses the number for a new connection, the stale socket is garbage-collected and closes it again, and from then on every new socket that gets that number fails withFile descriptor N is used by transport <TCPTransport closed=False reading=True ...>. A blocked event loop makes the window much easier to hit.Minimal repro:
Results (same machine, same aiohttp/aiohappyeyeballs; a stress script with many aiohttp requests under short
wait_fortimeouts while the loop is periodically blocked):RuntimeError: File descriptor 12 is used by transport ...So for anyone landing here: upgrading to uvloop >= 0.23.0 resolves the cancellation path, and this issue can probably be closed as a duplicate of #738. Note that #785 describes a second, still-open path to the same double-close (connection reset before the TLS handshake, Linux only), which 0.23.0 does not cover.
(Investigation and repro done with AI assistance; the outputs above are from actual runs.)