Repository navigation
[core][sandbox] Stream the image tar into mkfs.erofs and free the tree as it goes - #67493
Conversation
…e as it goes A cold pull held the extracted tree and a flat tar of it, two full copies of the image, while mkfs.erofs built the image. With many concurrent cold pulls of distinct images that filled the node's disk. build_erofs_image now streams the tar into mkfs.erofs's stdin, which it reads when given no source (erofs-utils 1.7+), and deletes each file from the tree once it's in the stream. Members keep tar.add's order, so the image is byte-identical. Files with other hard links stay, so links aren't stored as copies, and read-only directories get owner write after their header is written, so a worker that isn't root can still delete their files. Signed-off-by: xyuzh <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors the EROFS image building process to stream the tar archive directly into mkfs.erofs's standard input, deleting files as they are processed to reduce disk usage. It also adds comprehensive tests to verify this streaming behavior and error handling. The review feedback focuses on improving the robustness of the subprocess exception handling and cleanup, specifically by catching ConnectionError instead of BrokenPipeError during streaming, wrapping proc.kill() to prevent masking original exceptions, and catching general OSError exceptions when closing the input pipe in the finally block.
| except BrokenPipeError: | ||
| # mkfs.erofs stopped reading; its exit status and output say why. | ||
| pass |
There was a problem hiding this comment.
Catching only BrokenPipeError can be fragile because writing to a broken pipe can raise other connection-related errors (such as ConnectionResetError) depending on the OS platform and Python version. If a different connection error is raised, it will propagate to the outer except BaseException block, killing the process and raising a generic traceback instead of waiting for proc and reporting the helpful mkfs.erofs error message.
However, catching a generic OSError here is dangerous because a local file read error (which is also an OSError) would be caught, causing the code to proceed to proc.wait() while proc.stdin is still open, leading to a deadlock.
To be both robust and safe from deadlocks, we should catch ConnectionError, which is the base class for all connection/pipe-related errors (including BrokenPipeError and ConnectionResetError).
| except BrokenPipeError: | |
| # mkfs.erofs stopped reading; its exit status and output say why. | |
| pass | |
| except ConnectionError: | |
| # mkfs.erofs stopped reading; its exit status and output say why. | |
| pass |
| except BaseException: | ||
| proc.kill() | ||
| proc.wait() | ||
| raise |
There was a problem hiding this comment.
If proc.kill() raises an OSError (for example, ProcessLookupError because the process has already exited), it will propagate and mask the original exception that triggered the except BaseException block. Wrapping proc.kill() in a try...except OSError block ensures that the original exception is safely re-raised.
except BaseException:
try:
proc.kill()
except OSError:
pass
proc.wait()
raise| try: | ||
| proc.stdin.close() | ||
| except BrokenPipeError: | ||
| pass |
There was a problem hiding this comment.
In the finally block, we should catch any OSError (not just BrokenPipeError) when closing proc.stdin. Since the process might have been killed or exited, closing the pipe can raise various system errors depending on the platform. Catching OSError ensures that no unexpected exceptions are raised during cleanup, which could mask other active exceptions.
| try: | |
| proc.stdin.close() | |
| except BrokenPipeError: | |
| pass | |
| try: | |
| proc.stdin.close() | |
| except OSError: | |
| pass |
…e as it goes (ray-project#67493) ## Description While mkfs.erofs builds a cold-pulled image, the node holds the extracted tree and a flat tar of it: two full copies on top of the image. Running SkyRL's Harbor integration with 64 SWE-bench Verified tasks at once on two 60-CPU nodes, 32 concurrent cold pulls per node filled the host's 200 GB disk within 20 seconds and both nodes died. This PR streams the tar into mkfs.erofs's stdin, which it reads when given no source (erofs-utils 1.7, Ray's minimum, already does), and deletes each file from the tree once it's in the stream. - Members keep `tar.add`'s order, so the image is byte-identical. - Files with other hard links stay until the caller removes the tree, so links aren't stored as copies. - A read-only directory gets owner write once its header is in the stream, so a worker that isn't root can delete its files. The image keeps the original mode. - If mkfs.erofs exits early, the build reports its error. Any other failure kills and reaps it first. On django-13279 (2.8 GiB tree and image), two four-round A/B runs on Anyscale: | | master | this PR | | --- | --- | --- | | Peak disk use beyond the tree | 5.5-6.1 GiB | 0.67-0.74 GiB | | Build time, 8 builds | 48-177 s, median 72 s | 48-50 s, one 99 s during another pod's large write | With the UUID and build time pinned, both give byte-identical images, and `fsck.erofs` passes. mkfs.erofs still spools file data under `$TMPDIR` (default `--sort=path`). `--sort=none` (erofs-utils 1.8.2+) would drop that too, but it changes the data layout, so it's a separate change. ## Related issues None filed. Found while validating SkyRL's Harbor integration on Ray Sandbox (skyrl-project/SkyRL#2467). ## Additional information - Not a duplicate: no open PR streams the build or frees the tree. ray-project#67484 edits the same mkfs.erofs call and fake mkfs.erofs fixture, so whichever lands second needs a small rebase. - Tests: three new tests in `test_image_utils.py` cover the streamed tar matching `tar.add` member for member (hard links, a read-only directory, a file larger than a pipe), mkfs.erofs failing before it reads, and mkfs.erofs being killed when reading the tree fails. The first and third fail on master. - Tested: `TEST_SANDBOX=1 pytest python/ray/experimental/sandbox/tests/test_image_utils.py`: 44 passed on macOS as a non-root user. The A/B ran on Linux with erofs-utils 1.9.4. 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
While mkfs.erofs builds a cold-pulled image, the node holds the extracted tree and a flat tar of it: two full copies on top of the image. Running SkyRL's Harbor integration with 64 SWE-bench Verified tasks at once on two 60-CPU nodes, 32 concurrent cold pulls per node filled the host's 200 GB disk within 20 seconds and both nodes died.
This PR streams the tar into mkfs.erofs's stdin, which it reads when given no source (erofs-utils 1.7, Ray's minimum, already does), and deletes each file from the tree once it's in the stream.
tar.add's order, so the image is byte-identical.On django-13279 (2.8 GiB tree and image), two four-round A/B runs on Anyscale:
With the UUID and build time pinned, both give byte-identical images, and
fsck.erofspasses. mkfs.erofs still spools file data under$TMPDIR(default--sort=path).--sort=none(erofs-utils 1.8.2+) would drop that too, but it changes the data layout, so it's a separate change.Related issues
None filed. Found while validating SkyRL's Harbor integration on Ray Sandbox (skyrl-project/SkyRL#2467).
Additional information
test_image_utils.pycover the streamed tar matchingtar.addmember for member (hard links, a read-only directory, a file larger than a pipe), mkfs.erofs failing before it reads, and mkfs.erofs being killed when reading the tree fails. The first and third fail on master.TEST_SANDBOX=1 pytest python/ray/experimental/sandbox/tests/test_image_utils.py: 44 passed on macOS as a non-root user. The A/B ran on Linux with erofs-utils 1.9.4. pre-commit passes.