From ce2d14613f0bfab1ffd48c57c579121a36da0e66 Mon Sep 17 00:00:00 2001 From: Curtis Robert Date: Mon, 22 Apr 2024 00:37:56 -0700 Subject: [PATCH] [internal/metadataproviders/azure] Fix memory leak on AKS (#32574) **Description:** From [`client.Do` documentation](https://pkg.go.dev/net/http#Client.Do), a client's response can safely be ignored when an error is returned, but must be handled in all other cases, even when a response code is not 2XX. This fixes the internal AKS detection to properly close the response body in the case of a non-200 status returned. This also enables `goleak` on the `internal/metadata/azure` package which helps ensure no goroutines are being leaked. `goleak` is what detected this bug. **Link to tracking Issue:** #30438 **Testing:** All existing tests are passing as well as added `goleak` check. --- .chloggen/goleak_internalazure.yaml | 27 +++++++++++++++++++ internal/metadataproviders/azure/metadata.go | 5 ++-- .../metadataproviders/azure/package_test.go | 14 ++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 .chloggen/goleak_internalazure.yaml create mode 100644 internal/metadataproviders/azure/package_test.go diff --git a/.chloggen/goleak_internalazure.yaml b/.chloggen/goleak_internalazure.yaml new file mode 100644 index 000000000000..02da1a24b6e1 --- /dev/null +++ b/.chloggen/goleak_internalazure.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: processor/resourcedetection, exporter/datadog + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix memory leak on AKS + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32574] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/internal/metadataproviders/azure/metadata.go b/internal/metadataproviders/azure/metadata.go index b6241feab869..54f73d3a27d7 100644 --- a/internal/metadataproviders/azure/metadata.go +++ b/internal/metadataproviders/azure/metadata.go @@ -74,12 +74,13 @@ func (p *azureProviderImpl) Metadata(ctx context.Context) (*ComputeMetadata, err resp, err := p.client.Do(req) if err != nil { return nil, fmt.Errorf("failed to query Azure IMDS: %w", err) - } else if resp.StatusCode != 200 { + } + defer resp.Body.Close() + if resp.StatusCode != 200 { //lint:ignore ST1005 Azure is a capitalized proper noun here return nil, fmt.Errorf("Azure IMDS replied with status code: %s", resp.Status) } - defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read Azure IMDS reply: %w", err) diff --git a/internal/metadataproviders/azure/package_test.go b/internal/metadataproviders/azure/package_test.go new file mode 100644 index 000000000000..c7b9e1e8dda9 --- /dev/null +++ b/internal/metadataproviders/azure/package_test.go @@ -0,0 +1,14 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package azure + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +}