Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Don't close base images prematurely #455

Merged
merged 1 commit into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Bugfix: Don't close base images prematurely
Our previous implementation was closing the base image stream before it
was even used. With this commit we make it clear who is responsible for
closing this stream, and ensure it is closed only after it's used.

Signed-off-by: Leandro Motta Barros <leandro@balena.io>
Change-type: patch
lmbarros committed Mar 29, 2024
commit 01a3fc31a37f2252af0ac5504659e9fbeb5c3d1d
9 changes: 7 additions & 2 deletions distribution/pull_v2.go
Original file line number Diff line number Diff line change
@@ -592,7 +592,11 @@ func (p *v2Puller) pullSchema2Layers(ctx context.Context, target distribution.De
// Target image already exists locally, no need to pull anything
return digest, nil
}
deltaBase, err = DeltaBaseImageFromConfig(img.Config, p.config.ImageStore)
deltaBaseCloser, err := DeltaBaseImageFromConfig(img.Config, p.config.ImageStore)
if deltaBaseCloser != nil {
defer deltaBaseCloser.Close()
}
deltaBase = deltaBaseCloser
if err != nil {
return "", err
}
@@ -1029,6 +1033,8 @@ func toOCIPlatform(p manifestlist.PlatformSpec) specs.Platform {
// image associated with imgConfig. Passing an imgConfig that is not a delta
// image is not considered an error: in this case the function returns a nil
// ReadSeekCloser (and a nil error).
//
// The caller is responsible for Close()ing the returned stream.
func DeltaBaseImageFromConfig(imgConfig *container.Config, imgConfigStore ImageConfigStore) (ioutils.ReadSeekCloser, error) {
if base, ok := imgConfig.Labels["io.resin.delta.base"]; ok {
digest, err := digest.Parse(base)
@@ -1040,7 +1046,6 @@ func DeltaBaseImageFromConfig(imgConfig *container.Config, imgConfigStore ImageC
if err != nil {
return nil, fmt.Errorf("loading delta base image %q: %w", digest, err)
}
defer stream.Close()

return stream, nil
}
6 changes: 5 additions & 1 deletion image/tarexport/load.go
Original file line number Diff line number Diff line change
@@ -114,7 +114,11 @@ func (l *tarexporter) Load(inTar io.ReadCloser, outStream io.Writer, quiet bool)
return nil
}

deltaBase, err = mobyDistribution.DeltaBaseImageFromConfig(img.Config, imgConfigStore)
deltaBaseCloser, err := mobyDistribution.DeltaBaseImageFromConfig(img.Config, imgConfigStore)
if deltaBaseCloser != nil {
defer deltaBaseCloser.Close()
}
deltaBase = deltaBaseCloser
if err != nil {
return err
}