Skip to content

OSError: [Errno 9] Bad file descriptor with uvloop #10506

Open

Description

@tkukushkin

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 descriptor

Python Version

3.13.1

aiohttp Version

3.11.13

multidict Version

6.1.0

propcache Version

0.3.0

yarl Version

1.18.3

OS

Debian 12.9

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Activity

  1. mklokocka commented on Mar 3, 2025

    @mklokocka

    Also having trouble with file descriptors on the latest versions of aiohttp and aiohappyeyeballs. Not using uvloop.

    BlockingIOError: [Errno 115] Operation now in progress
      File "asyncio/selector_events.py", line 509, in _sock_connect
        sock.connect(address)
    
    RuntimeError: File descriptor 265 is used by transport <_SelectorSocketTransport fd=265 read=polling write=<idle, bufsize=0>>
      File "usr/local/lib/python3.10/site-packages/<internal library>.py", line 53, in mpub_asyncio
        async with self.session_asyncio.post(f"{self.url}/mpub", timeout=timeout, **kwargs) as r:
      File "aiohttp/client.py", line 1425, in __aenter__
        self._resp: _RetType = await self._coro
      File "ddtrace/contrib/trace_utils_async.py", line 35, in wrapper
        return await func(mod, pin, wrapped, instance, args, kwargs)
      File "ddtrace/contrib/aiohttp/patch.py", line 108, in _traced_clientsession_request
        resp = await func(*args, **kwargs)  # type: aiohttp.ClientResponse
      File "aiohttp/client.py", line 703, in _request
        conn = await self._connector.connect(
      File "ddtrace/contrib/aiohttp/patch.py", line 62, in connect
        result = await self.__wrapped__.connect(req, *args, **kwargs)
      File "aiohttp/connector.py", line 548, in connect
        proto = await self._create_connection(req, traces, timeout)
      File "aiohttp/connector.py", line 1056, in _create_connection
        _, proto = await self._create_direct_connection(req, traces, timeout)
      File "aiohttp/connector.py", line 1380, in _create_direct_connection
        transp, proto = await self._wrap_create_connection(
      File "aiohttp/connector.py", line 1116, in _wrap_create_connection
        sock = await aiohappyeyeballs.start_connection(
      File "aiohappyeyeballs/impl.py", line 93, in start_connection
        raise first_exception
      File "aiohappyeyeballs/impl.py", line 71, in start_connection
        sock = await _connect_sock(
      File "aiohappyeyeballs/impl.py", line 163, in _connect_sock
        await loop.sock_connect(sock, address)
      File "asyncio/selector_events.py", line 499, in sock_connect
        self._sock_connect(fut, sock, address)
      File "asyncio/selector_events.py", line 515, in _sock_connect
        self._ensure_fd_no_transport(fd)
      File "asyncio/selector_events.py", line 248, in _ensure_fd_no_transport
        raise RuntimeError(
  2. bdraco commented on Mar 3, 2025

    @bdraco
    Member

    @mklokocka Are you using uvloop as well?

  3. tkukushkin commented on Mar 3, 2025

    @tkukushkin
    Author

    Not using uvloop.

  4. bdraco commented on Mar 3, 2025

    @bdraco
    Member

    Not using uvloop.

    The trace back you posted has uvloop in it.

  5. tkukushkin commented on Mar 3, 2025

    @tkukushkin
    Author

    I'm talking about @mklokocka comment

  6. bdraco commented on Mar 3, 2025

    @bdraco
    Member

    @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 aiohappyeyeballs staggered 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#L68

    Which version of aiohappyeyeballs do you have installed?

  7. bdraco commented on Mar 4, 2025

    @bdraco
    Member

    Please update to aiohappyeyeballs 2.4.8 and check if the problem goes away

  8. mklokocka commented on Mar 4, 2025

    @mklokocka

    @bdraco Thanks, the project ran for a few hours with the latest aiohappyeyeballs without any issues.

  9. bdraco commented on Mar 4, 2025

    @bdraco
    Member

    Thanks. If the problem reoccurs, let me know and we can reopen

  10. tkukushkin commented on Mar 12, 2025

    @tkukushkin
    Author

    @bdraco Hello! We've updated aiohappyeyeballs to 2.6.1 and it haven't helped with errors from my original message.

  11. reopened this on Mar 12, 2025
  12. bdraco commented on Mar 12, 2025

    @bdraco
    Member

    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

    The amount of time in seconds to wait for a connection attempt to complete, before starting the next attempt in parallel. This is the “Connection Attempt Delay” as defined in RFC 8305. To disable Happy Eyeballs, set this to None. The default value recommended by the RFC is 0.25 (250 milliseconds).

  13. 32 remaining items

  14. bdraco commented on Apr 19, 2025

    @bdraco
    Member

    Looks like the uvloop PR might be moving along. 🤞

  15. tkukushkin commented on Apr 21, 2025

    @tkukushkin
    Author

    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 localhost resolves to IPv6 and IPv4 addresses. IPv6 address leads to an error:

    aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host localhost:8000 ssl:default [Connect call failed ('::1', 8000, 0, 0)]
    
  16. added a commit that references this issue on Apr 21, 2025
  17. tkukushkin commented on Sep 24, 2025

    @tkukushkin
    Author

    @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_connection directly as you mentioned here)?

  18. bdraco commented on Sep 24, 2025

    @bdraco
    Member

    @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

  19. pakal commented on Mar 4, 2026

    @pakal

    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.

  20. jperezrealini commented on Mar 4, 2026

    @jperezrealini

    I replaced uvloop with good ol' asyncio.run on python 3.12 and it's working as a charm, with no performance impact.

  21. pakal commented on Mar 5, 2026

    @pakal

    We did a quick benchmark without uvloop, alas we lost something like 10% max RPM, so it's not playable for now :(

  22. Dreamsorcerer commented on Mar 5, 2026

    @Dreamsorcerer
    Member

    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.

  23. pakal commented on Mar 9, 2026

    @pakal

    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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions