Skip to content

Commit ce2d146

Browse files
authored
[internal/metadataproviders/azure] Fix memory leak on AKS (#32574)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> 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:** <Issue number if applicable> #30438 **Testing:** <Describe what testing was performed and which tests were added.> All existing tests are passing as well as added `goleak` check.
1 parent b82cd7f commit ce2d146

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

.chloggen/goleak_internalazure.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: processor/resourcedetection, exporter/datadog
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix memory leak on AKS
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32574]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

internal/metadataproviders/azure/metadata.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ func (p *azureProviderImpl) Metadata(ctx context.Context) (*ComputeMetadata, err
7474
resp, err := p.client.Do(req)
7575
if err != nil {
7676
return nil, fmt.Errorf("failed to query Azure IMDS: %w", err)
77-
} else if resp.StatusCode != 200 {
77+
}
78+
defer resp.Body.Close()
79+
if resp.StatusCode != 200 {
7880
//lint:ignore ST1005 Azure is a capitalized proper noun here
7981
return nil, fmt.Errorf("Azure IMDS replied with status code: %s", resp.Status)
8082
}
8183

82-
defer resp.Body.Close()
8384
respBody, err := io.ReadAll(resp.Body)
8485
if err != nil {
8586
return nil, fmt.Errorf("failed to read Azure IMDS reply: %w", err)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package azure
5+
6+
import (
7+
"testing"
8+
9+
"go.uber.org/goleak"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
goleak.VerifyTestMain(m)
14+
}

0 commit comments

Comments
 (0)