Skip to content

Fix fileutil.CopyFile close semantics and error propagation#31164

Merged
pelikhan merged 3 commits intomainfrom
copilot/fix-double-close-fileutil
May 9, 2026
Merged

Fix fileutil.CopyFile close semantics and error propagation#31164
pelikhan merged 3 commits intomainfrom
copilot/fix-double-close-fileutil

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 9, 2026

  • Review prior PR feedback and inspect recent CopyFile changes
  • Update copyFileContents to remove partial output only after closing the destination
  • Narrow helper input from io.ReadCloser to io.Reader and keep ownership in CopyFile
  • Run targeted pkg/fileutil tests and final validation
  • Reply to the new PR comment with the addressing commit hash

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix double-close and improve error handling in fileutil Fix fileutil.CopyFile close semantics and error propagation May 9, 2026
Copilot AI requested a review from pelikhan May 9, 2026 05:24
@pelikhan pelikhan marked this pull request as ready for review May 9, 2026 05:30
Copilot AI review requested due to automatic review settings May 9, 2026 05:30
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

Fixes pkg/fileutil.CopyFile resource finalization so the destination is closed exactly once and Close() errors can be surfaced (without overriding earlier io.Copy / Sync() failures).

Changes:

  • Introduces copyFileContents helper to centralize io.CopySync()Close() sequencing and error precedence.
  • Updates CopyFile to rely on the helper (removing prior double-close/cleanup behavior).
  • Adds unit tests covering success-path Close() failure propagation and copy-failure precedence/cleanup.
Show a summary per file
File Description
pkg/fileutil/fileutil.go Refactors copy logic into a helper to fix close semantics and propagate close errors.
pkg/fileutil/fileutil_test.go Adds targeted tests for close-error propagation and copy-failure precedence/cleanup behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment thread pkg/fileutil/fileutil.go
Comment on lines +126 to +137
defer func() {
if closeErr := out.Close(); closeErr != nil && err == nil {
err = closeErr
}
}()

if _, err = io.Copy(out, in); err != nil {
if removeErr := os.Remove(dst); removeErr != nil {
log.Printf("Failed to remove partial destination file during cleanup: %s", removeErr)
}
return err
}
Comment thread pkg/fileutil/fileutil.go Outdated
Close() error
}

func copyFileContents(in io.ReadCloser, out syncWriteCloser, dst string) (err error) {
@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented May 9, 2026

@copilot Review all comments

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 9, 2026

@copilot Review all comments

Addressed the actionable review threads in e65499e: partial-file cleanup now runs after the destination is closed, and copyFileContents now takes an io.Reader to match ownership. Verified with go test ./pkg/fileutil/....

@pelikhan pelikhan merged commit 3701c2f into main May 9, 2026
@pelikhan pelikhan deleted the copilot/fix-double-close-fileutil branch May 9, 2026 11:33
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.

fileutil.CopyFile calls out.Close() twice on the error path and may swallow Close errors on success

3 participants