Skip to content

[core][sandbox] Cache an image once however its reference is spelled - #66985

Merged
pcmoritz merged 4 commits into
ray-project:masterfrom
xyuzh:sandbox-image-cache-key
Oct 19, 2026
Merged

pcmoritz merged 4 commits into
ray-project:masterfrom
xyuzh:sandbox-image-cache-key

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Description

The image cache named each entry after the literal image reference. python:3.12-slim, library/python:3.12-slim and docker.io/library/python:3.12-slim name the same image, but each one pulled it and built its own EROFS root filesystem. SkyRL's SWE-bench Multimodal tasks spell their images in different ways, and on a CoreWeave node each extra spelling of a 1.1 GiB image cost about a minute and 2.9 GiB of disk.

This PR names each entry after the normalized reference (registry, repository, and tag or digest, with Docker Hub written as docker.io), followed by a short hash of it. The spellings above all map to docker.io_library_python_3.12-slim-ea59dc550ffd7fa7.

  • The hash keeps apart references that look the same once /, : and @ become _, such as repo@sha256:<hex> and a tag named sha256_<hex>. Master's literal names had the same collisions.
  • The readable part stops at 128 characters, so a long reference stays under the file-name limit.
  • parse_image_ref lowercases the registry host. Docker.io/python used to be pulled from https://Docker.io and fail.
  • A local archive spelled as a path or ending in .tar keeps its file name, as before.

Existing entries have literal names, so after an upgrade each node pulls each image once more. The old entries are the least recently extracted, so eviction removes them first when the cache needs room.

Related issues

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

Additional information

  • Not a duplicate: searches for "sandbox image cache" and "sandbox image reference" found no open PR that normalizes references. [core][sandbox] Move an image aside before deleting it #66469, [core][sandbox] Boot sandboxes from an overlayfs rootfs if required #66470 and [core][sandbox] Add GPU support to Ray Sandboxes via CDI #65898 check entry names in this module, and the new names pass their check.
  • Tests: new tests cover one entry and one pull for the spellings, separate entries for colliding references, short names for long references, uppercase hosts, and releasing a pin after its relative archive path is deleted. The spelling, digest-versus-tag and host-case tests fail on master.
  • Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils 1.9.4): TEST_SANDBOX=1 pytest 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 and not test_create_sandbox_helper": 93 passed, 1 skipped (95 passed merged with master); -k netns: 9 passed. pre-commit passes.
  • I used Claude Code to help write and test this change.

The image cache named each entry after the literal reference, so
`python:3.12-slim`, `library/python:3.12-slim`, and
`docker.io/library/python:3.12-slim` each pulled the image and built its
EROFS root filesystem again, and each took its own disk. A large image
takes about a minute and a few GB per copy. Registry references are now
normalized before naming the entry, so every spelling shares one. Local
tar archives keep their file names.

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 introduces container image reference normalization in the sandbox image manager, ensuring that different spellings of the same image (such as with or without registry or library prefixes) share a single cache entry. This is implemented via a new image_cache_name helper function, with corresponding updates to the image manager, tests, and documentation. Feedback suggests normalizing the registry hostname to lowercase within image_cache_name to prevent potential cache misses from casing differences.

Comment on lines +195 to +197
registry, repo, reference = parse_image_ref(image)
if registry == "registry-1.docker.io":
registry = "docker.io"

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

Registry hostnames are case-insensitive according to the OCI and Docker distribution specifications, whereas repository names and tags/digests can be case-sensitive. To prevent potential cache misses due to casing differences in the registry hostname (e.g., Docker.io vs docker.io), we should normalize the registry hostname to lowercase before performing the comparison and constructing the cache name.

Suggested change
registry, repo, reference = parse_image_ref(image)
if registry == "registry-1.docker.io":
registry = "docker.io"
registry, repo, reference = parse_image_ref(image)
registry = registry.lower()
if registry == "registry-1.docker.io":
registry = "docker.io"

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.

Fixed in ff1fd44, in parse_image_ref rather than only in the cache name, so the pull goes to the same host the name says. Before, Docker.io/python wasn't recognized as Docker Hub and was pulled from https://Docker.io, which redirects to www.docker.com, so the pull failed. Only the host is lowercased: repository paths must already be lowercase, and tags are case-sensitive. test_parse_image_ref and test_image_cache_name_normalizes_registry_spellings cover it.

@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 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit d8e67df. Configure here.

) from err

safe_name = sanitize_image_name(image)
safe_name = image_cache_name(image)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pinned old cache entries never evict

Medium Severity

Cache directories are now named from the normalized reference, but eviction still skips any directory that has files under .users. Pins are released only through the new path, so a pre-upgrade directory left pinned by a sandbox that never released it is never deleted and keeps counting against the cache cap.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit d8e67df. Configure here.

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: a pin under an old name would never have been released anyway. A pin (<entry>/.users/<sandbox_id>) is removed only by the backend that created the sandbox, through its in-memory _sandbox_metadata. An upgrade restarts that process, so on master a pin taken before the upgrade also stays forever, whatever the entry is called, because _has_users doesn't check whether the owner is alive. Within one process, 0768a4c makes the name depend on the image string alone, so a pin and its release always agree; test_release_finds_the_pin_after_its_archive_is_deleted covers that.

xyuzh added 3 commits October 14, 2026 22:03
Host names are case-insensitive, but parse_image_ref kept the host as
written. GHCR.io/org/repo and ghcr.io/org/repo got separate cache
entries, and Docker.io/python wasn't recognized as Docker Hub: the pull
went to https://Docker.io, which redirects to www.docker.com, and failed.

Signed-off-by: xyuzh <[email protected]>
The cache name was the normalized reference with "/", ":" and "@" all
turned into "_", so different references could share an entry and the
later pull got the earlier one's image: repo@sha256:<hex> and a tag
named sha256_<hex>, or ghcr.io/a/b_c:1 and ghcr.io/a/b/c:1. Master's
literal names had the same collisions. A long reference also made a
name longer than the 255-byte limit on a file name.

Append a hash of the normalized reference, and cut the readable part to
128 characters.

The spelling test now stubs the registry instead of pulling busybox
from Docker Hub, and the remote pull test checks for the archive of the
entry it pulled rather than of the old literal name.

Signed-off-by: xyuzh <[email protected]>
image_cache_name treated any existing file as a local archive. A
relative archive path without a .tar suffix, such as app.tar.gz, named
one entry while the file existed and another once it was gone. A
sandbox deleted after its archive released its pin under the wrong
name, and the pin kept the entry from ever being evicted. Master named
entries after the literal string, so this is new in this PR.

Recognize a local archive by its spelling (a .tar suffix, or a leading
/, ./ or ../), the test _extract_image_layers applies before it reports
a missing archive. An existing file spelled otherwise is still read
from disk; its entry is named like a registry reference.

Signed-off-by: xyuzh <[email protected]>
@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 15, 2026
pcmoritz added a commit that referenced this pull request Oct 15, 2026
… once (#67023)

## Description

Cold pulls of large images are slow. A SWE-bench image with 1.1 GiB of
layers took 57-64 s to pull on a CoreWeave node, and two things wasted
most of that time:

- Layers were downloaded one at a time, and each was extracted only
after its download finished.
- Each layer was decompressed twice: `tar.getmembers()` reads the whole
gzip stream, then `extractfile()` seeks back and decompresses it again.

This PR downloads up to three layers at once (Docker's default) into
temporary files and extracts them in manifest order as they arrive, so
whiteouts and overrides apply exactly as before. A failed download fails
the pull right away. The other downloads stop at their next read, and
ones that haven't started never connect.

`extract_tar_layer` iterates the archive instead of calling
`getmembers()` first, so it reads each layer once. It keeps the `r:*`
reader rather than stream mode (`r|*`), because only `r:*` checks where
a gzip stream ends. That check catches a layer download the registry
closed early.

Same image, two cold pulls each, alternating:

| | master | this PR |
| --- | --- | --- |
| Whole pull | 63.7 s / 56.7 s | 37.6 s / 37.8 s |
| Download and extraction | 38.1 s / 33.0 s | 18.6 s / 16.8 s |

This PR doesn't touch the EROFS build. It took 17-24 s in these runs,
and both sides built the same 2.92 GiB image.

## Related issues

None filed. Found while running SkyRL's SWE-bench Multimodal tasks on
Ray Sandbox (skyrl-project/SkyRL#2467).

## Additional information

- Not a duplicate: no open PR changes how layers are fetched or read.
#66469, #66985 and #67026 edit other parts of `image_utils.py`.
- Tests: four new tests. Three layer downloads must run at once and
still apply in order. A failed layer must fail the pull within 5 s and
close the other downloads. A layer download cut short on a member
boundary must fail the pull. A 2 MiB layer must be read once. The first,
second and fourth fail on master, and the third fails with stream mode.
- Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils
1.9.4): `TEST_SANDBOX=1 pytest 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"`: 93 passed, 1 skipped. `-k netns`: 7 passed. 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]>

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

It seems currently we parse something like python:3.12-slim@sha256:<hash> by folding the tag into the repo, whereas upstream would parse it by dropping the tag, that might be worth fixing before we merge it.

@pcmoritz
pcmoritz merged commit 87f45d6 into ray-project:master Oct 19, 2026
7 checks passed
xyuzh added a commit to xyuzh/ray that referenced this pull request Oct 21, 2026
…tence

Apply the seven style suggestions from the docs review. Also put back
the sentence that ray-project#66985 added to the "Container images" intro, which
was lost when master was merged into this branch.

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

## Description

Cold pulls of large images are slow. A SWE-bench image with 1.1 GiB of
layers took 57-64 s to pull on a CoreWeave node, and two things wasted
most of that time:

- Layers were downloaded one at a time, and each was extracted only
after its download finished.
- Each layer was decompressed twice: `tar.getmembers()` reads the whole
gzip stream, then `extractfile()` seeks back and decompresses it again.

This PR downloads up to three layers at once (Docker's default) into
temporary files and extracts them in manifest order as they arrive, so
whiteouts and overrides apply exactly as before. A failed download fails
the pull right away. The other downloads stop at their next read, and
ones that haven't started never connect.

`extract_tar_layer` iterates the archive instead of calling
`getmembers()` first, so it reads each layer once. It keeps the `r:*`
reader rather than stream mode (`r|*`), because only `r:*` checks where
a gzip stream ends. That check catches a layer download the registry
closed early.

Same image, two cold pulls each, alternating:

| | master | this PR |
| --- | --- | --- |
| Whole pull | 63.7 s / 56.7 s | 37.6 s / 37.8 s |
| Download and extraction | 38.1 s / 33.0 s | 18.6 s / 16.8 s |

This PR doesn't touch the EROFS build. It took 17-24 s in these runs,
and both sides built the same 2.92 GiB image.

## Related issues

None filed. Found while running SkyRL's SWE-bench Multimodal tasks on
Ray Sandbox (skyrl-project/SkyRL#2467).

## Additional information

- Not a duplicate: no open PR changes how layers are fetched or read.
ray-project#66469, ray-project#66985 and ray-project#67026 edit other parts of `image_utils.py`.
- Tests: four new tests. Three layer downloads must run at once and
still apply in order. A failed layer must fail the pull within 5 s and
close the other downloads. A layer download cut short on a member
boundary must fail the pull. A 2 MiB layer must be read once. The first,
second and fourth fail on master, and the third fails with stream mode.
- Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils
1.9.4): `TEST_SANDBOX=1 pytest 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"`: 93 passed, 1 skipped. `-k netns`: 7 passed. 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
pawelbeza pushed a commit to pawelbeza/ray that referenced this pull request Nov 2, 2026
…ay-project#66985)

## Description

The image cache named each entry after the literal image reference.
`python:3.12-slim`, `library/python:3.12-slim` and
`docker.io/library/python:3.12-slim` name the same image, but each one
pulled it and built its own EROFS root filesystem. SkyRL's SWE-bench
Multimodal tasks spell their images in different ways, and on a
CoreWeave node each extra spelling of a 1.1 GiB image cost about a
minute and 2.9 GiB of disk.

This PR names each entry after the normalized reference (registry,
repository, and tag or digest, with Docker Hub written as `docker.io`),
followed by a short hash of it. The spellings above all map to
`docker.io_library_python_3.12-slim-ea59dc550ffd7fa7`.

- The hash keeps apart references that look the same once `/`, `:` and
`@` become `_`, such as `repo@sha256:<hex>` and a tag named
`sha256_<hex>`. Master's literal names had the same collisions.
- The readable part stops at 128 characters, so a long reference stays
under the file-name limit.
- `parse_image_ref` lowercases the registry host. `Docker.io/python`
used to be pulled from https://Docker.io and fail.
- A local archive spelled as a path or ending in `.tar` keeps its file
name, as before.

Existing entries have literal names, so after an upgrade each node pulls
each image once more. The old entries are the least recently extracted,
so eviction removes them first when the cache needs room.

## Related issues

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

## Additional information

- Not a duplicate: searches for "sandbox image cache" and "sandbox image
reference" found no open PR that normalizes references. ray-project#66469, ray-project#66470
and ray-project#65898 check entry names in this module, and the new names pass
their check.
- Tests: new tests cover one entry and one pull for the spellings,
separate entries for colliding references, short names for long
references, uppercase hosts, and releasing a pin after its relative
archive path is deleted. The spelling, digest-versus-tag and host-case
tests fail on master.
- Tested on a CoreWeave pod (runsc release-20261005.0, erofs-utils
1.9.4): `TEST_SANDBOX=1 pytest 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
and not test_create_sandbox_helper"`: 93 passed, 1 skipped (95 passed
merged with master); `-k netns`: 9 passed. 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 go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants