Skip to content

Commit

Permalink
Add retry on file rename
Browse files Browse the repository at this point in the history
We use os.Rename to avoid potentially creating a situation where a
config file might be corrupted. This can however lead to a weird
situation when another process locks the file (i.e. Antivirus). Adding a
short retry loop should allow us to wait until the other process is
done.

[#172734111](https://www.pivotaltracker.com/story/show/172734111)

Co-authored-by: Sebastian Vidrio <svidrio@vmware.com>
  • Loading branch information
Lisa Burns and vitreuz committed Oct 23, 2020
1 parent 4996574 commit 04df8ae
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions util/configv3/write_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"syscall"
"time"
)

// WriteConfig creates the .cf directory and then writes the config.json. The
Expand Down Expand Up @@ -44,6 +45,14 @@ func (c *Config) WriteConfig() error {
return err
}

for i := 0; i < 5; i++ {
if err := os.Rename(tempConfigFileName, ConfigFilePath()); err == nil {
return nil
}

time.Sleep(50 * time.Millisecond)
}

return os.Rename(tempConfigFileName, ConfigFilePath())
}

Expand Down

0 comments on commit 04df8ae

Please sign in to comment.