Skip to content

TEST: Close Windows event-loop wakeup sockets abortively - #4808

Open
Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:test-windows-event-loop-wakeup-sockets
Open

Richard Lundeen (richlundeen) wants to merge 1 commit into
microsoft:mainfrom
richlundeen:test-windows-event-loop-wakeup-sockets

Conversation

@richlundeen

Copy link
Copy Markdown
Contributor

Description

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

  • Apply abortive close to the private Proactor/Selector wakeup socket pair for every unit-test event loop through a Windows-only autouse fixture.
  • Remove the narrower event-loop subclass and policy override while preserving pytest-asyncio and anyio lifecycle behavior.
  • Add socket attribution and TIME_WAIT regression coverage, plus contributor guidance for the session-wide hook.

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>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 16:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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"),

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants