Skip to content

[core][sandbox] Reap network="public" helpers instead of leaving zombies - #66987

Merged
pcmoritz merged 3 commits into
ray-project:masterfrom
xyuzh:sandbox-public-reap
Oct 16, 2026
Merged

pcmoritz merged 3 commits into
ray-project:masterfrom
xyuzh:sandbox-public-reap

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Description

A network="public" sandbox's launch shell starts a namespace holder (unshare and its sleep) and slirp4netns, then exec'd runsc, which became their parent and never waits for them. A stopped sandbox's helpers were orphaned to the pod's init, and Anyscale's init, like many container entrypoints, doesn't reap orphans. So every public sandbox left three zombies under PID 1 until the pod restarted. SkyRL's SWE-bench Multimodal runs left about 300 per node.

Changes in backend/gvisor.py:

  • The shell stays the parent. It runs runsc in the background and waits for it, and an exit trap kills the holder's sleep (so unshare reaps it) and slirp4netns, then waits for both.
  • Teardown (_terminate_tree) gives the shell 2 s to exit on its own. It kills the process group only if the shell is still running, before reaping it, so the signal can't reach a group that reused the pid.
  • The holder, slirp4netns and runsc run under setpriv --pdeathsig KILL, so they die with the shell, which is what Ray kills when the owning worker exits.
  • create_sandbox checks for setpriv up front. The docs ask for util-linux 2.33 or later, which added --pdeathsig.

On an Anyscale node whose PID 1 doesn't reap orphans:

master This PR
New zombies after 5 public sandboxes are created and deleted 16 0
15 s after ray.kill of a public sandbox's owner, without delete Holder still running Nothing running

#67022, stacked on this PR, reclaims the runsc state and bundle that a killed owner leaves.

Related issues

None filed. Found with SkyRL's Harbor environment for Ray Sandbox (skyrl-project/SkyRL#2467).

Additional information

  • Not a duplicate: no open PR changes how these helpers are reaped (I searched "sandbox zombie", "slirp4netns zombie" and "sandbox reap"). [core][sandbox] Boot sandboxes from an overlayfs rootfs if required #66470, [core][sandbox] Add GPU support to Ray Sandboxes via CDI #65898 and [core][sandbox] Add checkpoint/restore, pause/resume support using runsc #66471 also edit the end of the public launch script, so whichever lands later needs the same change.
  • Tests: test_netns_teardown_leaves_no_zombies and test_netns_launch_shell_death_stops_the_sandbox make the test process a child subreaper, so a helper the backend fails to reap becomes the test's own zombie. Both fail on master. test_terminate_tree_kills_the_group_only_before_reaping_it needs no gVisor and fails on the first commit.
  • Tested in a CoreWeave pod (Ubuntu 22.04, runsc release-20261005.0, Ray nightly) with TEST_SANDBOX=1 pytest --import-mode=importlib in python/ray/experimental/sandbox/tests: test_gvisor_backend.py -k netns: 11 passed; test_gvisor_backend.py test_image_manager.py test_image_utils.py test_sandbox_config.py test_sandbox_runtime.py -k "not netns": 89 passed, 1 skipped (test_create_sandbox_helper deselected, since it starts its own Ray). pre-commit passes.
  • I used Claude Code to help write and test this change.

A network="public" sandbox starts a namespace holder and slirp4netns from
a launch shell that then exec'd runsc. That made runsc their parent, so
when the sandbox stopped they were orphaned to the pod's init, which often
never reaps. Each sandbox left three zombies (unshare, its sleep, and
slirp4netns), and thousands of RL trials exhaust the process table.

The launch shell now stays the parent of the helpers and of runsc, and an
exit trap kills and reaps the helpers. Teardown gives the shell a moment
to exit on its own before it kills the process group. Ray kills a dead
worker's child processes, which is now only the shell, so `setpriv
--pdeathsig` makes every process the shell started die with it.

Signed-off-by: xyuzh <[email protected]>
@xyuzh
xyuzh marked this pull request as ready for review October 14, 2026 16:45
@xyuzh
xyuzh requested review from a team as code owners October 14, 2026 16:45

@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 improves sandbox process lifecycle management and cleanup under network="public" by utilizing setpriv --pdeathsig and a shell exit trap to ensure helper processes (like slirp4netns and runsc) are properly reaped and do not become zombies. It also updates the documentation and validation to require setpriv. Feedback on the changes suggests checking for the existence of the prctl attribute on libc before calling it in the test suite to prevent crashes on non-Linux platforms.

Comment on lines +713 to +715
libc = ctypes.CDLL(None, use_errno=True)
if libc.prctl(_PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) != 0:
pytest.skip("prctl(PR_SET_CHILD_SUBREAPER) isn't available")

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

On non-Linux platforms (such as macOS), libc does not have the prctl symbol, which will cause libc.prctl to raise an AttributeError and crash the test suite during execution/collection instead of gracefully skipping. We should check if prctl is available on libc before calling it.

Suggested change
libc = ctypes.CDLL(None, use_errno=True)
if libc.prctl(_PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) != 0:
pytest.skip("prctl(PR_SET_CHILD_SUBREAPER) isn't available")
libc = ctypes.CDLL(None, use_errno=True)
if not hasattr(libc, "prctl") or libc.prctl(_PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) != 0:
pytest.skip("prctl(PR_SET_CHILD_SUBREAPER) isn't available")

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.

Not changing this: _subreaper() can't run off Linux. It only runs in tests that boot real gVisor sandboxes, and conftest's session-wide ensure_runsc skips every test in this directory when runsc isn't on PATH and ci/env/install-runsc.sh can't install it, which it refuses to do on non-Linux hosts. Both callers here also take ensure_slirp4netns, which skips unless a real network="public" sandbox comes up. On Linux, libc always has prctl.

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

xyuzh added 2 commits October 14, 2026 22:05
… leader

_terminate_tree waited for the launch process, which reaps it, and then
killed its process group. Once the leader is reaped and nothing else is
in the group, its pid, which is also the group id, can go to a new
process, so the SIGKILL could reach an unrelated process group. The
create path had the same order: poll() reaps a launch process that
exits early.

A launch process that exits on its own leaves an empty group: runsc
starts its sandbox and gofer in sessions of their own, and the public
launch shell reaps its helpers before it exits. So teardown now kills the
group only when the launch process is still running after the grace
period, while it's unreaped.

Signed-off-by: xyuzh <[email protected]>
The launch script now runs its helpers under `setpriv --pdeathsig`,
which util-linux added in 2.33. RHEL 8 images ship 2.32, where a
network="public" sandbox fails to start with "unrecognized option
'--pdeathsig'", so the requirement can't just say util-linux.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 15, 2026
create_sandbox read the namespaces that set_egress_allowlist checks from
the launch process, which only works while the network="public" launch
shell execs into nsenter. ray-project#66987 keeps that shell as the parent of runsc,
in the worker's own namespaces, so every later update would have been
refused.

Read them instead from the holder that netns.pid names, right after the
sandbox starts, while that holder runs its network. A pid in the worker's
own user or network namespace isn't the holder, so it's left unknown, and
updates are refused.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 16, 2026
@pcmoritz
pcmoritz enabled auto-merge (squash) October 16, 2026 20:44
@pcmoritz
pcmoritz merged commit ce6ff7c into ray-project:master Oct 16, 2026
8 checks passed
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 19, 2026
create_sandbox read the namespaces that set_egress_allowlist checks from
the launch process, which only works while the network="public" launch
shell execs into nsenter. ray-project#66987 keeps that shell as the parent of runsc,
in the worker's own namespaces, so every later update would have been
refused.

Read them instead from the holder that netns.pid names, right after the
sandbox starts, while that holder runs its network. A pid in the worker's
own user or network namespace isn't the holder, so it's left unknown, and
updates are refused.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 19, 2026
…ay-project#66987

ray-project#66987 changed how the network="public" launch script starts runsc: the
shell now runs it in the background under setpriv --pdeathsig and waits,
instead of exec'ing nsenter. The ruleset-order test looked for the old
exec line, so it now looks for the new one.

Signed-off-by: xyuzh <[email protected]>
pcmoritz added a commit that referenced this pull request Oct 20, 2026
…aves (#67022)

## Description

When the Ray worker that owns a sandbox crashed or was OOM-killed, the
sandbox kept running with nobody to delete it: its launch process had a
session of its own, out of reach of the raylet's process-group cleanup,
and its TTL died with the actor. However the owner died, its runsc
state, bundle (with the overlay's backing file) and image-cache pin
stayed on the node, and the pin kept the image from being evicted.

In `backend/gvisor.py`:

- The launch process stays in its owner's process group (no
`start_new_session`), so when the raylet kills a dead worker's group,
the sandbox goes too: runsc's sandbox through runsc's parent-death
signal, a `public` sandbox's helpers through #66987's `setpriv
--pdeathsig`. Teardown kills only the launch process.
- Every launch command starts with `trap '' INT QUIT`, so a Ctrl-C in
the creator's terminal, or a Jupyter kernel interrupt, still leaves its
sandboxes running, as on master. A SIGQUIT (Ctrl-\\) still stops them,
since runsc is a Go program and installs its own handler.
- Each bundle holds an `.owner` file that the owning backend keeps
`flock`ed, and the kernel drops the lock when the owner dies.
`create_sandbox` starts a background pass, at most once a minute per
process, that reclaims every bundle whose lock is free: it kills a
sandbox still running there, runs `runsc delete -force`, releases the
image pin and removes the bundle, owner file last. The file is locked
before it gets its name, so a bundle being created is never taken, and
it holds flags rather than a command line, so a planted file can't make
the reclaim run anything. Start with `_own_bundle` and
`_reclaim_if_unowned`.
- A create that fails before its sandbox starts deletes its bundle at
once.

A sandbox now stops with the worker that created it. One created outside
a Ray worker, such as by a driver script, outlives its creator until a
later reclaim pass stops it, and `sandboxes.md` says so.

Sandbox processes 3 s and 15 s after its owner's worker is SIGKILLed,
without `delete`:

| | #66987 | This PR |
| --- | --- | --- |
| `network="none"` | 3 and 3 | 0 and 0 |
| `network="public"` | 5 and 5 | 0 and 0 |
| runsc state, bundle and image pin after the next create | Still there
| Gone |

## Related issues

None filed. Found with SkyRL's Harbor environment for Ray Sandbox
(skyrl-project/SkyRL#2467). Its SWE-bench Multimodal report notes both
leaks.

## Additional information

- Not a duplicate: no open PR ties a sandbox to its owner or reclaims a
dead owner's files (I searched "sandbox orphan", "owner", "crash",
"leak", "reclaim", "process group" and "TTL"). My #66932 also deletes
dead owners' files for sandboxes with limits, and #66469 also deletes a
failed create's bundle; whichever lands second drops its copy. #66471
and #66470 add launch paths that still use `start_new_session`.
- Builds on #66987, which is merged.
- Tests: four gVisor tests kill an owner with its group (as the raylet
does) or alone (as a driver can die), for `none` and `public`, and check
that the sandbox stops and the reclaim deletes its files and pin. Two
more (`test_owner_interrupt_leaves_its_sandbox_running` and its `netns`
twin) send the owner's group SIGINT, then SIGTERM: the sandbox must keep
running, network included, and then stop. Six unit tests cover which
bundles a reclaim takes, what it runs, the order it deletes in, failed
creates, the background pass and teardown. All fail on #66987.
- Tested in a CoreWeave pod (Ubuntu 22.04, runsc release-20261012.0, Ray
nightly), rebased on master afd4ccf, with `TEST_SANDBOX=1 pytest
--import-mode=importlib` in `python/ray/experimental/sandbox/tests`, on
`test_gvisor_backend.py test_image_manager.py test_image_utils.py
test_sandbox_config.py test_sandbox_runtime.py`: `-k netns`: 16 passed;
`-k "not netns"`: 120 passed, 1 skipped (`test_create_sandbox_helper`
deselected; it passes on its own). pre-commit passes.
- I used Claude Code to help write and test this change.

---------

Signed-off-by: xyuzh <[email protected]>
Signed-off-by: Philipp Moritz <[email protected]>
Co-authored-by: Philipp Moritz <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 20, 2026
create_sandbox read the namespaces that set_egress_allowlist checks from
the launch process, which only works while the network="public" launch
shell execs into nsenter. ray-project#66987 keeps that shell as the parent of runsc,
in the worker's own namespaces, so every later update would have been
refused.

Read them instead from the holder that netns.pid names, right after the
sandbox starts, while that holder runs its network. A pid in the worker's
own user or network namespace isn't the holder, so it's left unknown, and
updates are refused.

Signed-off-by: xyuzh <[email protected]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 20, 2026
…ay-project#66987

ray-project#66987 changed how the network="public" launch script starts runsc: the
shell now runs it in the background under setpriv --pdeathsig and waits,
instead of exec'ing nsenter. The ruleset-order test looked for the old
exec line, so it now looks for the new one.

Signed-off-by: xyuzh <[email protected]>
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Nov 2, 2026
…ies (ray-project#66987)

## Description

A `network="public"` sandbox's launch shell starts a namespace holder
(`unshare` and its `sleep`) and `slirp4netns`, then exec'd runsc, which
became their parent and never waits for them. A stopped sandbox's
helpers were orphaned to the pod's init, and Anyscale's init, like many
container entrypoints, doesn't reap orphans. So every `public` sandbox
left three zombies under PID 1 until the pod restarted. SkyRL's
SWE-bench Multimodal runs left about 300 per node.

Changes in `backend/gvisor.py`:

- The shell stays the parent. It runs runsc in the background and waits
for it, and an exit trap kills the holder's `sleep` (so `unshare` reaps
it) and `slirp4netns`, then waits for both.
- Teardown (`_terminate_tree`) gives the shell 2 s to exit on its own.
It kills the process group only if the shell is still running, before
reaping it, so the signal can't reach a group that reused the pid.
- The holder, `slirp4netns` and runsc run under `setpriv --pdeathsig
KILL`, so they die with the shell, which is what Ray kills when the
owning worker exits.
- `create_sandbox` checks for `setpriv` up front. The docs ask for
util-linux 2.33 or later, which added `--pdeathsig`.

On an Anyscale node whose PID 1 doesn't reap orphans:

| | master | This PR |
| --- | --- | --- |
| New zombies after 5 `public` sandboxes are created and deleted | 16 |
0 |
| 15 s after `ray.kill` of a `public` sandbox's owner, without `delete`
| Holder still running | Nothing running |

ray-project#67022, stacked on this PR, reclaims the runsc state and bundle that a
killed owner leaves.

## Related issues

None filed. Found with SkyRL's Harbor environment for Ray Sandbox
(skyrl-project/SkyRL#2467).

## Additional information

- Not a duplicate: no open PR changes how these helpers are reaped (I
searched "sandbox zombie", "slirp4netns zombie" and "sandbox reap").
ray-project#66470, ray-project#65898 and ray-project#66471 also edit the end of the `public` launch
script, so whichever lands later needs the same change.
- Tests: `test_netns_teardown_leaves_no_zombies` and
`test_netns_launch_shell_death_stops_the_sandbox` make the test process
a child subreaper, so a helper the backend fails to reap becomes the
test's own zombie. Both fail on master.
`test_terminate_tree_kills_the_group_only_before_reaping_it` needs no
gVisor and fails on the first commit.
- Tested in a CoreWeave pod (Ubuntu 22.04, runsc release-20261005.0, Ray
nightly) with `TEST_SANDBOX=1 pytest --import-mode=importlib` in
`python/ray/experimental/sandbox/tests`: `test_gvisor_backend.py -k
netns`: 11 passed; `test_gvisor_backend.py test_image_manager.py
test_image_utils.py test_sandbox_config.py test_sandbox_runtime.py -k
"not netns"`: 89 passed, 1 skipped (`test_create_sandbox_helper`
deselected, since it starts its own Ray). pre-commit passes.
- I used Claude Code to help write and test this change.

---------

Signed-off-by: xyuzh <[email protected]>
Signed-off-by: Pawel Beza <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4b9df5c4-6ec3-4e1f-ac3e-54f320ebd8c4
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Nov 2, 2026
…aves (ray-project#67022)

## Description

When the Ray worker that owns a sandbox crashed or was OOM-killed, the
sandbox kept running with nobody to delete it: its launch process had a
session of its own, out of reach of the raylet's process-group cleanup,
and its TTL died with the actor. However the owner died, its runsc
state, bundle (with the overlay's backing file) and image-cache pin
stayed on the node, and the pin kept the image from being evicted.

In `backend/gvisor.py`:

- The launch process stays in its owner's process group (no
`start_new_session`), so when the raylet kills a dead worker's group,
the sandbox goes too: runsc's sandbox through runsc's parent-death
signal, a `public` sandbox's helpers through ray-project#66987's `setpriv
--pdeathsig`. Teardown kills only the launch process.
- Every launch command starts with `trap '' INT QUIT`, so a Ctrl-C in
the creator's terminal, or a Jupyter kernel interrupt, still leaves its
sandboxes running, as on master. A SIGQUIT (Ctrl-\\) still stops them,
since runsc is a Go program and installs its own handler.
- Each bundle holds an `.owner` file that the owning backend keeps
`flock`ed, and the kernel drops the lock when the owner dies.
`create_sandbox` starts a background pass, at most once a minute per
process, that reclaims every bundle whose lock is free: it kills a
sandbox still running there, runs `runsc delete -force`, releases the
image pin and removes the bundle, owner file last. The file is locked
before it gets its name, so a bundle being created is never taken, and
it holds flags rather than a command line, so a planted file can't make
the reclaim run anything. Start with `_own_bundle` and
`_reclaim_if_unowned`.
- A create that fails before its sandbox starts deletes its bundle at
once.

A sandbox now stops with the worker that created it. One created outside
a Ray worker, such as by a driver script, outlives its creator until a
later reclaim pass stops it, and `sandboxes.md` says so.

Sandbox processes 3 s and 15 s after its owner's worker is SIGKILLed,
without `delete`:

| | ray-project#66987 | This PR |
| --- | --- | --- |
| `network="none"` | 3 and 3 | 0 and 0 |
| `network="public"` | 5 and 5 | 0 and 0 |
| runsc state, bundle and image pin after the next create | Still there
| Gone |

## Related issues

None filed. Found with SkyRL's Harbor environment for Ray Sandbox
(skyrl-project/SkyRL#2467). Its SWE-bench Multimodal report notes both
leaks.

## Additional information

- Not a duplicate: no open PR ties a sandbox to its owner or reclaims a
dead owner's files (I searched "sandbox orphan", "owner", "crash",
"leak", "reclaim", "process group" and "TTL"). My ray-project#66932 also deletes
dead owners' files for sandboxes with limits, and ray-project#66469 also deletes a
failed create's bundle; whichever lands second drops its copy. ray-project#66471
and ray-project#66470 add launch paths that still use `start_new_session`.
- Builds on ray-project#66987, which is merged.
- Tests: four gVisor tests kill an owner with its group (as the raylet
does) or alone (as a driver can die), for `none` and `public`, and check
that the sandbox stops and the reclaim deletes its files and pin. Two
more (`test_owner_interrupt_leaves_its_sandbox_running` and its `netns`
twin) send the owner's group SIGINT, then SIGTERM: the sandbox must keep
running, network included, and then stop. Six unit tests cover which
bundles a reclaim takes, what it runs, the order it deletes in, failed
creates, the background pass and teardown. All fail on ray-project#66987.
- Tested in a CoreWeave pod (Ubuntu 22.04, runsc release-20261012.0, Ray
nightly), rebased on master afd4ccf, with `TEST_SANDBOX=1 pytest
--import-mode=importlib` in `python/ray/experimental/sandbox/tests`, on
`test_gvisor_backend.py test_image_manager.py test_image_utils.py
test_sandbox_config.py test_sandbox_runtime.py`: `-k netns`: 16 passed;
`-k "not netns"`: 120 passed, 1 skipped (`test_create_sandbox_helper`
deselected; it passes on its own). pre-commit passes.
- I used Claude Code to help write and test this change.

---------

Signed-off-by: xyuzh <[email protected]>
Signed-off-by: Philipp Moritz <[email protected]>
Co-authored-by: Philipp Moritz <[email protected]>
Signed-off-by: Pawel Beza <[email protected]>
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4b9df5c4-6ec3-4e1f-ac3e-54f320ebd8c4
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