Fix file descriptor leak in GCP FGetObject on error paths#2053
Open
SebTardif wants to merge 1 commit into
Open
Fix file descriptor leak in GCP FGetObject on error paths#2053SebTardif wants to merge 1 commit into
SebTardif wants to merge 1 commit into
Conversation
hiddeco
approved these changes
May 18, 2026
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer objectFile.Close() |
Member
There was a problem hiding this comment.
Would be solved by something like:
Suggested change
| defer objectFile.Close() | |
| defer func() { | |
| if closeErr := objectFile.Close(); closeErr != nil && err == nil { | |
| err = closeErr | |
| } | |
| }() |
Author
There was a problem hiding this comment.
Good catch. The bare defer objectFile.Close() was wrong for a writable handle. I've replaced it with explicit close-on-error on both failure paths, matching the Azure BlobClient.FGetObject pattern (blob.go:328-336). Close errors are now logged instead of silently discarded.
objectFile opened with os.OpenFile is only closed on the happy path. If NewReader or io.Copy fails, the file descriptor leaks. Close objectFile on each error path with checked close errors, matching the pattern used by the Azure BlobClient.FGetObject implementation. The leak was introduced in fluxcd#434 (2021-09-01). Assisted-by: Grok <noreply@x.ai> Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
2638e7b to
4ec284f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
objectFileopened withos.OpenFileinGCSClient.FGetObjectis only closed on the happy path. IfNewReaderorio.Copyfails, the file descriptor leaks.Fix
Close
objectFileon each error path with checked close errors, matching the pattern used by the AzureBlobClient.FGetObjectimplementation (blob.go:328-336).The initial version used
defer objectFile.Close(), which CodeQL flagged as discarding the close error on a writable file handle (code-scanning/6). The updated fix uses explicit close-on-error instead, consistent with the existing Azure pattern.Error paths fixed
NewReaderfailsobjectFile.Close()called, error loggedio.CopyfailsobjectFile.Close()called, error loggedobjectFile.Close()checkedOrigin
The leak was introduced in #434 (2021-09-01) when GCS support was added.