Skip to content

[core][sandbox] Make oom-score-adj configurable for each sandbox - #67688

Merged
andrewsykim merged 1 commit into
ray-project:masterfrom
pawelbeza:pawelbeza/sandbox-oom-score-adj
Nov 6, 2026
Merged

andrewsykim merged 1 commit into
ray-project:masterfrom
pawelbeza:pawelbeza/sandbox-oom-score-adj

Conversation

@pawelbeza

Copy link
Copy Markdown
Contributor

Today the recommended setup for production RL sandboxing is to have separate sandbox pool manager actor per ray node. This way we don't waste resources and have single controller for all sandboxes located on any node. One missing piece is that today there is no way to protect manager from being killed by linux OOM killer.

This PR adds an optional per-sandbox oom_score_adj setting so sandbox workloads can have a different OOM-killer priority from their manager.

A sandbox manager may protect itself from OOM killing by setting  oom_score_adj=-1000 . Without an explicit reset, the sandboxes it launches inherit that protection. Setting  oom_score_adj=0  in the sandbox launcher allows Linux to kill sandbox workloads when memory runs out, while keeping the manager protected.

@pawelbeza
pawelbeza requested a review from a team as a code owner November 2, 2026 14:12

@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 support for configuring the Linux OOM-killer score adjustment (oom_score_adj) for sandbox host processes. It refactors command wrapping into a reusable wrap_command function, updates the cgroup_launcher to apply the OOM score adjustment before executing workloads, and adds validation and comprehensive tests. The review feedback suggests simplifying the type validation for oom_score_adj in SandboxConfig using a direct type check, and improving test robustness by skipping the OOM priority test if /proc/self/oom_score_adj is not writable in the execution environment.

@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 f409790. Configure here.

@pawelbeza
pawelbeza force-pushed the pawelbeza/sandbox-oom-score-adj branch 2 times, most recently from 85a406a to 1731b3f Compare November 2, 2026 14:40
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Nov 2, 2026
Add an optional per-sandbox  oom_score_adj  setting so sandbox workloads can have a different OOM-killer priority from their manager.

A sandbox manager may protect itself from OOM killing by setting  oom_score_adj=-1000 . Without an explicit reset, the sandboxes it
launches inherit that protection. Setting  oom_score_adj=0  in the sandbox launcher allows Linux to kill sandbox workloads when memory
runs out, while keeping the manager protected.

Signed-off-by: Pawel Beza <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 61db4d37-071b-4f66-aa8b-99577a7f5ceb
@pawelbeza
pawelbeza force-pushed the pawelbeza/sandbox-oom-score-adj branch from 1731b3f to edb022f Compare November 3, 2026 12:16
@pawelbeza

pawelbeza commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@Kunchd just to simplify understanding how we actually use this feature with single pool manager actor per ray node

<pod>/
├── <main-container>/       # Ray/training processes
└── sandboxes/                    # Aggregate CPU/memory/I/O budget
    ├── manager/            # Manager actor; cpu.weight=10000
    ├── ray-sandbox-<id1>/   # Per-sandbox limits
    └── ray-sandbox-<id2>/

# Manager initialization:
budget = host.prepare(config.resources)  # Creates <pod>/sandboxes and sandboxes/manager
root = budget.root
(root / "manager" / "cgroup.procs").write_text(str(os.getpid()))
Path("/proc/self/oom_score_adj").write_text("-1000")
os.environ["RAY_SANDBOX_CGROUP_PATH"] = str(root)
runtime = SandboxRuntime()

# Sandbox creation:
sandbox = runtime.create(
    image,
    cpu=shape.cpu,
    memory=shape.memory,
    oom_score_adj=0,  # don't inherit the manager's OOM immunity.
    # ...other options...
)

@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Nov 5, 2026
@andrewsykim
andrewsykim merged commit 2f545e6 into ray-project:master Nov 6, 2026
7 checks passed
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 6, 2026
ray-project#67688 made exec, write_file and read_file start `runsc exec` through
the launcher to set the sandbox's OOM score, which also joined the
sandbox's cgroup again. They keep the OOM score and skip the cgroup:
_wrap_workload passes no cgroup to wrap_command. The OOM priority test
now expects only the launch in the cgroup.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 6, 2026
Signed-off-by: xyuzh <[email protected]>

ray-project#67688 added SandboxConfig.oom_score_adj, which the launch applies to the sandbox's processes, so a pooled sandbox can stand in only for creates that ask for the same score. test_boot_key_covers_every_boot_time_field caught it.
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 9, 2026
ray-project#67876 added exec's combine_output, which leaves stderr as None and
returns the merged bytes in ExecResult.output. The OOM note now goes at
the end of output with combine_output, and into stderr without it.
ray-project#67688 runs every runsc exec through the launcher's wrap_command, so the
unit test's stand-in cgroup no longer passes through _wrap_workload.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 9, 2026
…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.
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]>
pawelbeza added a commit to pawelbeza/ray that referenced this pull request Nov 9, 2026
…-project#67688)

Today the recommended setup for production RL sandboxing is to have
separate sandbox pool manager actor per ray node. This way we don't
waste resources and have single controller for all sandboxes located on
any node. One missing piece is that today there is no way to protect
manager from being killed by linux OOM killer.

This PR adds an optional per-sandbox `oom_score_adj` setting so sandbox
workloads can have a different OOM-killer priority from their manager.

A sandbox manager may protect itself from OOM killing by setting
 oom_score_adj=-1000 . Without an explicit reset, the sandboxes it
launches inherit that protection. Setting  oom_score_adj=0  in the
sandbox launcher allows Linux to kill sandbox workloads when memory runs
out, while keeping the manager protected.

Signed-off-by: Pawel Beza <[email protected]>
Co-authored-by: Pawel Beza <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 61db4d37-071b-4f66-aa8b-99577a7f5ceb
Signed-off-by: Pawel Beza <[email protected]>
Cherry-picked-from: 2f545e6
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 0baa3cf-af4e-472e-ad89-8f02c6e8be11
pcmoritz pushed a commit that referenced this pull request Nov 13, 2026
## Description

Every `exec`, `write_file` and `read_file` on a sandbox with resource
limits ran `runsc exec` through the cgroup launcher, which starts a
Python interpreter and moves it into the sandbox's cgroup first. The
command itself runs inside the sandbox's Sentry, which is already in
that cgroup. `runsc exec` only relays it, so the join limited nothing.

A cgroup migration takes a global lock and waits for an RCU grace
period. On a 352-thread NVIDIA Vera host, `exec("true")` in a limited
sandbox took 49 ms median with the join and 3 ms without, and because
the lock is global, every limited sandbox's commands on the node queued
behind each other.

This PR drops the join from those three calls. Sandbox launches still
start inside the cgroup, so CPU and memory limits apply as before. The
`runsc exec` relay process now counts against its caller's cgroup
instead of the sandbox's. A sandbox that sets `oom_score_adj` (#67688)
still gets that score on `runsc exec`: from util-linux's `choom` where
the node has it, and from the launcher otherwise. With `choom`, exec in
such a sandbox takes 3.1 ms on real gVisor, against 7.3 ms through the
launcher.

## Related issues

None.

## Additional information

- Not a duplicate: no open PR changes the per-exec cgroup join (searched
"sandbox cgroup exec" and "cgroup_launcher exec").
- Tests: new `test_commands_in_a_limited_sandbox_skip_the_cgroup_join`,
with and without an OOM score, and #67688's
`test_backend_applies_oom_priority_to_start_exec_and_file_io` now
expects only the launch in the cgroup. Both fail on master.
`test_choom_gives_a_command_the_sandboxs_oom_score` runs the real
`choom`. On a Vera node (aarch64, 64 KiB pages, delegated cgroup v2,
runsc release-20261026.0 built for 64 KiB pages), `TEST_SANDBOX=1
RAY_SANDBOX_CGROUP_PATH=<delegated> pytest tests/test_resource_limits.py
tests/test_cgroup.py`: 30 passed. On macOS, `test_gvisor_backend.py`,
`test_image_manager.py`, `test_sandbox_config.py`, `test_cgroup.py` and
`test_resource_limits.py` match master (162 passed against 161, the same
23 macOS-only failures). With master 11fe651 merged in
(363ed2b), the real-gVisor suites in a privileged container
(aarch64, runsc release-20261026.0, erofs-utils 1.9.4): 465 passed, 25
skipped, and the same two GPU tests fail as on master (463 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community 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.

4 participants