Repository navigation
TEST: Close Windows event-loop wakeup sockets abortively - #4808
Open
Richard Lundeen (richlundeen) wants to merge 1 commit into
Open
Richard Lundeen (richlundeen) wants to merge 1 commit into
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>
Contributor
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"), |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.