Skip to content

test.test_asyncio.test_base_events failing on windows due to non-integer fileno() return #151540

Closed

Description

@neek78

Bug report

Bug description:

test.test_asyncio.test_base_events is calling a real system function with a mocked object, which is causing failures. It seems the test case test_create_connection_happy_eyeballs_ipv4_only is failing.

nick@COMPY D:\src\cpython-trunk>pcbuild\amd64\python.exe -m test
== CPython 3.16.0a0 (heads/main:11f032f904c, Jun 16 2026, 12:13:34) [MSC v.1951 64 bit (AMD64)]
== Windows-11-10.0.26200-SP0 little-endian
== Python build: release with_assert
== cwd: D:\src\cpython-trunk\build\test_python_worker_17156æ
== CPU count: 32
== encodings: locale=cp1252 FS=utf-8
== resources: all test resources are disabled, use -u option to unskip tests

Using random seed: 1122715071
0:00:00 mem: 30.7 MiB Run 505 tests sequentially in a single process
0:00:00 mem: 30.7 MiB [  1/505] test.test_asyncio.test_base_events
Exception in callback _ProactorReadPipeTransport._loop_reading()
handle: <Handle _ProactorReadPipeTransport._loop_reading()>
Traceback (most recent call last):
  File "D:\src\cpython-trunk\Lib\asyncio\events.py", line 94, in _run
    self._context.run(self._callback, *self._args)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\src\cpython-trunk\Lib\asyncio\proactor_events.py", line 306, in _loop_reading
    self._read_fut = self._loop._proactor.recv_into(self._sock, self._data)
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\src\cpython-trunk\Lib\asyncio\windows_events.py", line 497, in recv_into
    self._register_with_iocp(conn)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "D:\src\cpython-trunk\Lib\asyncio\windows_events.py", line 713, in _register_with_iocp
    _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: an integer is required

The TypeError is coming from the return value from obj.fileno() which is returning a <class 'unittest.mock.MagicMock'>.

Tracing the source of that object, it seems it's being created in Lib/test/test_asyncio/utils.py - mock_nonblocking_socket() which doesn't have anything setup to handle fileno() calls. At this point I'm not sure what would be correct to return here as _register_with_iocp() is calling a real system function which would expect a real handle.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Activity

  1. added
    type-bug
    on Jun 16, 2026
  2. sharktide commented on Jun 16, 2026

    @sharktide
    Contributor

    afaik the correct behavior is that the mocked socket must provide a real, integer-valued fileno(), because the Windows Proactor event loop always calls into an actual OS API (CreateIoCompletionPort) that requires a real Windows handle. A MagicMock object cannot satisfy that contract, so the test shouldn't allow a mock socket to even get to that code path.

    test_create_connection_happy_eyeballs_ipv4_only uses mock_nonblocking_socket() from test_asyncio/utils.py.
    That helper returns a MagicMock that pretends to be a socket but does not define fileno().

    On Windows, the asyncio Proactor loop eventually calls:

    _overlapped.CreateIoCompletionPort(obj.fileno(), ...)
    

    But since obj.fileno() returns a MagicMock, CPython raises:

    TypeError: an integer is required
    

    This is technically correct since the OS API must receive a real integer handle... but how should we patch this?

  3. added
    tests
    on Jun 16, 2026
  4. added a commit that references this issue on Jul 11, 2026
  5. moved this from Todo to Done in asyncioon Jul 11, 2026
  6. added a commit that references this issue on Jul 25, 2026
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

    OS-windowsteststopic-asynciotype-bug

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions