Skip to content

fix: correct SO_TYPE for DGRAM sockets, stderr race, socket ID wrap - #35

Merged
danbugs merged 6 commits into
mainfrom
fix/bugs
May 15, 2026
Merged

fix: correct SO_TYPE for DGRAM sockets, stderr race, socket ID wrap#35
danbugs merged 6 commits into
mainfrom
fix/bugs

Conversation

@danbugs

@danbugs danbugs commented May 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Three independent bug fixes:

  • SO_TYPE for DGRAM: net_getsockopt hardcoded SO_TYPE to 1 (SOCK_STREAM),
    but net_socket accepts type=2 (DGRAM). Now stores socket type in
    HostSocket at creation time and returns the actual value. Propagated through
    net_accept as well.
  • stderr capture race: dup2(capture_fd, 2) is process-global — concurrent
    VMs race on fd 2. Added a process-wide Mutex held for the capture duration.
    Also made the temp file path unique per thread.
  • SocketTable next_id wrap: Changed next_id from u32 to u64 and all
    related fd parsing sites. Prevents silent wraparound after 2^32 inserts.

Test plan

  • All existing socket, networking, and filesystem tests pass
  • Verified HostSocket tuple carries type through accept path

Copilot AI left a comment

Copy link
Copy Markdown

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 applies three bug fixes in the host runtime: (1) make net_getsockopt(SO_TYPE) reflect the actual socket type (STREAM vs DGRAM), (2) prevent concurrent VMs from racing while redirecting process-wide stderr, and (3) avoid silent socket-id wraparound by moving socket table IDs to u64.

Changes:

  • Track and return the real socket type (including for accepted sockets) and update socket table IDs from u32 to u64.
  • Serialize stderr redirection/restoration with a process-wide mutex to avoid cross-VM dup2 races.
  • Make stderr capture temp file names unique per thread.

Reviewed changes

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

File Description
host/src/stderr_capture.rs Adds a process-wide mutex and holds it across stderr redirection/restoration to prevent dup2 races between concurrent VMs.
host/src/lib.rs Extends socket tracking to include socket type, returns correct SO_TYPE, updates socket IDs to u64, and makes capture temp filenames per-thread unique.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread host/src/stderr_capture.rs Outdated
Comment on lines 27 to 33
let guard = STDERR_LOCK
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let capture_fd = std::fs::File::create(path)?.into_raw_fd();
let original_stderr_raw = unistd::dup(2)?;
unistd::dup2(capture_fd, 2)?;
unistd::close(capture_fd)?;
Comment on lines 41 to 44
pub fn restore(self) -> Result<()> {
unistd::dup2(self.original_stderr.as_raw_fd(), 2)?;
// _guard is dropped here, releasing STDERR_LOCK
Ok(())
Comment thread host/src/lib.rs
Comment on lines 1340 to 1344
let t = table.clone();
tools.register("net_getsockopt", move |args| {
let fd = args["fd"].as_u64().ok_or_else(|| anyhow!("missing 'fd'"))? as u32;
let fd = args["fd"].as_u64().ok_or_else(|| anyhow!("missing 'fd'"))?;
let level = args["level"].as_i64().unwrap_or(0) as i32;
let optname = args["optname"].as_i64().unwrap_or(0) as i32;

@github-actions github-actions Bot 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.

Linux Benchmarks

Details
Benchmark suite Current: 3410596 Previous: f5eb506 Ratio
hello_world (median) 20 ms 20 ms 1
pandas (median) 100 ms 110 ms 0.91
density (per VM) 7 MB 7 MB 1
snapshot (disk) 385 MiB 385 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

danbugs added 6 commits May 15, 2026 06:13
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>
Signed-off-by: danbugs <danilochiarlone@gmail.com>

@github-actions github-actions Bot 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.

Windows Benchmarks

Details
Benchmark suite Current: 3410596 Previous: f5eb506 Ratio
hello_world (median) 257 ms 221 ms 1.16
pandas (median) 808 ms 697 ms 1.16
density (per VM) 6 MB 6 MB 1
snapshot (disk) 393 MiB 393 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

@danbugs
danbugs merged commit 2ad29b8 into main May 15, 2026
79 checks passed
@danbugs
danbugs deleted the fix/bugs branch May 15, 2026 06:28
@danbugs danbugs mentioned this pull request May 15, 2026
1 task
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