From 918461837b68a2b88512d002d8692f18ab503387 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Thu, 4 Mar 2021 13:41:40 +0000 Subject: [PATCH] Ensure response body is closed when we are finished with the request When creating an http request in go, this sets two goroutines in the background running. If the body is not closed properly, these goroutines are leaked. This leads to a gradual increase in memory used by the application over time. We must ensure we always close response bodies --- pkg/termination/termination.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/termination/termination.go b/pkg/termination/termination.go index 1b912429e..4aaa8d841 100644 --- a/pkg/termination/termination.go +++ b/pkg/termination/termination.go @@ -107,6 +107,9 @@ func (h *handler) run(ctx context.Context) error { req.Header.Add("Metadata-Flavor", "Google") resp, err := http.DefaultClient.Do(req) + if resp != nil { + defer resp.Body.Close() + } if err != nil { return false, fmt.Errorf("could not get URL %q: %w", h.pollURL.String(), err) }