From f93a7d22d31b00103a26980faa08bf738267a240 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Tue, 12 Mar 2024 10:32:43 +0800 Subject: [PATCH] Fix leaks caused by http body not close --- internal/build/download.go | 6 ++++-- rpc/http.go | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/build/download.go b/internal/build/download.go index fda573df83..206c51dce1 100644 --- a/internal/build/download.go +++ b/internal/build/download.go @@ -84,10 +84,12 @@ func (db *ChecksumDB) DownloadFile(url, dstPath string) error { resp, err := http.Get(url) if err != nil { return fmt.Errorf("download error: %v", err) - } else if resp.StatusCode != http.StatusOK { - return fmt.Errorf("download error: status %d", resp.StatusCode) } defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("download error: status %d", resp.StatusCode) + } if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil { return err } diff --git a/rpc/http.go b/rpc/http.go index dd376b1ecd..4b5142101b 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -237,6 +237,8 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos body = buf.Bytes() } + resp.Body.Close() + return nil, HTTPError{ Status: resp.Status, StatusCode: resp.StatusCode,