Migrate relay IO to MultiHandleWait infrastructure#40730
Draft
benhillis wants to merge 1 commit into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR migrates multiple service/common IO relay paths from bespoke overlapped IO loops to the shared wsl::windows::common::io infrastructure (MultiHandleWait + ReadHandle/RelayHandle/EventHandle). This consolidates relay behavior across the codebase and removes duplicated cancellation/wait logic.
Changes:
- Replaces manual relay/read loops in several components with
MultiHandleWait+ handle abstractions (relay, dmesg, guest telemetry logger). - Moves the standalone cancellable
InterruptableRead/Write/Waitimplementations intowsl::windows::common::ioand updates call sites accordingly. - Simplifies relay thread creation by collapsing multiple
CreateThreadoverloads into a single templated helper, and adds configurable buffer sizing forReadHandle/RelayHandle.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/windows/service/exe/LxssUserSession.cpp | Updates callers to use wsl::windows::common::io::InterruptableRead/Wait directly. |
| src/windows/service/exe/GuestTelemetryLogger.cpp | Replaces the manual overlapped read loop with MultiHandleWait + ReadHandle and exit EventHandles. |
| src/windows/common/relay.hpp | Collapses multiple CreateThread overloads into a single template wrapper around InterruptableRelay. |
| src/windows/common/relay.cpp | Rewrites relay helpers (InterruptableRelay, BidirectionalRelay, ScopedRelay::Run, ScopedMultiRelay::Run) to use MultiHandleWait + RelayHandle/ReadHandle. |
| src/windows/common/HandleIO.h | Adds buffer size configuration to ReadHandle and propagates it through RelayHandle; declares standalone interruptable IO helpers in io namespace. |
| src/windows/common/HandleIO.cpp | Implements standalone io::InterruptableRead/Write/Wait and updates ReadHandle to allocate its buffer based on the new size parameter. |
| src/windows/common/Dmesg.cpp | Replaces the manual interruptable read loop with MultiHandleWait + ReadHandle and uses io::InterruptableWrite for output/com1 writes. |
Comment on lines
+212
to
+213
| ReadHandle::ReadHandle(HandleWrapper&& MovedHandle, std::function<void(const gsl::span<char>& Buffer)>&& OnRead, size_t BufferSize) : | ||
| Handle(std::move(MovedHandle)), OnRead(OnRead), Buffer(BufferSize), Offset(InitializeFileOffset(Handle.Get())) |
- Rewrite InterruptableRelay, BidirectionalRelay, ScopedMultiRelay::Run, and ScopedRelay::Run to use MultiHandleWait directly - Rewrite Dmesg read loop and GuestTelemetryLogger read loop to use ReadHandle + MultiHandleWait + EventHandle for exit signals - Move InterruptableRead/Write/Wait canonical implementations to wsl::windows::common::io namespace in HandleIO.h/cpp; remove redundant relay:: forwarding wrappers and update all callers to use io:: directly - Collapse 4 CreateThread overloads into a single variadic template - Add configurable BufferSize parameter to ReadHandle/RelayHandle - Remove dead code (CancelPendingIo, InitializeFileOffset, manual overlapped loops) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2b9a867 to
8f442d2
Compare
| } | ||
| ioWait.AddHandle( | ||
| std::make_unique<io::RelayHandle<io::ReadHandle>>(io::HandleWrapper{InputHandle}, io::HandleWrapper{OutputHandle}, BufferSize), | ||
| io::MultiHandleWait::IgnoreErrors | io::MultiHandleWait::CancelOnCompleted); |
| ioWait.AddHandle( | ||
| std::make_unique<io::ReadHandle>( | ||
| io::HandleWrapper{InputHandle}, [](const gsl::span<char>&) {}, BufferSize), | ||
| io::MultiHandleWait::IgnoreErrors | io::MultiHandleWait::CancelOnCompleted); |
| io::MultiHandleWait ioWait; | ||
| ioWait.AddHandle( | ||
| std::make_unique<io::RelayHandle<io::ReadHandle>>(io::HandleWrapper{Input}, io::HandleWrapper{Output}, BufferSize), | ||
| io::MultiHandleWait::IgnoreErrors | io::MultiHandleWait::CancelOnCompleted); |
| } | ||
| }, | ||
| BufferSize), | ||
| io::MultiHandleWait::IgnoreErrors); |
| ProcessInput(std::string_view{buffer.data(), static_cast<size_t>(buffer.size())}); | ||
| } | ||
| }), | ||
| io::MultiHandleWait::IgnoreErrors); |
| ProcessInput(Source, buffer); | ||
| } | ||
| }), | ||
| io::MultiHandleWait::IgnoreErrors); |
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
Migrates the old hand-rolled overlapped IO relay loops to the newer
MultiHandleWait/RelayHandle/ReadHandlesystem, reducing code by ~284 lines while consolidating all IO patterns into a single infrastructure.PR Checklist
Detailed Description
What changed
wsl::windows::common::ionamespace in HandleIO.h/cpp; removed redundant relay:: forwarding wrappers; updated all callers to use io:: directlyDesign decisions
HandleWrapper(HANDLE)is non-owning - used where the same handle is managed elsewhereIgnoreErrorsflag on read handles ensures pipe disconnects don't throwCancelOnCompleted | NeedNotCompleteon exit events ensures clean cancellation without requiring the event to fireInterruptableRelayuses a drain-onlyReadHandle(no-op callback) instead of RelayHandle to avoid writing to invalid handleValidation Steps