Skip to content

[core][sandbox] Wait for gVisor boots with runsc's pid file - #67074

Merged
pcmoritz merged 9 commits into
ray-project:masterfrom
xyuzh:sandbox-boot-wait-10ms
Nov 9, 2026
Merged

pcmoritz merged 9 commits into
ray-project:masterfrom
xyuzh:sandbox-boot-wait-10ms

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Description

A gVisor sandbox create polls runsc state every 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.pid writes the file once the container exists. The start loop stats it every 10 ms and calls runsc state only after that, every 50 ms.
  • The public wrapper checks for the holder and slirp4netns every 10 ms instead of 0.1 s, still for up to 10 s.

Measured on 2026-10-14 against base 00a3adb, on a CoreWeave node pinned to 16 cores, creating python:3.12-slim sandboxes from a cached image; medians of 5 alternating runs per side. Every create succeeded, and CPU per create fell by up to 29%.

master this PR
One create, network="none" (p50) 114 ms 41 ms
One create, network="host" (p50) 115 ms 31 ms
One create, network="public" (p50) 324 ms 71 ms
96 creates at once, none 98/s 116/s
96 creates at once, public 48/s 70/s

Related issues

Split out of #66821.

Additional information

  • Not a duplicate: no open PR changes how a create waits (I searched for "runsc state", "pid-file" and "sandbox boot"). [core][sandbox] Add checkpoint/restore, pause/resume support using runsc #66471's restore path has its own copy of the 0.1 s loop. [core][sandbox] Run runsc CLI calls with GOMAXPROCS=2 #67075 edits the same runsc state call; whichever lands second needs a one-line merge.
  • Tests: test_restore_in_place_waits_for_a_new_pid_file checks 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's test_backend_applies_oom_priority_to_start_exec_and_file_io now writes the pid file its fake launch stands in for. The run-command tests now expect --pid-file and fail on master; every test that boots a sandbox goes through the new wait. test_create_times_out_when_runsc_state_hangs checks that a runsc state call that never returns can't hold a create past its deadline.
  • Tested at a04985a (master a83e55e 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: 474 passed, 25 skipped, and the same two GPU tests fail as on master (473 passed), since that container's Ray predates get_cdi_kind. As on master that hour, test_netns_owner_interrupt_leaves_its_sandbox_running failed because example.com answered the sandbox's wget with 403. pre-commit passes.
  • I used Claude Code to help write and test this change.

@gemini-code-assist gemini-code-assist 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.

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)

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.

medium

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).

Suggested change
res = subprocess.run(state_args, capture_output=True, text=True)
res = subprocess.run(state_args, capture_output=True, text=True, timeout=5)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Oct 15, 2026

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

Looks good to me, seems like a simple optimization!

@xyuzh
xyuzh force-pushed the sandbox-boot-wait-10ms branch from a6821d4 to 79937e2 Compare October 22, 2026 19:28
@pcmoritz
pcmoritz enabled auto-merge (squash) October 22, 2026 20:11
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Oct 22, 2026
xyuzh added 2 commits October 22, 2026 15:01
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]>
@xyuzh
xyuzh force-pushed the sandbox-boot-wait-10ms branch from 79937e2 to e26b6a6 Compare October 22, 2026 22:05
@github-actions
github-actions Bot disabled auto-merge October 22, 2026 22:05
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]>
@xyuzh

xyuzh commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@pcmoritz I merged master in e08a4eb, with one change after your approval. #66471's checkpoint/restore also launches sandboxes, using runsc restore. So restores now get --pid-file too. Restore takes all of create's flags (restore.go#L37-L38, #L155), and container.New writes the file for both (container.go#L387).

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): test_{gvisor_backend,checkpoint_restore,image_manager,image_utils,sandbox_runtime,overlayfs,sandbox_config}.py 362 passed, 15 skipped (master: 361 passed, 15 skipped). The 361 include master's mocked restore tests.

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

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 xyuzh left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

xyuzh added 2 commits November 8, 2026 22:17
…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.
@xyuzh

xyuzh commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Merged master (9fc7a43). #67688's new test_backend_applies_oom_priority_to_start_exec_and_file_io fakes runsc run without writing its --pid-file, so with this PR's start loop its creates timed out once both were on one branch. a04985a has the fake write the pid file, as runsc does. Real-gVisor suites: 474 passed, and the same two GPU tests fail as on master.

@pcmoritz
pcmoritz enabled auto-merge (squash) November 9, 2026 06:47
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]>
@github-actions
github-actions Bot disabled auto-merge November 9, 2026 07:39
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]>
@pcmoritz
pcmoritz enabled auto-merge (squash) November 9, 2026 09:18
@pcmoritz
pcmoritz merged commit 6c61f9a into ray-project:master Nov 9, 2026
7 checks passed
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 9, 2026
Signed-off-by: xyuzh <[email protected]>

ray-project#67074 rewrote the start loop's runsc state call; it keeps runsc_env().
pcmoritz added a commit that referenced this pull request Nov 9, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests sandbox

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants