Close each file after its transfer finished during directory upload/download - #3514
Open
wty-Bryant wants to merge 3 commits into
Open
Close each file after its transfer finished during directory upload/download#3514wty-Bryant wants to merge 3 commits into
wty-Bryant wants to merge 3 commits into
Conversation
Tongs2000
reviewed
Aug 12, 2026
Comment on lines
+310
to
+318
| defer func() { | ||
| if err := file.Close(); err != nil { | ||
| d.setErr(fmt.Errorf("error when closing file %s: %v", data.path, err)) | ||
| } | ||
| file, err := os.Create(data.path) | ||
| if err != nil { | ||
| d.setErr(fmt.Errorf("error when creating file %s: %v", data.path, err)) | ||
| continue | ||
| if fileCopyFail { | ||
| os.Remove(data.path) // only remove the file if the copy failed | ||
| } | ||
| n, err := io.Copy(file, out.Body) | ||
| }() | ||
| n, err := io.Copy(file, out.Body) |
Contributor
There was a problem hiding this comment.
Overall this looks great. One small corner case I wanted to confirm. In downloadSingleObject Since the defer runs before the caller's setErr, and the new setErr keeps the first non-cancellation error, if io.Copy fails and Close() also fails on the same object, the surfaced error will be the close error rather than the copy error. Which do we actually want to report? Same shape exists in uploadSingleFile.
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.
Fixes #3512 , this pr fixes the open file leak for both directory upload/download