From c5204dfb64449519bda03083ad4d282c06c3ff49 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sun, 12 May 2024 19:17:31 +0300 Subject: [PATCH] fix: close HTTP resp body to prevent resource leak (#4857) This PR fixes possible resource leaks by adding a statement that closes a response body. From the [documentation](https://pkg.go.dev/net/http): > The caller must close the response body when finished with it Also, see this [article](https://golang50shad.es/index.html#close_http_resp_body) for a deep dive into the problem. --- .golangci.yaml | 1 + internal/pipe/blob/blob_minio_test.go | 8 ++++++-- internal/pipe/discord/discord.go | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index ed5a08241..c2c57ca17 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -5,6 +5,7 @@ linters: enable: - thelper - gofumpt + - bodyclose - tparallel - unconvert - unparam diff --git a/internal/pipe/blob/blob_minio_test.go b/internal/pipe/blob/blob_minio_test.go index e854ddf8f..2aa25af73 100644 --- a/internal/pipe/blob/blob_minio_test.go +++ b/internal/pipe/blob/blob_minio_test.go @@ -55,8 +55,12 @@ func TestMain(m *testing.M) { }) requireNoErr(err) requireNoErr(pool.Retry(func() error { - _, err := http.Get(fmt.Sprintf("http://localhost:%s/minio/health/ready", resource.GetPort("9000/tcp"))) - return err + resp, err := http.Get(fmt.Sprintf("http://localhost:%s/minio/health/ready", resource.GetPort("9000/tcp"))) + if err != nil { + return err + } + resp.Body.Close() + return nil })) listen = "localhost:" + resource.GetPort("9000/tcp") diff --git a/internal/pipe/discord/discord.go b/internal/pipe/discord/discord.go index daa5bd3f0..1e3461708 100644 --- a/internal/pipe/discord/discord.go +++ b/internal/pipe/discord/discord.go @@ -91,6 +91,7 @@ func (p Pipe) Announce(ctx *context.Context) error { if err != nil { return fmt.Errorf("discord: %w", err) } + defer resp.Body.Close() if resp.StatusCode != 204 && resp.StatusCode != 200 { return fmt.Errorf("discord: %s", resp.Status)