Repository navigation
Fix infinite CPU spin in ProcessInteropMessages on synchronous EOF - #41205
Mathias Hoeg Boisen (Bojje) wants to merge 2 commits into
Conversation
When ReadFile completes synchronously with zero bytes (socket EOF), the loop had no exit condition and spun indefinitely. The async path already handled this case correctly via a BytesRead == 0 check after GetOverlappedResult, but the synchronous completion path fell through to the next iteration unconditionally. Add a synchronous success handler that checks for EOF and processes the window size message, mirroring the existing async path logic. Fixes microsoft#41173 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Fixes an infinite CPU spin in the WSL interop control-message loop (ProcessInteropMessages) when the control socket reaches EOF and ReadFile completes synchronously with BytesRead == 0. This prevents leaked “Interop” threads from consuming 100% CPU indefinitely on certain shutdown races in WSL2 interop sessions.
Changes:
- Add a synchronous-success branch for
ReadFileto handleBytesRead == 0(EOF) and to process window-size messages immediately. - Preserve the existing async (
ERROR_IO_PENDING+WaitForMultipleObjects+GetOverlappedResult) behavior while aligning sync-path handling with it.
Comments suppressed due to low confidence (1)
src/windows/common/interop.cpp:482
- Same as the synchronous path: relying on
WI_ASSERThere means partial reads or unexpected message types can slip through in release builds. Convert this to a runtime validation so malformed/short messages don't get interpreted as a resize request.
WI_ASSERT((BytesRead == sizeof(WindowSizeMessage)) && (WindowSizeMessage.Header.MessageType == LxInitMessageWindowSizeChanged));
const COORD Size{static_cast<SHORT>(WindowSizeMessage.Columns), static_cast<SHORT>(WindowSizeMessage.Rows)};
THROW_IF_FAILED(ResizePseudoConsole(Result->PseudoConsole.get(), Size));
| WI_ASSERT((BytesRead == sizeof(WindowSizeMessage)) && (WindowSizeMessage.Header.MessageType == LxInitMessageWindowSizeChanged)); | ||
|
|
||
| auto CancelIo = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] { | ||
| CancelIoEx(MessageHandle, &Overlapped); | ||
| GetOverlappedResult(MessageHandle, &Overlapped, &BytesRead, TRUE); | ||
| }); | ||
| const COORD Size{static_cast<SHORT>(WindowSizeMessage.Columns), static_cast<SHORT>(WindowSizeMessage.Rows)}; | ||
| THROW_IF_FAILED(ResizePseudoConsole(Result->PseudoConsole.get(), Size)); | ||
| continue; |
There was a problem hiding this comment.
The WI_ASSERT on this line (and the identical one on line 479 in the async path) is pre-existing — both were carried over from the original code. Changing them to runtime checks would be a behavioral change beyond the scope of this bug fix. Happy to address it in a follow-up if the maintainers want.
Keep the original !Success block body unchanged — just wrap it in an else branch. The synchronous success handler is a pure addition. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
@microsoft-github-policy-service agree |
Mathias Hoeg Boisen (Bojje)
left a comment
There was a problem hiding this comment.
The WI_ASSERT on this line (and the identical one on line 479 in the async path) is pre-existing — both were carried over from the original code. Changing them to runtime checks would be a behavioral change beyond the scope of this bug fix. Happy to address it in a follow-up if the maintainers want.
|
Closing — pushed without testing. Apologies for the noise. |
Summary
ReadFileinProcessInteropMessagescompletes synchronously with zero bytes (socket EOF), the loop had no exit condition and spun at 100% CPU indefinitely.GetOverlappedResult) already handled this correctly with aBytesRead == 0check, but the synchronous path fell through to the next loop iteration unconditionally.Notes
The fix keeps the sync/async result handling separate to match the codebase's existing style (see
ReadHandle::Schedule/CollectinHandleIO.cpp). A future improvement could refactorProcessInteropMessagesto use theMultiHandleWait+ReadHandleinfrastructure, which would eliminate the duplication structurally — but that's a larger change better suited for a separate PR.Test plan
Unable to build and test locally (Linux/WSL2 environment, no Windows build toolchain). The change is a pure addition of a
BytesRead == 0check on the synchronousReadFilesuccess path, mirroring the identical check already present on the async path. Requesting CI validation and maintainer review.Fixes #41173
🤖 Generated with Claude Code