Skip to content

Commit 19934b8

Browse files
committed
fix: leak
resolves distribution#4494 replaces distribution#4567 Signed-off-by: Vadim Bauer <[email protected]>
1 parent c3a9722 commit 19934b8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

internal/client/transport/http_reader.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (hrs *HTTPReadSeeker) reset() {
165165
}
166166
}
167167

168-
func (hrs *HTTPReadSeeker) reader() (io.Reader, error) {
168+
func (hrs *HTTPReadSeeker) reader() (_ io.Reader, retErr error) {
169169
if hrs.err != nil {
170170
return nil, hrs.err
171171
}
@@ -191,6 +191,11 @@ func (hrs *HTTPReadSeeker) reader() (io.Reader, error) {
191191
if err != nil {
192192
return nil, err
193193
}
194+
defer func() {
195+
if retErr != nil {
196+
_ = resp.Body.Close()
197+
}
198+
}()
194199

195200
// Normally would use client.SuccessStatus, but that would be a cyclic
196201
// import
@@ -276,8 +281,11 @@ func (hrs *HTTPReadSeeker) reader() (io.Reader, error) {
276281

277282
hrs.rc = body
278283
} else {
279-
defer resp.Body.Close()
280284
if hrs.errorHandler != nil {
285+
// Closing the body should be handled by the existing defer,
286+
// but in case a custom "errHandler" is used that doesn't return
287+
// an error, we close the body regardless.
288+
defer resp.Body.Close()
281289
return nil, hrs.errorHandler(resp)
282290
}
283291
return nil, fmt.Errorf("unexpected status resolving reader: %v", resp.Status)

0 commit comments

Comments
 (0)