Skip to content

Fix infinite CPU spin in ProcessInteropMessages on synchronous EOF - #41205

Closed
Mathias Hoeg Boisen (Bojje) wants to merge 2 commits into
microsoft:masterfrom
Bojje:fix/interop-sync-read-spin
Closed

Mathias Hoeg Boisen (Bojje) wants to merge 2 commits into
microsoft:masterfrom
Bojje:fix/interop-sync-read-spin

Conversation

@Bojje

@Bojje Mathias Hoeg Boisen (Bojje) commented Jul 29, 2026 •

Copy link
Copy Markdown

Summary

  • When ReadFile in ProcessInteropMessages completes synchronously with zero bytes (socket EOF), the loop had no exit condition and spun at 100% CPU indefinitely.
  • The async completion path (via GetOverlappedResult) already handled this correctly with a BytesRead == 0 check, but the synchronous path fell through to the next loop iteration unconditionally.
  • Added a synchronous success handler that checks for EOF and processes the window size message, mirroring the existing async path.

Notes

The fix keeps the sync/async result handling separate to match the codebase's existing style (see ReadHandle::Schedule/Collect in HandleIO.cpp). A future improvement could refactor ProcessInteropMessages to use the MultiHandleWait + ReadHandle infrastructure, 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 == 0 check on the synchronous ReadFile success path, mirroring the identical check already present on the async path. Requesting CI validation and maintainer review.

Fixes #41173

🤖 Generated with Claude Code

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]>
Copilot AI review requested due to automatic review settings July 29, 2026 15:26
@Bojje
Mathias Hoeg Boisen (Bojje) requested a review from a team as a code owner July 29, 2026 15:26
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

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 ReadFile to handle BytesRead == 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_ASSERT here 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));

Comment thread src/windows/common/interop.cpp Outdated
Comment on lines +439 to +443
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;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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]>
Copilot AI review requested due to automatic review settings July 29, 2026 15:34
@Bojje

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

@Bojje

Copy link
Copy Markdown
Author

Closing — pushed without testing. Apologies for the noise.

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

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.

ProcessInteropMessages spins at 100% CPU forever when an interop control socket reaches EOF

2 participants