Skip to content

Set SO_UPDATE_ACCEPT_CONTEXT on sockets accepted via AcceptEx in PortRelay#40505

Open
wangxin12 wants to merge 2 commits into
masterfrom
user/wangxin12/portrelay-fix
Open

Set SO_UPDATE_ACCEPT_CONTEXT on sockets accepted via AcceptEx in PortRelay#40505
wangxin12 wants to merge 2 commits into
masterfrom
user/wangxin12/portrelay-fix

Conversation

@wangxin12
Copy link
Copy Markdown
Contributor

Summary of the Pull Request

After AcceptEx completes, set SO_UPDATE_ACCEPT_CONTEXT to mark the socket as connected. Without this, getpeername/getsockname/shutdown and a number of setsockopt/getsockopt options fail on the accepted socket.

PR Checklist

  • Closes: Link to issue #xxx
  • 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

Copilot AI review requested due to automatic review settings May 12, 2026 05:07
@wangxin12 wangxin12 requested a review from a team as a code owner May 12, 2026 05:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates the Windows port relay implementations to call setsockopt(..., SO_UPDATE_ACCEPT_CONTEXT, ...) on sockets accepted via AcceptEx, ensuring the accepted socket is fully “connected” from Winsock’s perspective (enabling getpeername/getsockname/shutdown and related socket options to work correctly).

Changes:

  • Set SO_UPDATE_ACCEPT_CONTEXT after AcceptEx completion in the wslrelay localhost port relay (both async-completion and synchronous-completion paths).
  • Set SO_UPDATE_ACCEPT_CONTEXT after AcceptEx completion in the wslcsession PortRelayAcceptHandle accept path.

Reviewed changes

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

File Description
src/windows/wslrelay/localhost.cpp Updates the PortRelay accept flow to set SO_UPDATE_ACCEPT_CONTEXT on the accepted socket before relaying.
src/windows/wslcsession/PortRelayHandle.cpp Updates PortRelayAcceptHandle::Collect() to set SO_UPDATE_ACCEPT_CONTEXT prior to launching the relay thread.

Comment thread src/windows/wslrelay/localhost.cpp
Comment thread src/windows/wslrelay/localhost.cpp
Comment thread src/windows/wslcsession/PortRelayHandle.cpp
Comment thread src/windows/wslrelay/localhost.cpp
@benhillis
Copy link
Copy Markdown
Member

Can you add a regression test please?

@wangxin12
Copy link
Copy Markdown
Contributor Author

Can you add a regression test please?

Hi @benhillis , I looked into this and ran into a few obstacles that make a meaningful regression test hard. Wanted to walk through them and get your input on the best path forward.

  1. The accepted socket is not exposed, so we can't directly call APIs like shutdown on it to test.
  2. The current wsl::windows::common::relay::BidirectionalRelay logs and swallows the failure, and relay loop tears down both directions on the first EOF either way. From the client's perspective the connection still closes cleanly — just via closesocket instead of orderly shutdown.
LOG_LAST_ERROR_IF(shutdown(reinterpret_cast<SOCKET>(LeftHandle), SD_SEND) == SOCKET_ERROR)

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.

4 participants