-
Notifications
You must be signed in to change notification settings - Fork 12
fix(imds): close response body in HealthCheck and New #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Previously, GetMetadata response bodies were not closed in two places: - NewIMDSClient validation check - HealthCheck method This could cause HTTP connection and file descriptor leaks over time, especially when health checks run frequently. Added response body Close() calls following the same pattern used in HealthCheckDetailed and getMetadata methods. Also added tests to verify response bodies are properly closed using a tracking ReadCloser wrapper. Fixes #508
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes HTTP response body leaks in the IMDS client by ensuring response bodies are properly closed in two methods that were previously missing these cleanup calls, preventing potential connection and file descriptor exhaustion.
Changes:
- Added response body closure in
New()validation check andHealthCheck()method - Added comprehensive tests to verify response bodies are closed correctly
- Introduced helper utilities for tracking Close() calls in tests
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/backends/imds/client.go | Added Close() calls for response bodies in New() and HealthCheck() methods |
| pkg/backends/imds/client_test.go | Added test infrastructure and tests to verify response body closure |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return nil, fmt.Errorf("IMDS not available: %w", err) | ||
| } | ||
| if closeErr := validationOutput.Content.Close(); closeErr != nil { | ||
| log.Debug("Failed to close IMDS validation response: %v", closeErr) |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message uses %v format verb in log.Debug which expects format arguments, but log.Debug likely doesn't support format arguments. Either use fmt.Sprintf to format the message, or pass closeErr as a separate argument if log.Debug supports it.
| log.Debug("Failed to close IMDS validation response: %v", closeErr) | |
| log.Debug(fmt.Sprintf("Failed to close IMDS validation response: %v", closeErr)) |
| if closeErr := output.Content.Close(); closeErr != nil { | ||
| log.Debug("Failed to close IMDS health check response: %v", closeErr) | ||
| } |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message uses %v format verb in log.Debug which expects format arguments, but log.Debug likely doesn't support format arguments. Either use fmt.Sprintf to format the message, or pass closeErr as a separate argument if log.Debug supports it.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #511 +/- ##
==========================================
+ Coverage 62.00% 62.10% +0.09%
==========================================
Files 48 48
Lines 5393 5399 +6
==========================================
+ Hits 3344 3353 +9
+ Misses 1842 1837 -5
- Partials 207 209 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Reviewed. The response bodies are now closed in and , and the new tests validate closure behavior. Looks correct and addresses #508. Optional: add a test covering the validation close path for full coverage. |
Review ResponseCopilot comments (Lines 121 & 346)These are false positives. The // From pkg/log/log.go:182-184
func Debug(format string, v ...interface{}) {
logger.Debug(fmt.Sprintf(format, v...))
}This pattern is used consistently throughout the codebase (see lines 148, 249, 276, 298, 318, 371 in the same file). @abtreece's suggestion (test for
|
Refactored New() to extract client initialization into newWithClient() to enable testing with mock clients. Added tests: - TestNewWithClient_Success: verifies successful creation and response body closure - TestNewWithClient_IMDSUnavailable: verifies error handling when IMDS is unavailable
Added test coverage for
|
Summary
HealthCheckmethodNew(client creation) validation checkDetails
Previously,
GetMetadataresponse bodies were not closed in two places:New()- validation check during client creation (line 114)HealthCheck()- basic health check method (line 335)This is inconsistent with other methods in the same file (
HealthCheckDetailed,getMetadata) which properly close response bodies. The leak could cause HTTP connection and file descriptor exhaustion over time, especially when health checks run frequently.Changes
pkg/backends/imds/client.go:output.Content.Close()call inNew()after validation checkoutput.Content.Close()call inHealthCheck()after successful checkpkg/backends/imds/client_test.go:trackingReadCloserhelper to track Close() callsmockResponseWithCloseTracking()helper for creating trackable mock responsesTestHealthCheck_ClosesResponseBodytestTestHealthCheckDetailed_ClosesResponseBodytestTest plan
Fixes #508