Skip to content

Localhost Relay Supports Full Duplex in NAT Networking - #40171

Closed
wangxin12 wants to merge 13 commits into
masterfrom
user/wangxin12/localhostrelay
Closed

wangxin12 wants to merge 13 commits into
masterfrom
user/wangxin12/localhostrelay

Conversation

@wangxin12

@wangxin12 wangxin12 commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

This PR improves the localhost relay performance and reliability on both Linux and Windows, and migrates the Windows BidirectionalRelay to reuse the existing MultiHandleWait + RelayHandle infrastructure.

Linux

  • Non-blocking poll-based relay: Extracted a generic UtilRelay helper (util.cpp) that uses poll() with non-blocking fds, per-direction buffering, back-pressure via available buffer space.
  • Localhost relay (localhost.cpp) now calls UtilRelay instead of the old blocking relay, with caller-specified buffer sizes.

Windows

  • Add HalfCloseRelayHandle for socket specifically, supports half-close (shutdown(SD_SEND)) on EOF/error.
  • Rewrite BidirectionalRelay with MultiHandleWait + two RelayHandle<> instances, for consistency with other relay callers.
  • Buffer size parameter: Added BufferSize through ReadHandle → RelayHandle → BidirectionalRelay to preserve caller-specified buffer sizes (128KB for localhost relay vs 4KB default).

Testing

  • Verified with full duplex TCP stress test (50,000 x 32KB frames each direction)
  • TCP half-close works end-to-end: one direction can finish while the other continues
  • No sequence errors, no data corruption
image image

Without the change, the test tool hangs. image

PR Checklist

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Xin Wang (from Dev Box) added 2 commits April 14, 2026 02:04
@wangxin12
wangxin12 requested a review from a team as a code owner April 13, 2026 18:50
Copilot AI review requested due to automatic review settings April 13, 2026 18:50

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

Note

Copilot was unable to run its full agentic suite in this review.

Refactors the localhost relay implementation to use non-blocking, back-pressure-aware I/O on Linux and IOCP-based fully asynchronous I/O on Windows, improving full-duplex throughput and correct half-close behavior.

Changes:

  • Windows: replaced wait/event-based overlapped I/O with IOCP dispatch and per-direction read/write state machines.
  • Linux: switched sockets to non-blocking mode and implemented a poll-driven relay with per-direction buffering and compaction.
  • Added per-direction half-close handling so each direction can complete independently.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/windows/common/relay.cpp Reworks BidirectionalRelay to use IOCP with per-direction async read/write contexts and completion processing.
src/linux/init/localhost.cpp Implements a non-blocking poll() relay loop with per-direction buffers, back-pressure, and half-close support.

Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
@guanwenbin123

guanwenbin123 commented Apr 13, 2026 via email

Copy link
Copy Markdown

Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
Comment thread src/linux/init/localhost.cpp Resolved
@chemwolf6922

Copy link
Copy Markdown
Contributor

guanwenbin123 Hi, could you please configure your auto reply so it does not create a message for every wsl update? Thanks.

Comment thread src/windows/common/relay.cpp Outdated
rightOverlapped.hEvent = rightOverlappedEvent.get();
LARGE_INTEGER rightOffset{};
// Create a completion port and associate both handles.
wil::unique_handle iocp(CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 1));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

On the windows I think it would be nice to reuse MultHandleWait to do all of the IO scheduling (especially once https://github.com/microsoft/WSL/pull/40042/changes is merged)

We could either cherry-pick the changes from features/wsl-for-apps, or we could wait until that feature branch is merged into master, which should happen in the coming month

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good to know. Let's wait for https://github.com/microsoft/WSL/pull/40042/changes to be merged, then I can refactor changes on Windows side

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Blue (@OneBlue) Ben Hillis (@benhillis) , I cherry pick Ben's commit and make change on top of it (the PR seems to be still in working and have not updated for several weeks). Could you help review this when you get a chance?
I'd like to get this fix into the next 2.7.x release train if possible. Let me know your thought on this. Thanks!

Comment thread src/linux/init/localhost.cpp Resolved
Xin Wang (from Dev Box) and others added 4 commits April 22, 2026 03:31
The previous fix unconditionally called GetOverlappedResult(bWait=TRUE),
which hangs in IOCP mode because Overlapped.hEvent is nullptr and
cancellation completions go to the IOCP queue, not the file handle.

Fix: when RegisteredWithIocp, set a temporary event in the OVERLAPPED
before calling CancelIoEx so GetOverlappedResult can wait on it. This
ensures the kernel is done with the OVERLAPPED before the destructor
frees it, without hanging.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 03:08

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 5 out of 5 changed files in this pull request and generated 4 comments.

Comment thread src/linux/init/util.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.hpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 04:53

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 5 out of 5 changed files in this pull request and generated 10 comments.

Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/windows/common/relay.cpp Resolved
Comment thread src/linux/init/util.cpp Resolved
@benhillis

Copy link
Copy Markdown
Member

⚠️ Heads up! Due to a recent large merge to master, your PR now has merge conflicts. Sorry about that! Please resolve them when you get a chance, or reach out to Ben Hillis (@benhillis) for help.

@wangxin12
wangxin12 requested a review from a team as a code owner May 6, 2026 21:27
Copilot AI review requested due to automatic review settings June 4, 2026 15:26

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.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

@wangxin12 wangxin12 changed the title Localhost Relay: non-blocking IO on Linux side, IOCP with fully async reads and writes on Windows side Localhost Relay Supports Full Duplex in NAT Networking Jun 5, 2026
@wangxin12

Copy link
Copy Markdown
Contributor Author

Hi Blue (@OneBlue), I updated this PR with the latest MultiHandleWait + RelayHandle, could you please take another look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants