Skip to content

Commit

Permalink
fix: temporary file cleanup
Browse files Browse the repository at this point in the history
Improve Close error handling, clean up also if we exec successfully.
  • Loading branch information
scop committed Nov 15, 2023
1 parent a26603c commit c41f789
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions wrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,15 @@ Environment variables:
rc = 1
return
}
defer func() {
cleanUpTempFile := func() {
if closeErr := tmpf.Close(); err != nil && !errors.Is(closeErr, os.ErrClosed) {
warnOut("close tempfile: %v", closeErr)
}
if rmErr := os.Remove(tmpf.Name()); rmErr != nil && !errors.Is(rmErr, os.ErrNotExist) {
warnOut("remove tempfile: %v", rmErr)
}
}()
defer func() {
_ = tmpf.Close() // ignore error, we may have eagerly closed it already // TODO don't close multiple times, results are undefined
}()
}
defer cleanUpTempFile() // Note: does not happen if we exec successfully

// Download

Expand Down Expand Up @@ -397,6 +398,7 @@ Environment variables:

// Execute

cleanUpTempFile() // Note: deferred cleanup does not happen if we exec successfully
err = exec(exePath)
errorOut("exec: %v", err)
rc = 1
Expand Down

0 comments on commit c41f789

Please sign in to comment.