Skip to content

[core][sandbox] Hold one owner lock per process, not two files per sandbox - #67731

Open
xyuzh wants to merge 9 commits into
ray-project:masterfrom
xyuzh:sandbox-flat-fds
Open

xyuzh wants to merge 9 commits into
ray-project:masterfrom
xyuzh:sandbox-flat-fds

Conversation

@xyuzh

@xyuzh xyuzh commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Description

A process that hosts sandboxes kept two files open for each one: the bundle's locked .owner file and runsc's stderr log. Every runsc command the process runs forks it, and a fork copies the whole descriptor table, so each command started more slowly the more sandboxes the process hosted. Warm pools make one process host hundreds.

On a 352-CPU node, with 512 sandboxes in one process (1,028 open files), true ran at:

threads master this PR
32 499/s 753/s
64 435/s 927/s
128 461/s 950/s

Two interleaved runs each. With this PR the process held 5 files. Creating the 512 took 38 s either way.

This PR:

  • keeps one lock file per process in /tmp/ray/sandbox/.owners, locked for as long as the process lives, and has each bundle's owner file name it. A bundle's owner is gone once that lock is free. A forked child closes its copies of the lock, so it can't keep the lock held after the owner dies;
  • closes the stderr log once the sandbox runs, since the launch tree writes it through its own descriptors;
  • when a delete or a failed create leaves a bundle because its cgroup won't go, clears the lock from the owner file, so the reclaim pass retries it, as before;
  • has reclaim passes delete the locks of processes that are gone, once no bundle names them. An owner file whose lock is gone anyway falls back on the PID in the lock's name, and an owner that can't be checked counts as alive, because reclaiming a bundle kills its sandbox.

Bundles that #67022's format left behind are still reclaimed. No release has that format, but a Ray from before this PR would take this PR's bundles for unowned, so don't run both on one node.

Related issues

None.

Additional information

  • Not a duplicate: the open PRs that "sandbox owner file", "sandbox owner lock" and "sandbox file descriptors" find ([core][sandbox] Skip repeated work on each sandbox create #67076, [core][sandbox] Restore sandbox checkpoints on any node from shared storage #67485) are about other things.
  • Tests: test_an_owner_is_alive_while_its_process_runs (a real process holds the lock, then exits) and test_a_process_holds_one_descriptor_for_all_its_sandboxes; the reclaim and cgroup tests now orphan bundles through a process that has exited. test_a_dead_owners_lock_stays_while_a_bundle_names_it: a bundle that a reclaim pass couldn't delete keeps its dead owner's lock, so a reused PID can't make it look owned. test_an_owner_that_dies_during_the_lock_sweep_keeps_its_lock and test_a_forked_child_doesnt_hold_its_parents_owner_lock cover the two review fixes. At bc509ce, with master 81a7e99 merged in, the real-gVisor suites in a privileged container (aarch64, runsc release-20261026.0, erofs-utils 1.9.4), test_cgroup.py and test_resource_limits.py included: 476 passed (test_overlayfs.py in a session of its own), 25 skipped; master a83e55e: 473 passed, and the same two GPU tests fail, since that container's Ray predates get_cdi_kind. On macOS, the same failures as master, plus 2 passes. pre-commit passes.
  • I used Claude Code to help write and test this change.

…ndbox

A process that hosted sandboxes kept two files open for each: the bundle's
locked owner file and runsc's stderr log. Every runsc command it runs forks
the process, and a fork copies the descriptor table, so the more sandboxes
a process hosted, the slower each command started. With 512 sandboxes (1,028
open files), 64 threads ran 435 commands a second in them; with the files
closed, 927.

Each process now keeps one lock file in /tmp/ray/sandbox/.owners locked for
as long as it lives, and each bundle's owner file names it. The stderr log
is closed once the sandbox runs: the launch tree writes it through its own
descriptors. An owner that can't delete a bundle, because its cgroup won't
go, clears the lock from its owner file, so the reclaim pass retries it as
before. Reclaim passes delete the locks of processes that are gone; an owner
file that names a deleted lock falls back on the PID in the lock's name.

Signed-off-by: xyuzh <[email protected]>
@xyuzh
xyuzh requested a review from a team as a code owner November 3, 2026 07:44
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Nov 3, 2026
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 3, 2026
…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]>
xyuzh added a commit to xyuzh/ray that referenced this pull request Nov 3, 2026
…mpty directory

The sandbox directory can hold more than bundles (ray-project#67731 adds the owner
locks' directory), so look for bundle directories.

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

@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 using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 42bf898. Configure here.

xyuzh added 6 commits November 8, 2026 21:55
A reclaim pass deleted the locks of dead owners at its end, even when it
had left one of their bundles, say behind a cgroup it couldn't remove.
The next pass then went by the PID in the lock's name, and once another
process had that PID, it took the bundle for owned and never retried
it. Now the pass keeps the locks that bundles on the node still name.

Signed-off-by: xyuzh <[email protected]>
Microcheck build 58554 failed in its init step: downloads from GitHub failed during GitHub's incident on 2026-11-09.

Signed-off-by: xyuzh <[email protected]>
The jobs that failed in microcheck build 58564 failed only on downloads from GitHub during GitHub's incident on 2026-11-09.

Signed-off-by: xyuzh <[email protected]>
microcheck build 58579 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]>
GitHub delivered 0ae9fa4's push webhook two hours late, so Buildkite canceled bc509ce's build (58597) for one of the older commit.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Nov 9, 2026
Comment on lines +608 to +610
name = owner.get("owner_lock") if isinstance(owner, dict) else None
if name is None:
return False # Let go of by its owner (see _disown_bundle).

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.

What happens during a rolling upgrade or multi-virtualenv setup sharing /tmp/ray/sandbox? Pre-PR workers flock post-PR .owner files and kill live sandboxes, while post-PR _cpus_in_use treats pre-PR .owner files (missing 'owner_lock') as dead and double-assigns their CPUs.

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. The format this replaces is #67022's, which is only on master: no release has it (2.59.2 doesn't), so an upgrade between releases never sees both. On nightlies, restarting Ray on a node stops the old workers and their sandboxes first, and the new code still reclaims the bundles they leave. It can't reclaim a live old-format bundle, because reclaim first takes the flock on the owner file, which an old owner holds. Two installs from before and after this PR running sandboxes on one node at once would misread each other as you describe, and the description says not to do that.

Comment on lines +681 to +693
def _owner_locks_named() -> Set[str]:
"""The owner locks that the bundles on this node name."""
names = set()
for root_dir in glob.glob(os.path.join(_RAY_SANDBOX_DIR, "ray-sandbox-*")):
try:
with open(os.path.join(root_dir, _OWNER_FILE), encoding="utf-8") as f:
owner = json.load(f)
except (OSError, ValueError):
continue
name = owner.get("owner_lock") if isinstance(owner, dict) else None
if isinstance(name, str):
names.add(name)
return names

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.

Have you considered nesting bundles under a per-owner directory (_RAY_SANDBOX_DIR/owner-/ray-sandbox- with one held flock on owner-/.lock), which keeps 1 FD per process while avoiding .owners/ cross-directory GC, double .owner scans in _reclaim_unowned_bundles, and the _pid_running fallback?

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. Nesting would drop the .owners sweep, but it makes a bundle's path depend on its owner: _disown_bundle, which hands a bundle to the reclaim pass by rewriting one file, would have to move the directory, and everything that finds bundles as ray-sandbox-* under the sandbox directory would change with it. With the reordering in the first thread the sweep has no window left, and the PID fallback only applies to a lock that something outside Ray deleted.

Read the bundles' owner files after the owner locks are found free, not
before. An owner could write an owner file and die between the two, and
its lock was then deleted while a bundle still named it, which left the
bundle to the PID in the lock's name.

Close a forked child's copies of its parent's owner locks at the fork. A
lock is held while any copy of its file is open, so a child that outlived
the owner kept the owner's sandboxes from being reclaimed.

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.

Thanks for the review. 41baa3c fixes the sweep ordering and the fork case; the upgrade and layout questions are answered inline.

Comment on lines +608 to +610
name = owner.get("owner_lock") if isinstance(owner, dict) else None
if name is None:
return False # Let go of by its owner (see _disown_bundle).

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. The format this replaces is #67022's, which is only on master: no release has it (2.59.2 doesn't), so an upgrade between releases never sees both. On nightlies, restarting Ray on a node stops the old workers and their sandboxes first, and the new code still reclaims the bundles they leave. It can't reclaim a live old-format bundle, because reclaim first takes the flock on the owner file, which an old owner holds. Two installs from before and after this PR running sandboxes on one node at once would misread each other as you describe, and the description says not to do that.

Comment on lines +681 to +693
def _owner_locks_named() -> Set[str]:
"""The owner locks that the bundles on this node name."""
names = set()
for root_dir in glob.glob(os.path.join(_RAY_SANDBOX_DIR, "ray-sandbox-*")):
try:
with open(os.path.join(root_dir, _OWNER_FILE), encoding="utf-8") as f:
owner = json.load(f)
except (OSError, ValueError):
continue
name = owner.get("owner_lock") if isinstance(owner, dict) else None
if isinstance(name, str):
names.add(name)
return names

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. Nesting would drop the .owners sweep, but it makes a bundle's path depend on its owner: _disown_bundle, which hands a bundle to the reclaim pass by rewriting one file, would have to move the directory, and everything that finds bundles as ray-sandbox-* under the sandbox directory would change with it. With the reordering in the first thread the sweep has no window left, and the PID fallback only applies to a lock that something outside Ray deleted.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Nov 27, 2026

This branch has not been deployed

No deployments
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 stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants