Skip to content

[core][sandbox] Run runsc CLI calls with GOMAXPROCS=2 - #67075

Merged
pcmoritz merged 11 commits into
ray-project:masterfrom
xyuzh:sandbox-runsc-gomaxprocs
Nov 9, 2026
Merged

pcmoritz merged 11 commits into
ray-project:masterfrom
xyuzh:sandbox-runsc-gomaxprocs

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

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

@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 introduces a runsc_env() helper function to limit the GOMAXPROCS environment variable to 2 (unless already set) for all runsc CLI subprocesses in the gVisor sandbox backend. This change prevents excessive CPU consumption on multi-core systems during concurrent CLI calls. The helper is integrated into various subprocess invocations across gvisor.py and image_manager.py, and corresponding unit tests have been added to verify its behavior. There are no review comments, and the implementation looks solid with no additional feedback to provide.

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Oct 15, 2026
@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 21, 2026
@xyuzh
xyuzh force-pushed the sandbox-runsc-gomaxprocs branch from 57ace48 to 8b6c3f5 Compare October 22, 2026 19:31
@xyuzh

xyuzh commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@pcmoritz Rebased onto master, with one change after your approval: master added three runsc calls since this was written (the exec-timeout kill --pid and trace procfs from #66986, and the reclaim pass's kill and delete from #67022). They now run with GOMAXPROCS=2 too, and test_exec_kill_and_reclaim_run_runsc_with_gomaxprocs checks each one with a fake runsc. On a CoreWeave pod it's 171 passed and 1 skipped against master's 168 and 1, plus 21 netns tests passed.

@pcmoritz
pcmoritz enabled auto-merge (squash) October 22, 2026 20:12
Every runsc CLI call (run, state, exec, kill, delete, spec) is a
short-lived Go process, and Go sizes its scheduler to every CPU it can
see. On a many-core node, dozens of concurrent `runsc exec` calls then
spend most of their CPU in the kernel rather than in the call.

Run them with GOMAXPROCS=2 unless the worker's environment sets it, as
gVisor's containerd shim does. runsc doesn't pass the variable on to the
sandbox and gofer processes it starts, so a sandbox's own CPU is
unaffected.

Signed-off-by: xyuzh <[email protected]>
@xyuzh
xyuzh force-pushed the sandbox-runsc-gomaxprocs branch from 8b6c3f5 to 991c41d Compare October 22, 2026 22:05
@github-actions
github-actions Bot disabled auto-merge October 22, 2026 22:05
xyuzh added 2 commits November 2, 2026 00:00
Master's checkpoint/restore (ray-project#66471) added `runsc pause`, `resume` and
`checkpoint` calls, which ran with Go's default GOMAXPROCS. They get
runsc_env() like every other runsc call. A restore's launch already
does, since it shares `runsc run`'s Popen.

Signed-off-by: xyuzh <[email protected]>
@xyuzh

xyuzh commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

@pcmoritz I merged master in 4b68467. On top of that, 0ae359f changes code you'd already approved: #66471 added runsc pause, resume and checkpoint calls, which ran with Go's default GOMAXPROCS. They now get runsc_env() like every other runsc call. A restore's launch already did, since it shares runsc run's Popen. test_lifecycle_commands_run_runsc_with_gomaxprocs covers all four, and it fails without the change.

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 365 passed, 15 skipped (master: 361 passed, 15 skipped).

xyuzh added 3 commits November 3, 2026 07:01
…bundles

Write an owner file that no live owner holds or names, rather than lock one
with _own_bundle and close it: the bundle reads as unowned whether owners
lock their owner files (now) or a lock that the file names (ray-project#67731).

Signed-off-by: xyuzh <[email protected]>
Signed-off-by: xyuzh <[email protected]>

ray-project#65898's runsc --version and runsc nvproxy list-supported-drivers calls came in with master and ran with every CPU's worth of GOMAXPROCS, like no other runsc call.
@xyuzh

xyuzh commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

@pcmoritz, two changes since your approval: I merged master (0a0587f, a one-line conflict with #67876's combine_output in exec_command), and e09774d passes runsc_env() to the two runsc calls that came in with #65898's GPU checks (runsc --version and runsc nvproxy list-supported-drivers), with test_gpu_checks_run_runsc_with_runsc_env. Real-gVisor suites: 478 passed, and the same two GPU tests fail as on master.

Premerge build 76502 failed in its init step: downloads from GitHub failed during GitHub's incident on 2026-11-09.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz
pcmoritz enabled auto-merge (squash) November 9, 2026 06:48
The jobs that failed in premerge build 76504 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:40
premerge build 76506 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 added a commit that referenced this pull request Nov 9, 2026
## 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"). #66471's restore path has
its own copy of the 0.1 s loop. #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
#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.

---------

Signed-off-by: xyuzh <[email protected]>
Co-authored-by: Philipp Moritz <[email protected]>
Signed-off-by: xyuzh <[email protected]>

ray-project#67074 rewrote the start loop's runsc state call; it keeps runsc_env().
@pcmoritz
pcmoritz merged commit 91d9d86 into ray-project:master Nov 9, 2026
6 checks passed
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.

2 participants