From 67d07fe5cf4f165396872499b3d36ee5cba6774d Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Wed, 21 Jul 2021 10:28:47 +0800 Subject: [PATCH] remotes/docker/fetcher.go: Fix missing Close() Signed-off-by: Shiming Zhang --- remotes/docker/fetcher.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/remotes/docker/fetcher.go b/remotes/docker/fetcher.go index 9d6708def..5796fbf4a 100644 --- a/remotes/docker/fetcher.go +++ b/remotes/docker/fetcher.go @@ -148,7 +148,7 @@ func (r dockerFetcher) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.R }) } -func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, offset int64) (io.ReadCloser, error) { +func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, offset int64) (_ io.ReadCloser, retErr error) { req.header.Set("Accept", strings.Join([]string{mediatype, `*/*`}, ", ")) if offset > 0 { @@ -162,13 +162,17 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, if err != nil { return nil, err } + defer func() { + if retErr != nil { + resp.Body.Close() + } + }() if resp.StatusCode > 299 { // TODO(stevvooe): When doing a offset specific request, we should // really distinguish between a 206 and a 200. In the case of 200, we // can discard the bytes, hiding the seek behavior from the // implementation. - defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { return nil, errors.Wrapf(errdefs.ErrNotFound, "content at %v not found", req.String())