Skip to content

mutate: close layer readers during export - #2277

Merged
Subserial merged 1 commit into
google:mainfrom
gnix0:fix-export-layer-reader-close
Apr 28, 2026
Merged

Subserial merged 1 commit into
google:mainfrom
gnix0:fix-export-layer-reader-close

Conversation

@gnix0

@gnix0 gnix0 commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Summary

crane export normally ends up in mutate.Extract, which walks the image layers and reads each one with Uncompressed().

The problem is that mutate.Extract was deferring each layer reader close until the whole extraction returned. That used to be mostly harmless, but after #2271 remote layer reads hold a pull slot until the response body is closed. So with enough layers, export can use up all the available pull slots and then block while trying to open the next layer.

While checking that path, I also noticed the single-layer export shortcut copied from Uncompressed() without closing the reader.

This change closes each layer reader as soon as that layer has been processed, and also closes the reader in the single-layer export path.

Fixes #2276

Reproduced:

I added a local test image whose layer readers behave like the remote pull limiter: opening a reader takes one of four slots, and closing it gives the slot back. Before the fix, extracting five layers failed when the fifth reader opened before the earlier readers had been closed. After the fix, extraction completes and all slots are released.

Tests added:

  • TestExtractClosesLayerBeforeOpeningNext
  • TestExportClosesSingleLayerReader

Tested:

go test ./pkg/v1/mutate

go test ./pkg/crane

go test ./pkg/v1/remote ./pkg/v1/mutate ./pkg/crane

./cmd/crane/rebase_test.sh

crane export uses mutate.Extract for normal layered images.
mutate.Extract opened each layer reader in sequence, but deferred each
Close until the whole extraction returned. After google#2271, remote layer
readers hold a pull job slot until their response body is closed, so
extraction could fill all available slots and then block opening the next
layer.

The single-layer crane export path also copied from Uncompressed without
closing the reader.

Close each extraction layer reader as soon as that layer is processed,
and close the single-layer export reader after copying.

Fixes google#2276

Reproduced:
Added a test image whose layer readers model the remote pull limiter:
opening a reader consumes one of four slots, and Close releases it.
Before the fix, extracting five layers failed when the fifth reader
opened before the previous readers were closed. After the fix,
extraction completes and all slots are released.

Tested:
go test ./pkg/v1/mutate
go test ./pkg/crane
go test ./pkg/v1/remote ./pkg/v1/mutate ./pkg/crane
./cmd/crane/rebase_test.sh
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.37838% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 53.08%. Comparing base (d4f1050) to head (8b2e68b).

Files with missing lines Patch % Lines
pkg/v1/mutate/mutate.go 77.77% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2277      +/-   ##
==========================================
+ Coverage   52.08%   53.08%   +1.00%     
==========================================
  Files         166      166              
  Lines       14243    11275    -2968     
==========================================
- Hits         7418     5985    -1433     
+ Misses       6103     4567    -1536     
- Partials      722      723       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Subserial
Subserial merged commit 38d6e40 into google:main Apr 28, 2026
17 checks passed
Subserial pushed a commit to Subserial/go-containerregistry that referenced this pull request May 15, 2026
crane export uses mutate.Extract for normal layered images.
mutate.Extract opened each layer reader in sequence, but deferred each
Close until the whole extraction returned. After google#2271, remote layer
readers hold a pull job slot until their response body is closed, so
extraction could fill all available slots and then block opening the next
layer.

The single-layer crane export path also copied from Uncompressed without
closing the reader.

Close each extraction layer reader as soon as that layer is processed,
and close the single-layer export reader after copying.

Fixes google#2276

Reproduced:
Added a test image whose layer readers model the remote pull limiter:
opening a reader consumes one of four slots, and Close releases it.
Before the fix, extracting five layers failed when the fifth reader
opened before the previous readers were closed. After the fix,
extraction completes and all slots are released.

Tested:
go test ./pkg/v1/mutate
go test ./pkg/crane
go test ./pkg/v1/remote ./pkg/v1/mutate ./pkg/crane
./cmd/crane/rebase_test.sh
nandbhat added a commit to nandbhat/go-containerregistry that referenced this pull request May 21, 2026
tarball.MultiRefWrite (and crane pull, which uses it) opens each layer's
Compressed() reader and copies it into the tar without closing it.
After google#2271, remote layer readers hold a pull-job slot until their
response body is closed, so writing more than `defaultJobs` layers
fills all available slots and deadlocks the next layer open.

This is the symmetric leak to the one google#2277 fixed in mutate.Extract /
crane export. Close each layer reader as soon as that layer is
written.

Reproduced:
  crane pull postgres:16 /tmp/pg.tar    # v0.21.6 hangs; v0.21.5 works

Added TestWriteClosesLayerBeforeOpeningNext, which wraps a random.Image
with a token-limited Compressed() that fails if a reader is opened
before previous readers were closed. Before this change the test fails
with "layer reader opened before previous readers were closed". After
this change the test passes and the limiter is fully drained.

Tested:
  go test ./pkg/v1/tarball
  go test ./pkg/v1/tarball ./pkg/v1/mutate ./pkg/crane
Signed-off-by: Nandan Bhat <[email protected]>
Subserial pushed a commit that referenced this pull request May 21, 2026
* tarball: close layer readers during Write

tarball.MultiRefWrite (and crane pull, which uses it) opens each layer's
Compressed() reader and copies it into the tar without closing it.
After #2271, remote layer readers hold a pull-job slot until their
response body is closed, so writing more than `defaultJobs` layers
fills all available slots and deadlocks the next layer open.

This is the symmetric leak to the one #2277 fixed in mutate.Extract /
crane export. Close each layer reader as soon as that layer is
written.

Reproduced:
  crane pull postgres:16 /tmp/pg.tar    # v0.21.6 hangs; v0.21.5 works

Added TestWriteClosesLayerBeforeOpeningNext, which wraps a random.Image
with a token-limited Compressed() that fails if a reader is opened
before previous readers were closed. Before this change the test fails
with "layer reader opened before previous readers were closed". After
this change the test passes and the limiter is fully drained.

Tested:
  go test ./pkg/v1/tarball
  go test ./pkg/v1/tarball ./pkg/v1/mutate ./pkg/crane
Signed-off-by: Nandan Bhat <[email protected]>

* tarball: place writeLayer next to writeTarEntry

Address review feedback on #2308: the previous patch inserted writeLayer
between writeTarEntry's leading doc-comment and its declaration, orphaning
the comment. Move writeLayer immediately after writeTarEntry so the
existing function block is untouched, and tighten writeLayer's own
doc-comment to the non-obvious WHY (pull-limiter slot release).

No behavioral change.

Signed-off-by: Nandan Bhat <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crane: hang on crane export

3 participants