Skip to content

cost-analysis-export: Fix resource leaks and apply timeout#5771

Open
SebTardif wants to merge 2 commits into
Azure:masterfrom
SebTardif:fix/cost-analysis-resource-leaks
Open

cost-analysis-export: Fix resource leaks and apply timeout#5771
SebTardif wants to merge 2 commits into
Azure:masterfrom
SebTardif:fix/cost-analysis-resource-leaks

Conversation

@SebTardif
Copy link
Copy Markdown

This PR fixes two resource management issues in the cost-analysis-export example:

1. Apply the configured timeout (EXPORT_TIMEOUT)

The Config.Timeout field (env var EXPORT_TIMEOUT, default 10 minutes) is parsed and validated but never used. context.Background() propagates through every downstream call with no deadline, so the entire pipeline (HTTP requests, Azure Blob operations, SQLite queries) can hang indefinitely.

This change wraps the context with context.WithTimeout(ctx, cfg.Timeout) in ConfigureAndProcess, and replaces http.DefaultClient with a client that has a 2-minute per-request timeout as a safety net.

2. Close temp file and remove on disk in joinAndExportData

JoinData() creates a temp file and returns it with an open handle. The caller joinAndExportData passes it to UploadFile but never calls Close() or os.Remove(), leaking a file descriptor and temp file on every run.


Introduced in #5393 (2025-10-24).

Apply the configured EXPORT_TIMEOUT via context.WithTimeout so the
entire pipeline has a deadline. Replace http.DefaultClient with a
client that has a 2-minute per-request timeout. Close and remove the
temp file returned by JoinData in joinAndExportData.

Introduced in Azure#5393 (2025-10-24).

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif SebTardif requested review from a team, Copilot and sdesai345 May 13, 2026 21:39
@SebTardif SebTardif requested review from a team and palma21 as code owners May 13, 2026 21:39
@SebTardif SebTardif requested a review from seguler May 13, 2026 21:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes resource management issues in the cost-analysis-export example by applying the configured timeout and properly cleaning up temp files.

Changes:

  • Wrap context with context.WithTimeout using cfg.Timeout in ConfigureAndProcess.
  • Replace http.DefaultClient with a client that has a 2-minute per-request timeout.
  • Close and remove the temp file returned by JoinData() in joinAndExportData.

Comment thread examples/cost-analysis-export/main.go Outdated
Comment on lines +188 to +190
// Execute request with timeout
httpClient := &http.Client{Timeout: 2 * time.Minute}
resp, err := httpClient.Do(req)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Removed the separate http.Client{Timeout: 2m} and reverted to http.DefaultClient. The request already uses NewRequestWithContext(ctx) which carries cfg.Timeout, so the context deadline handles cancellation without a second undocumented knob.

_ = os.Remove(file.Name())
}()
slog.Info("data joined successfully", "file", file.Name())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an issue here. JoinData() already seeks back to position 0 before returning (line 812: tmpFile.Seek(0, 0)), so the file handle is at the start when UploadFile receives it. The Azure SDK's UploadFile also takes an *os.File and handles positioning internally.

The request already uses NewRequestWithContext(ctx) which carries
cfg.Timeout. A separate http.Client{Timeout: 2m} creates a second
undocumented timeout knob. Use DefaultClient and let the context
deadline handle cancellation.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants