Skip to content

[core][sandbox] Download image layers in parallel and decompress each once - #67023

Merged
pcmoritz merged 4 commits into
ray-project:masterfrom
xyuzh:sandbox-parallel-pull
Oct 15, 2026
Merged

pcmoritz merged 4 commits into
ray-project:masterfrom
xyuzh:sandbox-parallel-pull

Conversation

@xyuzh

@xyuzh xyuzh commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

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

… once

A cold pull downloaded an image's layers one at a time and extracted
each only after its download finished, so a SWE-bench image (1.1 GiB in
layers) spent 22 s downloading and 26 s extracting, one after the other.
Extraction also listed each layer's members with getmembers() and then
read them through extractfile(), which rewinds a compressed stream, so
every layer was decompressed twice.

Layers now download three at a time, as in Docker, into anonymous
temporary files, and each is extracted in manifest order as soon as it
and the layers before it have arrived. A failed download fails the pull
at once; downloads still running stop at their next read. Extraction
reads each layer in a single pass in tarfile's stream mode.

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

@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 optimizes the image layer extraction process by downloading registry blobs in parallel using a thread pool while ensuring they are applied in the correct order. Additionally, it updates extract_tar_layer to stream mode to prevent reading and decompressing layers twice, and introduces robust unit tests to verify parallel downloads and error handling. The feedback suggests checking the stop event before initiating network requests in _download_blob to prevent redundant connections if another layer has already failed.

Comment on lines +970 to +972
blob = tempfile.TemporaryFile(dir=blob_dir)
try:
req = _registry_request(url, headers, auth_header)

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

Checking stop.is_set() before initiating the network request with urllib.request.urlopen avoids making unnecessary network connections if the image pull has already been aborted due to a failure in another layer. This helps save network resources and avoids potential timeouts or rate-limiting issues on aborted pulls.

    blob = tempfile.TemporaryFile(dir=blob_dir)
    try:
        if stop.is_set():
            blob.seek(0)
            return blob
        req = _registry_request(url, headers, auth_header)

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 8a57812: _download_blob checks stop right after it creates the temporary file and returns before opening a connection. The empty file it returns is closed by the same done-callback that closes the other blobs of a failed pull.

…stopped

A download that a worker picks up after the pull failed opened its
connection before it checked whether to stop. It now checks first.
Review follow-up.

Signed-off-by: xyuzh <[email protected]>
@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Oct 14, 2026
Stream mode (r|*) read each layer once, but it doesn't check where a
gzip stream ends. When a registry closes a blob response early, read1()
returns no error, so a layer cut short on a member boundary extracted
without one, and the image was cached with files missing. Master's
GzipFile reader raised on every such cut: in 2,000 random cuts of a test
layer, stream mode read 29 without an error.

Iterating an r:* archive, instead of calling getmembers() first, also
reads the layer once and keeps that check. On a 572 MiB layer it was a
little faster than stream mode (1.75 s vs 1.93 s).

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

xyuzh commented Oct 15, 2026

Copy link
Copy Markdown
Member Author

One more commit after the approval, 93604c1. Stream mode (r|*) doesn't check where a gzip stream ends, so a layer whose download the registry closed early could extract without an error when the cut fell on a member boundary: 29 of 2,000 random cuts of a test layer, where master's reader raised on all of them. extract_tar_layer now iterates an r:* archive, which still reads each layer once and keeps that check. test_a_layer_cut_short_fails_the_pull fails without it, and a cold pull of the SWE-bench image still takes about 38 s, down from 57-64 s on master.

@pcmoritz pcmoritz added the go add ONLY when ready to merge, run all tests label Oct 15, 2026
@pcmoritz
pcmoritz enabled auto-merge (squash) October 15, 2026 17:35
@pcmoritz
pcmoritz merged commit fc9ddad into ray-project:master Oct 15, 2026
7 of 8 checks passed
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
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.

2 participants