gh-156333: rebuild the proactor self-pipe on EOF instead of busy-looping - #156343
Open
aidaodedjl wants to merge 1 commit into
Open
gh-156333: rebuild the proactor self-pipe on EOF instead of busy-looping#156343aidaodedjl wants to merge 1 commit into
aidaodedjl wants to merge 1 commit into
Conversation
…y-looping When the self-pipe socketpair of a BaseProactorEventLoop reaches a clean EOF (e.g. the OS tears the loopback connection down across a power or session state change on Windows), _loop_self_reading re-armed recv() on the dead socket, which completed immediately and rescheduled the callback forever, pinning one core at 100% CPU with nothing logged. Detect the EOF via the empty recv result and rebuild the socketpair instead: allocate the replacement first (so a failure leaves the previous state untouched), re-register signal.set_wakeup_fd on the new socket before closing the old one (mirroring close()), then arm the next read on the new socket so cross-thread wakeups keep working.
aidaodedjl
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
August 25, 2026 05:00
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.
Summary
On Windows,
BaseProactorEventLoopwakes itself through a self-pipe — a loopback TCP socketpair created bysocket.socketpair(). If that connection reaches a clean EOF while the loop is running — for example the OS tears the idle loopback connection down across a power/session state change —_loop_self_readingre-armedrecv()on the dead socket, which completed immediately and rescheduled the callback forever: one core pinned at 100% CPU, no exception raised, nothing logged, the process never recovers. Reproduced deterministically on main (3.16.0a0, self-built): 346,702 re-arms of_loop_self_readingduring a 3-second idle sleep (a gracefulloop._csock.shutdown(socket.SHUT_WR)models the OS teardown).At EOF
f.result()returnsb'', which is not an exception, so control fell through to theelsebranch and armed a read that could never block again.Fix: when the recv result is empty, rebuild the socketpair instead of re-arming on the dead one:
signal.set_wakeup_fd()on the new socket before closing the old sockets, mirroring the ordering used byclose();call_soon_threadsafe) keep working after the rebuild.Verification (real Windows machine, self-built 3.16.0a0 from the commit this PR is based on)
test_asynciosuite: 35/35 files, 2,625 tests, 0 failures (both new tests included).main(git checkoutof the pristineproactor_events.py, tests re-run: mock test FAIL, functional test FAIL), then pass with the patch.Out of scope / follow-ups
ConnectionResetErrorfrom the pending recv instead of a clean EOF; that pre-existing path is not handled here.BaseSelectorEventLoop._read_from_self(used byWindowsSelectorEventLoop) has the same EOF busy-loop shape: measured 582,692_read_from_selfcalls during a 3s idle sleep on the same machine/reproducer shape. Will be reported separately.