Repository navigation
mutate: close layer readers during export - #2277
Merged
Merged
Conversation
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
Subserial
approved these changes
Apr 28, 2026
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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
This was referenced May 19, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
crane exportnormally ends up inmutate.Extract, which walks the image layers and reads each one withUncompressed().The problem is that
mutate.Extractwas 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:
TestExtractClosesLayerBeforeOpeningNextTestExportClosesSingleLayerReaderTested:
go test ./pkg/v1/mutatego test ./pkg/cranego test ./pkg/v1/remote ./pkg/v1/mutate ./pkg/crane./cmd/crane/rebase_test.sh