-
Notifications
You must be signed in to change notification settings - Fork 3.9k
storage: fix an error message when trying to backup to local drive on… #41164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status:
complete! 0 of 0 LGTMs obtained (waiting on @danhhz, @darinpp, and @dt)
pkg/ccl/storageccl/export_storage.go, line 345 at r1 (raw file):
return errors.Wrapf(err, "syncing to local export tmp file %q", tmpP) } f.Close()
If io.Copy
or f.Sync()
return an error we'll leak this open file. Another alternative is to move the rename into a defer. You'd have to give name the error
return value. Something like:
func (l *localFileStorage) WriteFile(...) (err error) {
f, err := os.Create(tmpP)
...
defer func() {
f.Close()
if err == nil {
err = errors.Wrapf(os.Rename(tmpP, p), "renaming to local export file %q", p)
}
}()
// Copy, Sync, etc.
}
I'd also be fine with just expanding the That said, given the numerous existing caveats with |
a25f0a5
to
dc27a10
Compare
This is a good point but deferring the rename doesn't allow us to return rename errors. So I simply added an extra |
I changed it so we will now have always the deferred Close. So the new code is just an addition to the existing code and simply closes the file before rename. |
9ac235e
to
5fbbb44
Compare
is it okay to double-close? |
Yes. You will get an error |
Is that true on Unix?
|
5fbbb44
to
d706b37
Compare
I'd find it a tad easier to follow if we just got rid of the defer and called |
Ok. I changed it back to do the rename in the defer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for checking further into the double-close. This structure makes me feel safer.
Reviewable status:
complete! 1 of 0 LGTMs obtained (waiting on @danhhz and @petermattis)
bors r+ |
Build failed |
bors r+ |
41164: storage: fix an error message when trying to backup to local drive on… r=darinpp a=darinpp … Windows An often occuring pattern is: ``` { ... f, _ := os.Create(someFile) defer f.Close() ...rename or delete the file... } ``` This does not work on Windows by default. It can be made to work (kindof) when the file is opened with a special flag but the behaviour is not the same as on a posix system. There seems to be an effort to fix this in go as seen here golang/go#32088 Also the default for the newer Windows versions seems to be to allow this golang/go#32088 (comment) Until then - we should do an extra close before remove or delete (assuming these are the last operations). Fixes #41085 Release justification: bug fix for existing functionality Release note: None Co-authored-by: Darin <[email protected]>
Build failed |
d706b37
to
594cba8
Compare
… Windows An often occuring pattern is: { ... f, _ := os.Create(someFile) defer f.Close() ...rename or delete the file... } This does not work on Windows by default. It can be made to work (kindof) when the file is opened with a special flag but the behaviour is not the same as on a posix system. There seems to be an effort to fix this is go as seen here https://github.com/golang/go/issues/<i></i>32088 Also the default for the newer Windows versions seems to be to allow this https://github.com/golang/go/issues/<i></i>32088#issuecomment-502850674 Until then - we should defer a remove or delete to be done after the Close. Fixes cockroachdb#41085 Release justification: bug fix for existing functionality Release note: None
594cba8
to
adeff57
Compare
bors r+ |
41164: storage: fix an error message when trying to backup to local drive on… r=darinpp a=darinpp … Windows An often occuring pattern is: ``` { ... f, _ := os.Create(someFile) defer f.Close() ...rename or delete the file... } ``` This does not work on Windows by default. It can be made to work (kindof) when the file is opened with a special flag but the behaviour is not the same as on a posix system. There seems to be an effort to fix this in go as seen in issue https://github.com/golang/go/issues/<i></i>32088 Also the default for the newer Windows versions seems to be to allow this https://github.com/golang/go/issues/<i></i>32088#issuecomment-502850674 Until then - we should do an extra close before remove or delete (assuming these are the last operations). Fixes #41085 Release justification: bug fix for existing functionality Release note: None Co-authored-by: Darin <[email protected]>
Build succeeded |
… Windows
An often occuring pattern is:
This does not work on Windows by default. It can be made to work (kindof) when
the file is opened with a special flag but the behaviour is not the same as
on a posix system.
There seems to be an effort to fix this in go as seen in issue https://github.com/golang/go/issues/32088
Also the default for the newer Windows versions seems to be to allow this https://github.com/golang/go/issues/32088#issuecomment-502850674
Until then - we should do an extra close before remove or delete (assuming these are the last operations).
Fixes #41085
Release justification: bug fix for existing functionality
Release note: None