Skip to content

perf(sandbox): block in poll(2) while the boundary is idle - #6148

Open
FrostGod wants to merge 2 commits into
mainfrom
perf/6071-boundary-poll-wait/FrostGod
Open

FrostGod wants to merge 2 commits into
mainfrom
perf/6071-boundary-poll-wait/FrostGod

Conversation

@FrostGod

@FrostGod FrostGod commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

An idle sandbox boundary woke every 10 ms to check its termination flag. It now sleeps in poll(2) until a SIGTERM/SIGINT arrives or, in listener mode, a connection is pending. In a microVM each of those wakeups is a VM exit.

Related Issue

Closes #6071

Changes

  • The SIGTERM/SIGINT handler also writes to a nonblocking, close-on-exec self-pipe.
  • The listener and runtime-initiated loops wait in poll(2) on that pipe (plus the listener fd) instead of sleep(10ms), with a 1 s fallback timeout.
  • Installing the handlers drains any stale wakeup so a reused pipe cannot turn the idle wait into a busy loop.

Testing

Measured on a Docker-backed gateway (Linux 6.8, arm64) with an idle sandbox. Wakeups are context switches across all boundary threads over 10 s.

Boundary Wakeups/s docker stop (SIGTERM)
main 91 0.06 s, exit 0
This branch 4 0.06 s, exit 0

An unrelated idle sandbox on the same host stayed at about 97 wakeups/s in both runs.

  • openshell-sandbox boundary_server tests pass on Linux (69 passed), including the new pending-connection wakeup test; cargo fmt and clippy -D warnings (with perf-harness) are clean
  • Docker conformance suite against this branch's sandbox image: 77 passed
  • Unit tests added/updated
  • E2E tests added/updated (if applicable)

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

- Replace the 10 ms sleep loops with poll(2) on a self-pipe.
- Signal handler writes to the pipe to wake the wait.
- Listener mode also wakes on a pending connection.
- Idle microVM cost: ~2.5% to ~0.2% of a core.

Closes #6071

Signed-off-by: Divesh <dgude@nvidia.com>
Signed-off-by: divesh <dgude@nvidia.com>
@github-actions github-actions Bot added the state:accepted A maintainer decided OpenShell should pursue this issue label Dec 1, 2026

@EmilienM EmilienM 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.

Another iteration needed: 2 major (inline)

Both majors sit in the new poll wait: the wake pipe is never drained at runtime, and the new test races on the shared termination statics. The self-pipe design itself is sound (level-triggered byte, O_CLOEXEC/O_NONBLOCK, errno save/restore in the handler).

How this was reviewed
  • Commit ce641f7fe, round 1
  • Deep review, because the idle loops were rewritten onto poll(2) over a signal-handler self-pipe (crates/openshell-sandbox/src/boundary_server.rs)
  • Passes: architecture, performance, security, scalability, rust, kernel, then correctness, then a defensive review
  • Independent skeptics checked each candidate finding (2 for the architecture candidate, 3 with a reproducer for the performance one, 3 per finding in the defensive round); 4 of 4 held up, one downgraded to minor
  • Repo rules read from main: AGENTS.md, CLAUDE.md, CONTRIBUTING.md
  • Not covered: a full rust-review of crates/openshell-sandbox (38k lines, over the autonomous size cap; recommended separately)

});
let timeout = libc::c_int::try_from(timeout.as_millis()).unwrap_or(libc::c_int::MAX);
// SAFETY: `fds` holds two valid pollfds for the duration of the call.
unsafe { libc::poll(fds.as_mut_ptr(), 2, timeout) };

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.

Major: The wake pipe is never drained during serve, so one stray byte spins both idle loops at 100% CPU forever. Workload children are forked with pre_exec hooks (process.rs:636, pty.rs:73), and between fork and exec a child inherits both the SIGTERM handler and the pipe write end (O_CLOEXEC only closes at execve): a SIGTERM landing there runs the inherited handler, writes a byte into the shared pipe, and sets the flag only in the child's memory. The parent's flag stays false, so poll(2) returns POLLIN immediately on every iteration with nothing to drain. The install-time drain runs before serve starts, so it cannot cover this. Draining after poll, the way managed_children::wait_for_reaper_event already does, makes the loops self-healing:

Suggested change
unsafe { libc::poll(fds.as_mut_ptr(), 2, timeout) };
let ready = unsafe { libc::poll(fds.as_mut_ptr(), 2, timeout) };
if ready > 0 && fds[0].revents & libc::POLLIN != 0 {
let mut buf = [0u8; 64];
// SAFETY: `buf` is valid for `buf.len()` bytes and the pipe is
// nonblocking, so the loop ends once it is empty.
while unsafe { libc::read(wake, buf.as_mut_ptr().cast(), buf.len()) } > 0 {}
}


let started = std::time::Instant::now();
wait_for_termination_signal(Some(listener.as_raw_fd()), Duration::from_millis(50));
assert!(started.elapsed() >= Duration::from_millis(50));

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.

Major: This assertion races with the other tests in this binary that write the shared wake pipe: the EMFILE test calls request_boundary_termination(SIGTERM) directly (line 11025) and the supervisor-confirmation test sends a real kill(Pid::this(), SIGTERM) with the handler installed (line 6586), so a byte can land after this test's drain, wake poll early, and fail the 50ms assertion intermittently. Worth isolating this test from the global pipe, or serializing the tests that touch the termination statics, before it becomes a flaky lane.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state:accepted A maintainer decided OpenShell should pursue this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(sandbox): idle boundary wakes every 10 ms

2 participants