Skip to content

[core][sandbox] Keep a free-space floor under the image cache - #67025

Merged
pcmoritz merged 2 commits into
ray-project:masterfrom
xyuzh:sandbox-image-cache-free-space
Oct 14, 2026
Merged

pcmoritz merged 2 commits into
ray-project:masterfrom
xyuzh:sandbox-image-cache-free-space

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Description

The image cache is capped at half of the filesystem that holds it, whatever else is on that disk. The cache shares the disk with sandboxes' writable overlays, logs and spilled objects. SkyRL's SWE-bench Multimodal report found nodes with 44 GB free, against about 115 GB that its 46 images take as EROFS. The cap doesn't account for free space at all. A disk that fills fails every sandbox on the node, and on Kubernetes it gets the node's Pods evicted.

This PR keeps a free-space floor under the cache:

  • Eviction also frees space. Before each pull, eviction now also removes unused images, least recently extracted first, until the cache's filesystem has a minimum of free space, besides fitting the size cap. Images that a running sandbox uses, and the image being pulled, are never evicted, as before.
  • The floor. RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES sets it. The default is a tenth of the filesystem, the level of the kubelet's default nodefs.available<10% eviction threshold. Setting both it and RAY_SANDBOX_IMAGE_CACHE_MAX_BYTES to 0 disables eviction.
  • Tests turn it off. The sandbox tests set the floor to 0 in conftest.py, so a full runner disk can't change their results. The laptop I tested on had 2% free.
  • Docs. The "Bound the image cache" section describes the floor.

Related issues

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

Additional information

  • Not a duplicate. I searched open PRs for "sandbox image cache", "sandbox disk", "sandbox eviction" and "erofs". None changes when the cache evicts. [core][sandbox] Move an image aside before deleting it #66469 changes how an evicted image's directory is deleted (rename first) in the same loop. Whichever lands second needs a small merge.

  • Experiment. Anyscale staging didn't start clusters on 2026-10-14, so I ran this as a pod on a CoreWeave Kubernetes cluster: rayproject/ray:nightly-py312 (Ray 3.0.0.dev0 1ba2dca), runsc release-20261005.0, erofs-utils 1.9.4. I pulled busybox:latest, which no sandbox uses, and alpine:3.20, pinned as a live sandbox's image. Then I set the floor one byte above the current free space, as on a disk that other data has filled, and pulled debian:bookworm-slim:

    master This PR
    busybox:latest (unused) still cached evicted (4.5 MB)
    alpine:3.20 (in use) still cached still cached
    debian:bookworm-slim pulled pulled
  • Tests. In the same pod, with TEST_SANDBOX=1 pytest --import-mode=importlib in python/ray/experimental/sandbox/tests:

    • test_gvisor_backend.py -k netns: 7 passed.
    • test_image_utils.py test_image_manager.py test_sandbox_config.py test_sandbox_runtime.py test_gvisor_backend.py -k "not netns": 90 passed, 1 skipped. test_create_sandbox_helper was deselected because it starts its own Ray driver.
    • The new tests are test_image_cache_eviction_keeps_a_free_space_floor and test_image_cache_min_free_bytes_default_and_env. The first works with no size cap: eviction frees space down to the floor, oldest first, and keeps an image in use even when the floor can't be met. Both fail on master.
    • Lint: the repo's pre-commit hooks (pre-commit 3.5.0: ruff, black, pydoclint) pass on commit.
  • Review follow-up (fb71212). As gemini suggested, the floor's test asserts the warning that an unparsable setting logs, and a redundant import is gone.

  • AI assistance (Claude Code) was used to prepare this change and run the tests above.

The image cache was capped at half of its filesystem, whatever else was
on the disk. The cache shares that disk with sandboxes' writable
overlays, logs and spilled objects. Some of SkyRL's SWE-bench Multimodal
nodes had 44 GB free, less than the ~115 GB that its 46 images take as
EROFS, and the cap didn't account for it. A full disk fails every
sandbox on the node and gets Pods evicted.

Before each pull, eviction now also frees unused images, least recently
extracted first, until the filesystem has a minimum of free space:
RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES, by default a tenth of the
filesystem, the level at which the kubelet starts evicting Pods. Images
in use are never evicted, as before. Setting both this and
RAY_SANDBOX_IMAGE_CACHE_MAX_BYTES to 0 disables eviction. The sandbox
tests turn the floor off, so a full runner disk can't change them.

Signed-off-by: xyuzh <[email protected]>
@xyuzh
xyuzh requested review from a team as code owners October 14, 2026 18:08

@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 minimum free space floor configuration (RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES) for the sandbox image cache, defaulting to a tenth of the filesystem capacity. When the free space falls below this threshold, the least recently used images are evicted to prevent disk exhaustion. The changes include updates to the eviction logic, documentation, and corresponding unit tests. Feedback on the changes suggests minor improvements to the tests: asserting that a warning is correctly logged when an invalid environment variable value is parsed (to avoid polluting test logs), and removing a redundant import of the os module in one of the test files.

Comment on lines +288 to +289
monkeypatch.setenv("RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES", "5G")
assert image_cache_min_free_bytes(str(tmp_path)) == tenth

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

In test_image_cache_min_free_bytes_default_and_env, when testing the invalid env value '5G', a warning is logged. To prevent this warning from polluting the test output and to verify that the warning is indeed logged correctly, we should patch logger.warning and assert its call, similar to how it is done in test_image_cache_max_bytes_default_and_env below.

Suggested change
monkeypatch.setenv("RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES", "5G")
assert image_cache_min_free_bytes(str(tmp_path)) == tenth
monkeypatch.setenv("RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES", "5G")
with patch(
"ray.experimental.sandbox._internal.image_utils.logger.warning"
) as warning:
assert image_cache_min_free_bytes(str(tmp_path)) == tenth
warning.assert_called_once_with(
"Ignoring %s=%r: expected an integer number of bytes.",
"RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES",
"5G",
)

Comment on lines +661 to +662
import os
import time

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

The os module is already imported at the top of test_image_manager.py (line 3). Importing it again inside the test function is redundant. You can remove import os and only import time here.

Suggested change
import os
import time
import time

Review follow-up: the test now asserts the warning that an unparsable
RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES logs, as the cap's test does, and
drops a redundant import.

Signed-off-by: xyuzh <[email protected]>
@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
@pcmoritz
pcmoritz enabled auto-merge (squash) October 14, 2026 21:51
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Oct 14, 2026
@pcmoritz
pcmoritz merged commit 315a0d9 into ray-project:master Oct 14, 2026
8 checks passed
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Nov 2, 2026
…oject#67025)

## Description

The image cache is capped at half of the filesystem that holds it,
whatever else is on that disk. The cache shares the disk with sandboxes'
writable overlays, logs and spilled objects. SkyRL's SWE-bench
Multimodal report found nodes with 44 GB free, against about 115 GB that
its 46 images take as EROFS. The cap doesn't account for free space at
all. A disk that fills fails every sandbox on the node, and on
Kubernetes it gets the node's Pods evicted.

This PR keeps a free-space floor under the cache:

- **Eviction also frees space.** Before each pull, eviction now also
removes unused images, least recently extracted first, until the cache's
filesystem has a minimum of free space, besides fitting the size cap.
Images that a running sandbox uses, and the image being pulled, are
never evicted, as before.
- **The floor.** `RAY_SANDBOX_IMAGE_CACHE_MIN_FREE_BYTES` sets it. The
default is a tenth of the filesystem, the level of the kubelet's default
`nodefs.available<10%` eviction threshold. Setting both it and
`RAY_SANDBOX_IMAGE_CACHE_MAX_BYTES` to `0` disables eviction.
- **Tests turn it off.** The sandbox tests set the floor to 0 in
`conftest.py`, so a full runner disk can't change their results. The
laptop I tested on had 2% free.
- **Docs.** The "Bound the image cache" section describes the floor.

## Related issues

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

## Additional information

- **Not a duplicate.** I searched open PRs for "sandbox image cache",
"sandbox disk", "sandbox eviction" and "erofs". None changes when the
cache evicts. ray-project#66469 changes how an evicted image's directory is deleted
(rename first) in the same loop. Whichever lands second needs a small
merge.
- **Experiment.** Anyscale staging didn't start clusters on 2026-10-14,
so I ran this as a pod on a CoreWeave Kubernetes cluster:
`rayproject/ray:nightly-py312` (Ray 3.0.0.dev0 1ba2dca), runsc
release-20261005.0, erofs-utils 1.9.4. I pulled `busybox:latest`, which
no sandbox uses, and `alpine:3.20`, pinned as a live sandbox's image.
Then I set the floor one byte above the current free space, as on a disk
that other data has filled, and pulled `debian:bookworm-slim`:

  | | master | This PR |
  | --- | --- | --- |
  | `busybox:latest` (unused) | still cached | evicted (4.5 MB) |
  | `alpine:3.20` (in use) | still cached | still cached |
  | `debian:bookworm-slim` | pulled | pulled |

- **Tests.** In the same pod, with `TEST_SANDBOX=1 pytest
--import-mode=importlib` in `python/ray/experimental/sandbox/tests`:
  - `test_gvisor_backend.py -k netns`: 7 passed.
- `test_image_utils.py test_image_manager.py test_sandbox_config.py
test_sandbox_runtime.py test_gvisor_backend.py -k "not netns"`: 90
passed, 1 skipped. `test_create_sandbox_helper` was deselected because
it starts its own Ray driver.
- The new tests are `test_image_cache_eviction_keeps_a_free_space_floor`
and `test_image_cache_min_free_bytes_default_and_env`. The first works
with no size cap: eviction frees space down to the floor, oldest first,
and keeps an image in use even when the floor can't be met. Both fail on
master.
- Lint: the repo's pre-commit hooks (pre-commit 3.5.0: ruff, black,
pydoclint) pass on commit.
- **Review follow-up (fb71212).** As gemini suggested, the floor's
test asserts the warning that an unparsable setting logs, and a
redundant import is gone.
- AI assistance (Claude Code) was used to prepare this change and run
the tests above.

---------

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.

2 participants