Skip to content

Commit 8837501

Browse files
authored
Merge pull request #5772 from thaJeztah/1.5_backport_fix_missing_body_close
[release/1.5 backport] add missing body.Close() in docker/fetcher and docker/pusher
2 parents bc12da7 + 7b17268 commit 8837501

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

remotes/docker/fetcher.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (r dockerFetcher) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.R
148148
})
149149
}
150150

151-
func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, offset int64) (io.ReadCloser, error) {
151+
func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, offset int64) (_ io.ReadCloser, retErr error) {
152152
req.header.Set("Accept", strings.Join([]string{mediatype, `*/*`}, ", "))
153153

154154
if offset > 0 {
@@ -162,13 +162,17 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string,
162162
if err != nil {
163163
return nil, err
164164
}
165+
defer func() {
166+
if retErr != nil {
167+
resp.Body.Close()
168+
}
169+
}()
165170

166171
if resp.StatusCode > 299 {
167172
// TODO(stevvooe): When doing a offset specific request, we should
168173
// really distinguish between a 206 and a 200. In the case of 200, we
169174
// can discard the bytes, hiding the seek behavior from the
170175
// implementation.
171-
defer resp.Body.Close()
172176

173177
if resp.StatusCode == http.StatusNotFound {
174178
return nil, errors.Wrapf(errdefs.ErrNotFound, "content at %v not found", req.String())

remotes/docker/pusher.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,16 @@ func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, ref str
143143
// TODO: Set updated time?
144144
},
145145
})
146+
resp.Body.Close()
146147
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "content %v on remote", desc.Digest)
147148
}
148149
} else if resp.StatusCode != http.StatusNotFound {
149150
err := remoteserrors.NewUnexpectedStatusErr(resp)
150151
log.G(ctx).WithField("resp", resp).WithField("body", string(err.(remoteserrors.ErrUnexpectedStatus).Body)).Debug("unexpected response")
152+
resp.Body.Close()
151153
return nil, err
152154
}
155+
resp.Body.Close()
153156
}
154157

155158
if isManifest {

0 commit comments

Comments
 (0)