Repository navigation
[core][sandbox] Cap a sandbox's writable rootfs with disk - #66988
Conversation
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]>
There was a problem hiding this comment.
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.
| 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}" |
There was a problem hiding this comment.
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.
| 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}" |
There was a problem hiding this comment.
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.
| disk_bytes = parse_memory_bytes(self.disk) or 0 | ||
| if disk_bytes < 0: | ||
| raise ValueError("disk must be non-negative") |
There was a problem hiding this comment.
Similarly, we should safely parse and cast the disk size to an integer here to ensure consistency and robustness against float values.
| 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit e6c1fb5. Configure here.
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
left a comment
There was a problem hiding this comment.
Nice, glad this is pretty easy to set on the gvisor side!
…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]>
…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

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
storagelimit that had nothing to map to.This PR adds a
diskcap:diskoncreate(),Sandbox,SandboxRuntime.create()andSandboxConfig, in the unitsmemorytakes ("10Gi", or bytes). Zero, the default, means no cap. The HTTP API takesresources.disk_limit_mb. The gRPC facade sets none, since the client SDK never requests disk for sandboxes.ImageManager.create_oci_specwrites the cap into gVisor'sdev.gvisor.spec.rootfs.options: size=Nannotation. runsc ignores the size in its--overlay2flag for a rootfs that annotations configure.diskneeds an overlay:readonly=False, or aworkdir, 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. OtherwiseSandboxConfigraisesValueErrorand the HTTP API answers 422.Writes past the cap fail with
ENOSPCinside the sandbox, the sandbox keeps running, and deleting files frees space./tmpcounts toward the cap and/dev/shmtoward memory. The docs get a "Cap disk use" section.dd if=/dev/zero of=/root/fill bs=1M count=2048in a writablepython:3.12-slimsandbox on a CoreWeave node:disk="1Gi"ddNo space left on deviceat 1 GiBdf -h /in the sandboxRelated issues
None filed. Found with SkyRL's Harbor environment for Ray Sandbox (skyrl-project/SkyRL#2467).
Additional information
test_disk_caps_the_writable_overlayandtest_disk_caps_a_readonly_sandbox_with_a_workdirwrite past a 64 MiB cap in real gVisor sandboxes, in/and/workspace, and expectENOSPC. Unit tests cover the annotation, thereadonly/workdirrule, the HTTP 422 and thedisk_limit_mbpass-through. All fail on master.TEST_SANDBOX=1 pytestinpython/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_helperalone: 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.pytest http/tests: 150 passed, 11 skipped (nomodal). pre-commit passes.