Skip to content

Fix sockets not getting an ephemeral port between bind() and listen()#6473

Closed
Arshia001 wants to merge 7 commits intowasmerio:mainfrom
Arshia001:fix/issue-6403-tcp-bind-plan
Closed

Fix sockets not getting an ephemeral port between bind() and listen()#6473
Arshia001 wants to merge 7 commits intowasmerio:mainfrom
Arshia001:fix/issue-6403-tcp-bind-plan

Conversation

@Arshia001
Copy link
Copy Markdown
Member

No description provided.

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 addresses wasix TCP semantics so bind(..., port=0) allocates an ephemeral port immediately (before listen()/connect()), matching POSIX behavior and fixing #6403. It does so by introducing an explicit “bound TCP” state in virtual-net and plumbing it through wasix, journaling, and tests.

Changes:

  • Add WASIX wasm regression tests for bind(port=0) behavior (port becomes nonzero after bind(); remains stable after listen()/connect()).
  • Extend virtual-net with bind_tcp + VirtualTcpBoundSocket, implement it for host/loopback/remote backends, and add unit tests.
  • Update wasix socket state machine + journaling to capture the effective bound address (including ephemeral port).

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/wasix/tests/wasm_tests/socket_tests/bind-port-zero/main.c New wasm test asserting ephemeral port allocation happens at bind() and is stable across listen().
lib/wasix/tests/wasm_tests/socket_tests/bind-port-zero/build.sh Build script for the new wasm test.
lib/wasix/tests/wasm_tests/socket_tests/bind-port-zero-connect/main.c New wasm test asserting bind-assigned ephemeral port stays stable across connect().
lib/wasix/tests/wasm_tests/socket_tests/bind-port-zero-connect/build.sh Build script for the new wasm test.
lib/wasix/tests/wasm_tests/socket_tests.rs Registers the two new wasm socket tests in the Rust harness.
lib/wasix/src/syscalls/wasix/sock_bind.rs Journals the effective local address after bind (important for port=0 correctness in replay).
lib/wasix/src/net/socket.rs Introduces BoundTcp inode socket state; updates bind/listen/connect/addr_local/status paths accordingly.
lib/virtual-net/src/lib.rs Adds VirtualNetworking::bind_tcp and the VirtualTcpBoundSocket trait.
lib/virtual-net/src/host.rs Implements bind_tcp using socket2 and adds LocalTcpBoundSocket.
lib/virtual-net/src/loopback.rs Implements bind_tcp for loopback and allocates ephemeral ports at bind time.
lib/virtual-net/src/meta.rs Extends remote protocol with BindTcp, ListenBound, and ConnectBound.
lib/virtual-net/src/client.rs Implements remote bind_tcp and bound-socket transitions; adds binding-ownership tracking.
lib/virtual-net/src/server.rs Implements server-side handling for BindTcp and bound-socket transitions.
lib/virtual-net/src/tests.rs Adds tests covering ephemeral port allocation at bind and stability across listen/connect.
docs/dev/issue-6403-tcp-bind-plan.md Adds an implementation plan / rationale document for #6403.
Comments suppressed due to low confidence (1)

lib/virtual-net/src/client.rs:930

  • RemoteNetworkingClient::new_socket() inserts per-socket entries into accept_tx and sent_tx, but Drop for RemoteSocket only removes recv_tx and recv_with_addr_tx. This will grow the maps over time (socket IDs are monotonically increasing) and can leak memory/resources. Remove the corresponding accept_tx and sent_tx entries on drop as well (guarded by owns_socket_bindings, like the others).
        self.common.recv_tx.lock().unwrap().remove(&self.socket_id);
        self.common
            .recv_with_addr_tx
            .lock()
            .unwrap()
            .remove(&self.socket_id);

Comment thread lib/virtual-net/src/loopback.rs
Comment thread lib/virtual-net/src/server.rs
Comment thread lib/virtual-net/src/host.rs Outdated
Comment thread lib/wasix/src/net/socket.rs Outdated
Comment thread lib/virtual-net/src/loopback.rs
@artemyarulin
Copy link
Copy Markdown
Contributor

/patchsmith test

@Arshia001
Copy link
Copy Markdown
Member Author

too early @artem, let's wait for CI to turn green first

@artemyarulin
Copy link
Copy Markdown
Contributor

Cool, but can you resubmit it as PR on branch in this repo? patchsmith unfortunately doesn't run on the forks yet

@Arshia001
Copy link
Copy Markdown
Member Author

will do once all patches are in @artemyarulin

@Arshia001
Copy link
Copy Markdown
Member Author

Superseded by #6478

@Arshia001 Arshia001 closed this Apr 15, 2026
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.

3 participants