Skip to content

Close each file after its transfer finished during directory upload/download - #3514

Open
wty-Bryant wants to merge 3 commits into
mainfrom
fix-tm-directorytransfer-filehandleleak
Open

Close each file after its transfer finished during directory upload/download#3514
wty-Bryant wants to merge 3 commits into
mainfrom
fix-tm-directorytransfer-filehandleleak

Conversation

@wty-Bryant

Copy link
Copy Markdown
Contributor

Fixes #3512 , this pr fixes the open file leak for both directory upload/download

@wty-Bryant
wty-Bryant requested a review from a team August 12, 2026 22:12
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

transfermanager.UploadDirectory does not close files that it opens

2 participants