From d284c4aec92975867b66528df7a34ee302fc4984 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 29 Jun 2020 20:42:26 -0700 Subject: [PATCH] Fix lock obtainment to avoid freeze --- config/config.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 14c8c63..82ef67d 100644 --- a/config/config.go +++ b/config/config.go @@ -132,7 +132,7 @@ func ReadConfiguration(path string) (*Configuration, error) { } // Track the location where we created this configuration. - c.path = path + c.unsafeSetPath(path) // Replace environment variables within the configuration file with their // values from the host system. @@ -209,6 +209,9 @@ func (c *Configuration) unsafeSetPath(path string) { // Returns the path for this configuration file. func (c *Configuration) GetPath() string { + c.RLock() + defer c.RUnlock() + return c.path } @@ -266,11 +269,10 @@ func (c *Configuration) setSystemUser(u *user.User) error { gid, _ := strconv.Atoi(u.Gid) c.Lock() - defer c.Unlock() - c.System.Username = u.Username c.System.User.Uid = uid c.System.User.Gid = gid + c.Unlock() return c.WriteToDisk() } @@ -331,6 +333,10 @@ func (c *Configuration) EnsureFilePermissions() error { // lock on the file. This prevents something else from writing at the exact same time and // leading to bad data conditions. func (c *Configuration) WriteToDisk() error { + // Obtain an exclusive write against the configuration file. + c.writeLock.Lock() + defer c.writeLock.Unlock() + ccopy := *c // If debugging is set with the flag, don't save that to the configuration file, otherwise // you'll always end up in debug mode. @@ -347,10 +353,6 @@ func (c *Configuration) WriteToDisk() error { return err } - // Obtain an exclusive write against the configuration file. - c.writeLock.Lock() - defer c.writeLock.Unlock() - if err := ioutil.WriteFile(c.GetPath(), b, 0644); err != nil { return err }