Repository navigation
[core][sandbox] Wait for gVisor boots with runsc's pid file - #67074
Conversation
There was a problem hiding this comment.
Code Review
This pull request optimizes the gVisor sandbox startup process by tracking the container's PID file and adjusting polling intervals. It introduces a PID file check before running runsc state to avoid unnecessary CPU usage during container boot, and reduces the wait loop intervals for network setup. The test suite is updated to verify the new --pid-file argument. A review comment suggests adding a timeout to the subprocess.run call for runsc state to prevent potential indefinite hangs.
| res = None | ||
| if os.path.exists(pid_file): | ||
| state_args = self._runsc_base_args(config) + ["state", sandbox_id] | ||
| res = subprocess.run(state_args, capture_output=True, text=True) |
There was a problem hiding this comment.
To prevent potential indefinite hangs during sandbox creation, it is recommended to add a timeout to the subprocess.run call for runsc state, consistent with other subprocess.run invocations in this class (e.g., in delete_sandbox and _delete_container_state).
| res = subprocess.run(state_args, capture_output=True, text=True) | |
| res = subprocess.run(state_args, capture_output=True, text=True, timeout=5) |
There was a problem hiding this comment.
Done in a6821d4, a bit differently: the call gets the time the create has left (at least 1 s), and one that runs out counts as not running yet, so the create still ends with SandboxTimeoutError at its deadline. A fixed 5 s could cut short a slow but healthy runsc state on a loaded node. test_create_times_out_when_runsc_state_hangs fails without it.
pcmoritz
left a comment
There was a problem hiding this comment.
Looks good to me, seems like a simple optimization!
a6821d4 to
79937e2
Compare
A sandbox create polled `runsc state` every 0.1 s until the container ran. Each poll is a separate Go process, so a create waited up to 0.1 s past its boot, and a burst of creates spent CPU on the polling that the boots themselves need. Run `runsc run --pid-file` and stat that file every 10 ms; ask `runsc state` only once it exists, then every 50 ms. network="public" sandboxes also waited in 0.1 s steps for the namespace holder and slirp4netns; those waits now check every 10 ms, still for up to 10 s. Signed-off-by: xyuzh <[email protected]>
The start loop's runsc state call had no timeout, so a wedged runsc held the create forever, past timeout_seconds. The call now gets the time the create has left (at least 1 s), and a call that runs out counts as not running yet, so the loop's deadline check raises SandboxTimeoutError as it does for a slow boot. Signed-off-by: xyuzh <[email protected]>
79937e2 to
e26b6a6
Compare
Master's checkpoint/restore (ray-project#66471) also launches sandboxes with `runsc restore`, which honors --pid-file the way `runsc run` does, so restores get the pid file too. Once it exists, a restore's state is polled every 10 ms, as master polled restores, and a cold boot's every 50 ms. The restore tests' fake launches now write the pid file, and master's launch-argument assertions include --pid-file. Signed-off-by: xyuzh <[email protected]>
|
@pcmoritz I merged master in e08a4eb, with one change after your approval. #66471's checkpoint/restore also launches sandboxes, using Once the pid file exists, the create polls a restore's state every 10 ms, as master already did for restores, and a cold boot's every 50 ms. In the restore tests, the fake launches now write the pid file. Real gVisor in a privileged container (aarch64, runsc release-20261026.0): |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit e08a4eb. Configure here.
… place A sandbox restored in place reuses its bundle, and sandbox.pid from the launch that the checkpoint stopped was still there. The start loop took it as the new container's and asked `runsc state` from the start instead of waiting for the restore to write the file. Remove it with netns.pid and slirp4netns.ready. Signed-off-by: xyuzh <[email protected]>
xyuzh
left a comment
There was a problem hiding this comment.
@pcmoritz @andrewsykim, one change since your approvals: 52a8ef7 removes the old launch's pid file before an in-place restore, from Bugbot's finding below.
Signed-off-by: xyuzh <[email protected]>
…unch Signed-off-by: xyuzh <[email protected]> ray-project#67688's test fakes runsc run without writing the --pid-file file, so with this PR's start loop, which waits for that file before asking runsc state, its creates timed out.
|
Merged master (9fc7a43). #67688's new |
The jobs that failed in premerge build 76503 failed only on downloads from GitHub during GitHub's incident on 2026-11-09. Signed-off-by: xyuzh <[email protected]>
premerge build 76505 failed before running tests: GitHub's incident on 2026-11-09 still blocked downloads from CI then. It's resolved now. Signed-off-by: xyuzh <[email protected]>
Signed-off-by: xyuzh <[email protected]> ray-project#67074 rewrote the start loop's runsc state call; it keeps runsc_env().
## Description Every runsc CLI call the gVisor backend makes (`run`, `state`, `exec`, `kill`, `delete`, `spec`) is a Go process, and Go sizes its runtime to every CPU it can see. On a 64-core node without a CPU limit, 64 concurrent `runsc exec` calls cost 0.023 CPU-s each. gVisor's containerd shim runs runsc with `GOMAXPROCS=2` for this reason. This PR runs those calls with `GOMAXPROCS=2` too (`runsc_env()` in `backend/gvisor.py`), unless the worker's environment sets it. runsc doesn't pass the variable on to the sandbox or the gofer, so `nproc` in a sandbox still reports every CPU. Measured on 2026-10-14 against base 00a3adb, on a 64-core CoreWeave node: 64 sandboxes each ran 20 `true` execs back to back, all at once; medians of 5 alternating runs per side. | CPUs runsc sees | master | this PR | | --- | --- | --- | | 64 (no CPU limit) | 1,855 execs/s, 0.0226 CPU-s each | 2,436 execs/s, 0.0129 CPU-s each | | 16 (`taskset`) | 907 execs/s, 0.0162 CPU-s each | 990 execs/s, 0.0146 CPU-s each | In pods with CPU limits, Go already sizes itself to the limit and the gain is small: 2-4% more execs and creates through the gRPC facade on four 30-CPU pods, with `GOMAXPROCS=2` set in the Ray workers' environment (runsc inherits it). ## Related issues Split out of #66821. ## Additional information - Not a duplicate: no open PR sets `GOMAXPROCS` for runsc (I searched for "sandbox GOMAXPROCS" and "runsc spec"). #66932 and #67022 add runsc calls, which should pass `env=runsc_env()` if this lands first. #67074 and #67076 edit the same `runsc state` and `runsc spec` calls; one-line merges. - Tests: `test_runsc_env_caps_gomaxprocs_unless_set` checks the default and an override; `test_runsc_run_starts_with_gomaxprocs` boots a sandbox and reads its `runsc run`'s environment. Both fail on master. `test_gpu_checks_run_runsc_with_runsc_env` covers the two GPU checks' runsc calls that came in with #65898. - Tested at 3cfe602, with master 6c61f9a (which has #67074) merged in, in a privileged container (aarch64, runsc release-20261026.0, erofs-utils 1.9.4): `TEST_SANDBOX=1 pytest tests/test_{gvisor_backend,checkpoint_restore,image_manager,image_utils,sandbox_runtime,overlayfs,sandbox_config,cgroup,resource_limits}.py`: 480 passed, 25 skipped, and the same two GPU tests fail as on master (473 passed), since that container's Ray predates `get_cdi_kind`. pre-commit passes. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]> Co-authored-by: Philipp Moritz <[email protected]>

Description
A gVisor sandbox create polls
runsc stateevery 0.1 s until the container runs. Each poll is a Go process, so a create returns up to 0.1 s after its 30-40 ms boot, and a burst of creates spends CPU on polling.network="public"sandboxes also wait in 0.1 s steps for the namespace holder and slirp4netns.This PR changes the waits in
backend/gvisor.py:runsc run --pid-file <bundle>/sandbox.pidwrites the file once the container exists. The start loop stats it every 10 ms and callsrunsc stateonly after that, every 50 ms.Measured on 2026-10-14 against base 00a3adb, on a CoreWeave node pinned to 16 cores, creating
python:3.12-slimsandboxes from a cached image; medians of 5 alternating runs per side. Every create succeeded, and CPU per create fell by up to 29%.network="none"(p50)network="host"(p50)network="public"(p50)nonepublicRelated issues
Split out of #66821.
Additional information
runsc statecall; whichever lands second needs a one-line merge.test_restore_in_place_waits_for_a_new_pid_filechecks that a restore in place doesn't start from the stopped launch's pid file, and [core][sandbox] Make oom-score-adj configurable for each sandbox #67688'stest_backend_applies_oom_priority_to_start_exec_and_file_ionow writes the pid file its fake launch stands in for. The run-command tests now expect--pid-fileand fail on master; every test that boots a sandbox goes through the new wait.test_create_times_out_when_runsc_state_hangschecks that arunsc statecall that never returns can't hold a create past its deadline.TEST_SANDBOX=1 pytest tests/test_{gvisor_backend,checkpoint_restore,image_manager,image_utils,sandbox_runtime,overlayfs,sandbox_config,cgroup,resource_limits}.py: 474 passed, 25 skipped, and the same two GPU tests fail as on master (473 passed), since that container's Ray predatesget_cdi_kind. As on master that hour,test_netns_owner_interrupt_leaves_its_sandbox_runningfailed because example.com answered the sandbox'swgetwith 403. pre-commit passes.