From 04df8ae6dd68b64c2080c863c1ddf4c3e06abcd6 Mon Sep 17 00:00:00 2001 From: Lisa Burns Date: Fri, 23 Oct 2020 22:39:13 +0000 Subject: [PATCH] Add retry on file rename 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 --- util/configv3/write_config.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/util/configv3/write_config.go b/util/configv3/write_config.go index 11e2d7481c7..15f02637820 100644 --- a/util/configv3/write_config.go +++ b/util/configv3/write_config.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "syscall" + "time" ) // WriteConfig creates the .cf directory and then writes the config.json. The @@ -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()) }