Skip to content

[core][sandbox] Cap a sandbox's writable rootfs with disk - #66988

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

pcmoritz merged 3 commits into
ray-project:masterfrom
xyuzh:sandbox-disk-limit

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Description

A writable sandbox keeps what it writes in a private overlay whose backing file sits on the node's disk, and nothing bounded it. In SkyRL's SWE-bench Multimodal runs, an 11 GiB write inside one sandbox succeeded, so one runaway build could fill the disk for every trial on the node. Harbor tasks declare a storage limit that had nothing to map to.

This PR adds a disk cap:

  • disk on create(), Sandbox, SandboxRuntime.create() and SandboxConfig, in the units memory takes ("10Gi", or bytes). Zero, the default, means no cap. The HTTP API takes resources.disk_limit_mb. The gRPC facade sets none, since the client SDK never requests disk for sandboxes.
  • ImageManager.create_oci_spec writes the cap into gVisor's dev.gvisor.spec.rootfs.options: size=N annotation. runsc ignores the size in its --overlay2 flag for a rootfs that annotations configure.
  • disk needs an overlay: readonly=False, or a workdir, since a readonly sandbox with an explicit workdir already runs on a private writable overlay. With a cap, that workdir stays in the overlay instead of a bind-mounted node directory that writes could fill past the cap. Otherwise SandboxConfig raises ValueError and the HTTP API answers 422.

Writes past the cap fail with ENOSPC inside the sandbox, the sandbox keeps running, and deleting files frees space. /tmp counts toward the cap and /dev/shm toward memory. The docs get a "Cap disk use" section.

dd if=/dev/zero of=/root/fill bs=1M count=2048 in a writable python:3.12-slim sandbox on a CoreWeave node:

master disk="1Gi"
dd 2 GiB written No space left on device at 1 GiB
df -h / in the sandbox size 8.0E size 1.0G
Overlay file on the node 2.0 GiB 1.0 GiB

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 matching "sandbox disk", "sandbox storage limit" or "gvisor overlay size" adds a disk cap. [core][sandbox] Boot sandboxes from an overlayfs rootfs if required #66470 moves the overlay annotation, so whichever lands second carries the size option over; its kernel-overlayfs boot path would need its own cap.
  • Tests: test_disk_caps_the_writable_overlay and test_disk_caps_a_readonly_sandbox_with_a_workdir write past a 64 MiB cap in real gVisor sandboxes, in / and /workspace, and expect ENOSPC. Unit tests cover the annotation, the readonly/workdir rule, the HTTP 422 and the disk_limit_mb pass-through. All fail on master.
  • Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils 1.9.4), TEST_SANDBOX=1 pytest in python/ray/experimental/sandbox:
    • tests/test_gvisor_backend.py tests/test_image_manager.py tests/test_image_utils.py tests/test_sandbox_config.py tests/test_sandbox_runtime.py -k "not netns": 99 passed, 1 skipped; test_create_sandbox_helper alone: 1 passed.
    • tests/test_gvisor_backend.py -k netns: 7 passed.
    • http/tests/test_http_app.py test_sandbox_host.py test_http_schemas.py test_http_integration.py: 82 passed.
  • Tested on macOS: pytest http/tests: 150 passed, 11 skipped (no modal). pre-commit passes.
  • I used Claude Code to help write and test this change.

A writable sandbox keeps everything it writes in a private overlay, a file
on the node's disk, and nothing bounded it: one sandbox could write until
the node's disk filled. `disk` (or `resources.disk_limit_mb` in the HTTP
API) now caps that overlay through gVisor's rootfs size annotation. Writes
past the cap fail with ENOSPC inside the sandbox, the sandbox keeps
running, and deleting files frees the space. The --overlay2 flag's size
doesn't apply to a rootfs that annotations configure, so the cap goes in
dev.gvisor.spec.rootfs.options.

A read-only rootfs has no overlay, and its writable workdir is a host
directory, so `disk` requires readonly=False.

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

@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 disk limit option to cap the disk usage of writable sandboxes, updating the Python API, gVisor backend, HTTP/gRPC schemas, and documentation, along with adding comprehensive unit tests. The review feedback suggests casting the parsed disk size to an integer in both image_manager.py and config.py to prevent potential float representations that could cause gVisor parsing failures.

Comment on lines +454 to +459
disk_bytes = parse_memory_bytes(disk) or 0
if disk_bytes > 0:
# gVisor sizes the overlay's upper layer from this annotation,
# so writes past it fail with ENOSPC. The --overlay2 flag's
# size doesn't apply to a rootfs that annotations configure.
annotations["dev.gvisor.spec.rootfs.options"] = f"size={disk_bytes}"

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 ensure robustness against None or float values (which can result in float representations like size=1073741824.0 that gVisor might fail to parse), we should safely parse and cast the disk size to an integer, and avoid calling parse_memory_bytes when disk is not set.

Suggested change
disk_bytes = parse_memory_bytes(disk) or 0
if disk_bytes > 0:
# gVisor sizes the overlay's upper layer from this annotation,
# so writes past it fail with ENOSPC. The --overlay2 flag's
# size doesn't apply to a rootfs that annotations configure.
annotations["dev.gvisor.spec.rootfs.options"] = f"size={disk_bytes}"
disk_bytes = int(parse_memory_bytes(disk) or 0) if disk else 0
if disk_bytes > 0:
# gVisor sizes the overlay's upper layer from this annotation,
# so writes past it fail with ENOSPC. The --overlay2 flag's
# size doesn't apply to a rootfs that annotations configure.
annotations["dev.gvisor.spec.rootfs.options"] = f"size={disk_bytes}"

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: parse_memory_bytes returns an int for every input it accepts (each branch ends in int(...)) and None only for None or a blank string, which or 0 already maps to no cap, so the annotation can't read size=1073741824.0. a1f3fb0 adds float(1 << 30) and "1.5Gi" cases to test_create_oci_spec_caps_the_writable_overlay to pin that.

Comment on lines +179 to +181
disk_bytes = parse_memory_bytes(self.disk) or 0
if disk_bytes < 0:
raise ValueError("disk must be non-negative")

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

Similarly, we should safely parse and cast the disk size to an integer here to ensure consistency and robustness against float values.

Suggested change
disk_bytes = parse_memory_bytes(self.disk) or 0
if disk_bytes < 0:
raise ValueError("disk must be non-negative")
disk_bytes = int(parse_memory_bytes(self.disk) or 0) if self.disk else 0
if disk_bytes < 0:
raise ValueError("disk must be non-negative")

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: disk_bytes is only compared with 0 here, and parse_memory_bytes already returns an int or None (which or 0 covers), so neither the cast nor the if self.disk guard changes a result. Malformed strings still raise ValueError, which test_disk_requires_a_writable_rootfs checks.

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

@ray-gardener ray-gardener Bot added docs An issue or change related to documentation core Issues that should be addressed in Ray Core labels Oct 14, 2026
xyuzh added 2 commits October 14, 2026 22:11
A readonly sandbox with an explicit workdir already runs on a private
writable overlay, a file on the node's disk, but disk was rejected for
it, so the default readonly=True setup with a workdir couldn't be capped.
disk now requires readonly=False or a workdir. With a cap, the workdir
stays in the overlay instead of a bind-mounted node directory, which
took writes past the cap.

Signed-off-by: xyuzh <[email protected]>
parse_memory_bytes returns an int for every input it accepts, so the
rootfs size annotation never reads like size=1073741824.0. Pin that for a
float and a fractional size.

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

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

Nice, glad this is pretty easy to set on the gvisor side!

@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 16, 2026
@pcmoritz
pcmoritz merged commit eb6a7eb into ray-project:master Oct 16, 2026
7 checks passed
xyuzh added a commit to xyuzh/SkyRL that referenced this pull request Oct 28, 2026
…disk caps, egress allowlist

Moves the pin to Ray master @ 78e4b1f713, which has every merged sandbox fix
and ray-project/ray#67444, so vLLM's Ray metrics no longer need ray[serve] in
SkyRL's env. Validated there with 64 SWE-bench Verified tasks (Harbor's
oracle and SkyRL's generator).

- Pin Ray to master @ 78e4b1f713 in pyproject.toml, uv.lock and the image.
- dockerfile-parse in the harbor extra: the environment reads the task
  Dockerfile's WORKDIR with Harbor's parse_dockerfile_workdir, which
  imports it, and harbor[daytona,modal] doesn't install it, so every task
  with a Dockerfile failed in the environment's constructor.
- The image installs nftables and util-linux: Ray sets up network="public"
  sandboxes with nft, nsenter and setpriv since ray-project/ray#66359.
  gVisor goes to release-20261019.0.
- The task's storage_mb caps the writable rootfs (ray-project/ray#66988).
- New cidr_allowlist kwarg, passed to Ray, for public sandboxes that must
  reach a host inside the cluster. The comment on "public" said it could
  reach the Ray GCS; since ray-project/ray#66359 it reaches the internet only.
- black reformats lines the file already had to the repo's 120 columns.

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

## Description

A writable sandbox keeps what it writes in a private overlay whose
backing file sits on the node's disk, and nothing bounded it. In SkyRL's
SWE-bench Multimodal runs, an 11 GiB write inside one sandbox succeeded,
so one runaway build could fill the disk for every trial on the node.
Harbor tasks declare a `storage` limit that had nothing to map to.

This PR adds a `disk` cap:

- `disk` on `create()`, `Sandbox`, `SandboxRuntime.create()` and
`SandboxConfig`, in the units `memory` takes (`"10Gi"`, or bytes). Zero,
the default, means no cap. The HTTP API takes `resources.disk_limit_mb`.
The gRPC facade sets none, since the client SDK never requests disk for
sandboxes.
- `ImageManager.create_oci_spec` writes the cap into gVisor's
`dev.gvisor.spec.rootfs.options: size=N` annotation. runsc ignores the
size in its `--overlay2` flag for a rootfs that annotations configure.
- `disk` needs an overlay: `readonly=False`, or a `workdir`, since a
readonly sandbox with an explicit workdir already runs on a private
writable overlay. With a cap, that workdir stays in the overlay instead
of a bind-mounted node directory that writes could fill past the cap.
Otherwise `SandboxConfig` raises `ValueError` and the HTTP API answers
422.

Writes past the cap fail with `ENOSPC` inside the sandbox, the sandbox
keeps running, and deleting files frees space. `/tmp` counts toward the
cap and `/dev/shm` toward memory. The docs get a "Cap disk use" section.

`dd if=/dev/zero of=/root/fill bs=1M count=2048` in a writable
`python:3.12-slim` sandbox on a CoreWeave node:

| | master | `disk="1Gi"` |
| --- | --- | --- |
| `dd` | 2 GiB written | `No space left on device` at 1 GiB |
| `df -h /` in the sandbox | size 8.0E | size 1.0G |
| Overlay file on the node | 2.0 GiB | 1.0 GiB |

## 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 matching "sandbox disk", "sandbox storage
limit" or "gvisor overlay size" adds a disk cap. ray-project#66470 moves the
overlay annotation, so whichever lands second carries the size option
over; its kernel-overlayfs boot path would need its own cap.
- Tests: `test_disk_caps_the_writable_overlay` and
`test_disk_caps_a_readonly_sandbox_with_a_workdir` write past a 64 MiB
cap in real gVisor sandboxes, in `/` and `/workspace`, and expect
`ENOSPC`. Unit tests cover the annotation, the `readonly`/`workdir`
rule, the HTTP 422 and the `disk_limit_mb` pass-through. All fail on
master.
- Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils
1.9.4), `TEST_SANDBOX=1 pytest` in `python/ray/experimental/sandbox`:
- `tests/test_gvisor_backend.py tests/test_image_manager.py
tests/test_image_utils.py tests/test_sandbox_config.py
tests/test_sandbox_runtime.py -k "not netns"`: 99 passed, 1 skipped;
`test_create_sandbox_helper` alone: 1 passed.
  - `tests/test_gvisor_backend.py -k netns`: 7 passed.
- `http/tests/test_http_app.py test_sandbox_host.py test_http_schemas.py
test_http_integration.py`: 82 passed.
- Tested on macOS: `pytest http/tests`: 150 passed, 11 skipped (no
`modal`). 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
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 docs An issue or change related to documentation 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