Repository navigation
TEST: Close Windows event-loop wakeup sockets abortively - #4808
Richard Lundeen (richlundeen) wants to merge 1 commit into
Conversation
Windows event loops connect their private wakeup socket pair over loopback TCP, so a graceful close leaves one TIME_WAIT entry per loop. The previous unit-test loop factory covered only pytest-asyncio runners; TestClient and anyio portals, asyncio.run, direct loop constructors, and loops created by product code still accumulated TIME_WAIT and could exhaust ephemeral ports (WinError 10055) during the full suite. A single win32-only session fixture now sets abortive linger on the wakeup pair when any loop creates it, so never-closed loops are covered too. The event-loop policy, loop type, application sockets, and per-test loop isolation are unchanged. The internal _asyncio_loop_factory override and the test loop subclass are removed so only one hook applies. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The decorator accesses Windows-only asyncio.ProactorEventLoop during import, causing an AttributeError that aborts unit-test collection on Linux and macOS before the skip condition applies.
1 open issue
What changed in this PR
This PR broadens Windows unit-test event-loop cleanup to prevent ephemeral port exhaustion without changing application socket behavior.
Changes:
- Replace the custom loop factory with a session-wide abortive-close hook.
- Expand regression coverage across loop creation paths.
- Document the hook and its lifecycle guarantees.
| File | Description |
|---|---|
| tests/unit/test_event_loop.py | Adds loop-source, socket-isolation, and TIME_WAIT checks. |
| tests/unit/conftest.py | Installs the Windows-only session hook. |
| test_support/event_loop.py | Configures abortive close on private wakeup sockets. |
| doc/contributing/5_unit_tests.md | Explains the broader cleanup mechanism. |
Review effort: Balanced
| [ | ||
| pytest.param(_asyncio_run, id="asyncio_run"), | ||
| pytest.param(_closed_loop_source(asyncio.new_event_loop), id="new_event_loop"), | ||
| pytest.param(_closed_loop_source(asyncio.ProactorEventLoop), id="proactor_constructor"), |
| [ | ||
| pytest.param(_asyncio_run, id="asyncio_run"), | ||
| pytest.param(_closed_loop_source(asyncio.new_event_loop), id="new_event_loop"), | ||
| pytest.param(_closed_loop_source(asyncio.ProactorEventLoop), id="proactor_constructor"), |
There was a problem hiding this comment.
This decorator argument is evaluated at module import, before the module-level skipif applies, and asyncio.ProactorEventLoop only exists on Windows — so collection aborts with AttributeError on Linux/macOS. All 20 ubuntu/macos main-job runs on this PR fail with exactly this error (run 46922435372). Resolving the constructor lazily fixes it, e.g. parametrize with attribute-name strings and getattr(asyncio, name) inside the test, or wrap the factory in a lambda evaluated at call time.

Description
Prevent Windows unit-test event loops from exhausting the ephemeral loopback port range and failing with
WinError 10055after many TestClient, asyncio.run, and pytest-asyncio loop creations.