Skip to content

Support full duplex traffic and half close socket in NAT localhost relay - Windows side - #41458

Open
Feng Wang (chemwolf6922) wants to merge 15 commits into
masterfrom
user/chemwolf6922/support-full-duplex-in-NAT-localhost-relay
Open

Feng Wang (chemwolf6922) wants to merge 15 commits into
masterfrom
user/chemwolf6922/support-full-duplex-in-NAT-localhost-relay

Conversation

@chemwolf6922

@chemwolf6922 Feng Wang (chemwolf6922) commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

This is a continuation of wangxin12's work in #40171.

The original localhost relay, on both sides, would:

  1. Block the other direction's traffic if one direction's write would block.
  2. Close both sides if one side's read is closed. Which causes problems in half close socket scenarios.

This PR makes the Windows side changes to support full duplex traffic and half close socket in NAT localhost relay:

  1. Refactor the windows bi-directional relay with MultiHandleWait with a new half close capable relay handle.

The Linux changes will be in a separate PR.

PR Checklist

  • Closes:
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Add tests:
UnitTests::UnitTests::BidirectionalRelayContinuesAfterHalfClose
UnitTests::UnitTests::BidirectionalRelayFullDuplexUnderBackpressure

@chemwolf6922
Feng Wang (chemwolf6922) requested a review from a team as a code owner August 27, 2026 03:12
Copilot AI lite review requested due to automatic review settings August 27, 2026 03:12

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

This PR improves the NAT localhost relay implementation to correctly support full-duplex traffic and TCP half-close semantics, addressing hangs and premature connection teardown in bidirectional protocols.

Changes:

  • Refactors the Windows bidirectional relay to use MultiHandleWait, introducing HalfCloseRelayHandle to propagate half-close (shutdown(SD_SEND)) without terminating the opposite direction.
  • Rewrites the Linux localhost relay into a non-blocking, event-driven epoll loop to support full duplex and avoid per-connection threads.
  • Adds Windows unit/integration tests covering half-close and full-duplex behavior through the localhost relay.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/windows/UnitTests.cpp Adds a unit test validating relay continues after half-close.
test/windows/NetworkTests.cpp Adds WSL2 NAT localhost relay tests for half-close and full-duplex behavior; extends socat helper.
src/windows/common/relay.cpp Replaces legacy WaitForMultipleObjects relay loop with MultiHandleWait + relay handle abstractions.
src/windows/common/HandleIO.h Extends ReadHandle to accept a buffer size; adds HalfCloseRelayHandle declaration.
src/windows/common/HandleIO.cpp Implements buffer-sized ReadHandle construction and HalfCloseRelayHandle shutdown behavior.
src/linux/init/localhost.cpp Replaces blocking/threaded relay with per-connection state machine driven by epoll on non-blocking sockets.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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

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

Copilot AI review requested due to automatic review settings August 27, 2026 04:18

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

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

src/linux/init/localhost.cpp:521

  • connect() can return EINTR when a signal interrupts the attempt; for this case Linux leaves the connection attempt in progress, but this branch currently tears down the relay connection. The previous blocking implementation retried interrupted connects, and the new accept/read/write paths also handle interruptions. Treat EINTR like EINPROGRESS and let epoll complete the connection instead.
        const int result = connect(m_tcpSocket.get(), socketAddress, socketAddressSize);
        if (result < 0 && errno != EINPROGRESS)

Copilot AI review requested due to automatic review settings September 23, 2026 04:22
@chemwolf6922
Feng Wang (chemwolf6922) requested a review from a team as a code owner September 23, 2026 04:22

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 review overview

🟡 Changes recommended

The full-duplex test must verify that queued forward traffic resumes and drains.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)

Comment thread test/windows/wsl/NetworkTests.cpp Resolved
Copilot AI review requested due to automatic review settings September 23, 2026 05:24

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 review overview

🟡 Changes recommended

The critical relay starvation and interrupted Linux connection handling issues must be resolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Handle EINTR and EALREADY during nonblocking connect

src/​linux/​init/​localhost.cpp:522

The old blocking connect used TEMP_FAILURE_RETRY, but this nonblocking call now treats EINTR (and the EALREADY that can result when an interrupted connect is retried) as a terminal connection failure. A signal during setup will therefore drop an otherwise viable relay connection instead of waiting for it to complete; retry the call on EINTR and accept EALREADY as an in-progress state.

Comment thread src/windows/common/relay.cpp Resolved
Copilot AI review requested due to automatic review settings September 29, 2026 03:29

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 review overview

🔵 Needs a closer look

The cross-platform socket lifecycle and event-loop changes warrant final human networking review despite comprehensive coverage.

Review effort: Balanced
Findings: None

Resolved since last review (1)

Copilot AI balanced review requested due to automatic review settings October 12, 2026 08:54
@tunabirgun

This comment was marked as low quality.

@tunabirgun

This comment was marked as low quality.

Copilot AI balanced review requested due to automatic review settings November 9, 2026 02:50
@chemwolf6922 Feng Wang (chemwolf6922) changed the title Support full duplex traffic and half close socket in NAT localhost relay Support full duplex traffic and half close socket in NAT localhost relay - Windows side Nov 9, 2026

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.

🟡 Changes recommended

Test teardown can close relay sockets while the worker still uses them, and regression coverage does not exercise simultaneous bidirectional writes under back-pressure.

2 open issues

Review effort: Balanced

Comment thread src/windows/common/relay.cpp Resolved
Comment thread test/windows/wsl/UnitTests.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 9, 2026 03:15

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.

🔵 Needs a closer look

Failure-path cleanup closes relay sockets before the worker joins, risking races with pending I/O, and the tests do not verify reverse-traffic progress under write backpressure.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 9, 2026 03:18

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.

🟡 Changes recommended

Test cleanup closes relay sockets before the worker finishes, risking outstanding I/O accessing destroyed buffers, and regression coverage omits full-duplex traffic under blocked-write back-pressure.

3 open issues

Review effort: Balanced

Comment thread test/windows/wsl/UnitTests.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 9, 2026 04:34

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.

🔵 Needs a closer look

Completion and cancellation behavior still needs Windows runtime validation for TCP-to-Hyper-V and mixed socket/named-pipe KD relays, as the added tests cover only TCP loopback sockets.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

@chemwolf6922

Copy link
Copy Markdown
Contributor Author

Hi Tuna Birgün (@tunabirgun) . Thanks for the interest in this PR. However, please control the bot generated comments. It's disrupting the normal review message flow. I'll do a cleanup pass on those messages.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 03:18

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.

🔵 Needs a closer look

The asynchronous I/O and teardown changes affect TCP↔HvSocket NAT forwarding and socket↔pipe KD relaying, but tests cover only TCP↔TCP, leaving the production endpoint combinations unvalidated on Windows.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 16, 2026 03:46

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.

🔵 Needs a closer look

Windows runtime validation is needed for TCP↔Hyper-V half-close and KD socket↔named-pipe cancellation behavior, which the added loopback TCP tests do not cover.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
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.

NAT localhost forwarding: wslrelay.exe --mode 1 deadlocks under concurrent bidirectional bulk transfer

4 participants