Skip to content

fix: set default 30s timeout on guest-created sockets - #65

Merged
danbugs merged 2 commits into
mainfrom
fix/socket-timeouts
May 17, 2026
Merged

fix: set default 30s timeout on guest-created sockets#65
danbugs merged 2 commits into
mainfrom
fix/socket-timeouts

Conversation

@danbugs

@danbugs danbugs commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Sets 30s read/write timeout on every socket created via net_socket, preventing indefinite host-thread blocking
  • Replaces sock.connect(&sa)? with sock.connect_timeout(&sa, SOCKET_TIMEOUT)? in net_connect to bound TCP connect time
  • Previously, net_recv, net_accept, and net_connect could block the host thread forever — a reliable single-call DoS against the sandbox
  • Adds net_socket_has_default_timeout test

Test plan

  • cargo test --lib passes (54 tests)
  • cargo clippy clean
  • cargo fmt clean
  • Existing networking integration tests still pass (pyhl_runtime tests with --net)

Copilot AI review requested due to automatic review settings May 17, 2026 06:43

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 hardens the host-proxied networking tools by bounding how long host threads can block on guest-initiated socket operations, mitigating a single-call DoS vector from indefinite blocking.

Changes:

  • Introduces a 30s default socket read/write timeout for sockets created via net_socket.
  • Switches net_connect to connect_timeout to cap TCP connect duration.
  • Adds a unit test intended to validate the default timeout behavior.

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

Comment thread host/src/lib.rs Outdated
Comment on lines 1204 to 1205
let tbl = t.lock().unwrap();
let sock = tbl.get_socket(fd)?;
Comment thread host/src/lib.rs Outdated
Comment on lines +3233 to +3235
let fd = v["result"]["fd"].as_u64().unwrap();

// Verify the socket was created successfully
Comment thread host/src/lib.rs Outdated
Comment on lines +3227 to +3239
register_internal_tools(&mut tools, &exit_code, Some(&NetworkPolicy::AllowAll), None);

let req = br#"{"name":"net_socket","args":{"family":2,"type":1}}"#;
let resp = tools.dispatch(req);
let s = std::str::from_utf8(&resp).unwrap();
let v: serde_json::Value = serde_json::from_str(s).unwrap();
let fd = v["result"]["fd"].as_u64().unwrap();

// Verify the socket was created successfully
assert!(fd > 0);
// The timeout is set internally; we verify it works by checking
// that the response is valid (no error from set_read_timeout)
assert!(!s.contains("error"), "socket creation should succeed: {s}");

@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: 5af8e1f Previous: 5198570 Ratio
hello_world (median) 20 ms 20 ms 1
pandas (median) 110 ms 110 ms 1
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
danbugs force-pushed the fix/socket-timeouts branch from c0082a6 to 1b28cbd Compare May 17, 2026 06:59

@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: 5af8e1f Previous: 5198570 Ratio
hello_world (median) 310 ms 191 ms 1.62
pandas (median) 982 ms 540 ms 1.82
density (per VM) 6 MB 6 MB 1
snapshot (disk) 392 MiB 392 MiB 1

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

Adds SOCKET_TIMEOUT (30s) applied to all sockets at creation time via
set_read_timeout/set_write_timeout. Also uses connect_timeout in
handle_net_connect, releasing the SocketTable lock before the blocking
connect call to avoid stalling concurrent socket operations.

Includes a unit test that verifies the timeout is actually set.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs
danbugs force-pushed the fix/socket-timeouts branch from 206e878 to 2eb4c8b Compare May 17, 2026 07:11
Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs
danbugs merged commit 4c053f6 into main May 17, 2026
211 of 217 checks passed
@danbugs
danbugs deleted the fix/socket-timeouts branch May 17, 2026 07:55
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