Repository navigation
Conversation
- 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>
EmilienM
left a comment
There was a problem hiding this comment.
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) }; |
There was a problem hiding this comment.
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:
| 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)); |
There was a problem hiding this comment.
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.
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
poll(2)on that pipe (plus the listener fd) instead ofsleep(10ms), with a 1 s fallback timeout.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.
docker stop(SIGTERM)mainAn unrelated idle sandbox on the same host stayed at about 97 wakeups/s in both runs.
openshell-sandboxboundary_servertests pass on Linux (69 passed), including the new pending-connection wakeup test;cargo fmtandclippy -D warnings(withperf-harness) are cleanChecklist