Skip to content

Fix socket race condition in port tracking#40187

Open
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-potential-socket-race-condition-in-port-tracking
Open

Fix socket race condition in port tracking#40187
chemwolf6922 wants to merge 1 commit intomasterfrom
user/chemwolf6922/fix-potential-socket-race-condition-in-port-tracking

Conversation

@chemwolf6922
Copy link
Copy Markdown
Contributor

@chemwolf6922 chemwolf6922 commented Apr 15, 2026

Summary of the Pull Request

This PR makes the bind 0 port resolution in port tracking inline. To avoid duplicating the socket and causing race conditions. With the trade off of even slower bind calls.

PR Checklist

  • Closes: VSCode Remote-SSH SSH tunnel stuck on WSL2 2.7.1 #40109
  • 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

#40178 would be a better but much more complex solution. Which may not be worth it if the seccomp part is still required.

Validation Steps Performed

The problem described in #40109 is reproduceable. I have validated that this PR will fix the issue.
NetworkTests::MirroredTests::PortZeroBindIsTracked [Passed]
NetworkTests::VirtioProxyTests::PortZeroBindIsTracked [Passed]

@chemwolf6922 chemwolf6922 requested a review from a team as a code owner April 15, 2026 08:35
Copilot AI review requested due to automatic review settings April 15, 2026 08:35
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

Note

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

This PR removes the background thread used to resolve port-0 (ephemeral) binds and instead resolves them inline in the main GnsPortTracker::Run() loop to avoid duplicating sockets and triggering race conditions.

Changes:

  • Removed deferred port-0 resolution thread/queues and related synchronization primitives.
  • Made ResolvePortZeroBind return std::optional<PortAllocation> and resolve/register port-0 binds synchronously.
  • Simplified port-0 bind processing by directly calling HandleRequest/TrackPort after resolution.

Reviewed changes

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

File Description
src/linux/init/GnsPortTracker.h Removes deferred-resolution thread machinery; updates ResolvePortZeroBind signature to return an optional allocation.
src/linux/init/GnsPortTracker.cpp Deletes deferred resolver thread/queues; resolves port-0 binds inline and registers allocations immediately.

Comment thread src/linux/init/GnsPortTracker.cpp
Comment thread src/linux/init/GnsPortTracker.cpp
Comment thread src/linux/init/GnsPortTracker.cpp
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.

2 participants