Skip to content

Commit

Permalink
Merge pull request #5773 from thaJeztah/1.4_backport_fix_missing_body…
Browse files Browse the repository at this point in the history
…_close

[release/1.4 backport] add missing body.Close() in docker/fetcher and docker/pusher
  • Loading branch information
dmcgowan committed Jul 21, 2021
2 parents a5cefba + 14c3a8e commit 53add4c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions remotes/docker/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions remotes/docker/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ func (p dockerPusher) Push(ctx context.Context, desc ocispec.Descriptor) (conten
// TODO: Set updated time?
},
})
resp.Body.Close()
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "content %v on remote", desc.Digest)
}
} else if resp.StatusCode != http.StatusNotFound {
resp.Body.Close()
// TODO: log error
return nil, errors.Errorf("unexpected response: %s", resp.Status)
}
resp.Body.Close()
}

if isManifest {
Expand Down

0 comments on commit 53add4c

Please sign in to comment.