-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Handle errors on close in config file write. #2595
Handle errors on close in config file write. #2595
Conversation
cb2ff4c
to
6ac4868
Compare
I'm not sure if this fixes anything, however I have seen some weird behavior on Windows where temp config files are left around and there doesn't seem to be any errors reported. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
6ac4868
to
d021730
Compare
Codecov Report
@@ Coverage Diff @@
## master #2595 +/- ##
==========================================
- Coverage 58.05% 58.04% -0.01%
==========================================
Files 295 295
Lines 21165 21170 +5
==========================================
+ Hits 12288 12289 +1
- Misses 7975 7977 +2
- Partials 902 904 +2 |
The existence of https://success.docker.com/article/warning-error-loading-configjson seems to indicate there is a problem here 😂 |
That seems to be about loading the config file, not saving? Or is that hitting this code?
Also wondering if that's related to empty config files, and if we should ignore empty files (just like we ignore empty Testing if we're still producing an error for empty files, and it looks like we do;
|
temp.Close() | ||
if retErr != nil { | ||
if err := os.Remove(temp.Name()); err != nil { | ||
logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file") |
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.
Given that in this case, we have exited early, and didn't update the actual ~/.docker/config.json
, perhaps we should print this as a warning?
Something like
WARNING: Failed to update config file: error cleaning up temp file
WDYT?
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.
We could, but we should already get an error returned.
Right but how did it get that way? |
The symptom I've seen is multiple temp files left behind and an empty config file, btw. |
defer func() { | ||
temp.Close() | ||
if retErr != nil { | ||
if err := os.Remove(temp.Name()); err != nil { |
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.
Not sure if there's a situation where that's possible (thinking if an error occurs on os.Rename()
; not sure how atomic that is); should we ignore errors if the file didn't exist?
if err := os.Remove(temp.Name()); err != nil { | |
if err := os.Remove(temp.Name()); err != nil && !os.IsNotExist(err) { |
Guess it's not needed 😅
Should we consider using something like https://github.com/moby/moby/blob/4609153995b59f54b507446eb2b2cca9ebbbf8e8/pkg/ioutils/fswriters.go#L13 or https://github.com/google/renameio (which seems to handle specific corner-cases)? Problem I see with that approach is that we would (at least not "out of the box") loose the Hm.. looks like we may need to have a look at that in general. Here's some fun thing I tried; mkdir -p ~/.docker
touch ~/real-config.json
ln -s ~/real-config.json ~/.docker/config.json
ls -la ~/.docker/config.json
# lrwxrwxrwx 1 root root 22 Jun 23 12:34 /root/.docker/config.json -> /root/real-config.json
docker login
# Username: thajeztah
# Password:
# Login Succeeded
ls -la ~/.docker/config.json
-rw-r--r-- 1 root root 229 Jun 23 12:36 /root/.docker/config.json Looks like we need to resolve symlinks before we rename. Something like; cfgFile := configFile.Filename
if f, err := os.Readlink(cfgFile); err == nil {
cfgFile = f
}
// Try copying the current config file (if any) ownership and permissions
copyFilePermissions(cfgFile, temp.Name())
return os.Rename(temp.Name(), cfgFile) |
Opened #2599 for consideration (if we think it's ok to ignore an empty config file); #2595 (comment) |
Ping |
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.
LGTM
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.
LGTM
I'm not sure if this fixes anything, however I have seen some weird
behavior on Windows where temp config files are left around and there
doesn't seem to be any errors reported, and an empty config.json.