Skip to content

Commit

Permalink
Fix config reload race
Browse files Browse the repository at this point in the history
Fix the config reload race following the comments #219.

Signed-off-by: Qi Wang <[email protected]>
  • Loading branch information
QiWang19 committed Jul 15, 2020
1 parent 6b96c50 commit 9c635e6
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,9 @@ func stringsEq(a, b []string) bool {
}

var (
configOnce sync.Once
configErr error
config *Config
configErr error
configMutex sync.Mutex
config *Config
)

// Default returns the default container config.
Expand All @@ -845,9 +845,12 @@ var (
// The system defaults container config files can be overwritten using the
// CONTAINERS_CONF environment variable. This is usually done for testing.
func Default() (*Config, error) {
configOnce.Do(func() {
config, configErr = NewConfig("")
})
configMutex.Lock()
defer configMutex.Unlock()
if config != nil || configErr != nil {
return config, configErr
}
config, configErr = NewConfig("")
return config, configErr
}

Expand Down Expand Up @@ -937,12 +940,13 @@ func (c *Config) Write() error {
return nil
}

// Reload reloads the configuration from containers.conf files
// Reload clean the cached config and reloads the configuration from containers.conf files
// This function is meant to be used for long-running processes that need to reload potential changes made to
// the cached containers.conf files.
func Reload() (*Config, error) {
var err error
config, err = NewConfig("")
if err != nil {
return nil, errors.Wrapf(err, "containers.conf reload failed")
}
configMutex.Lock()
configErr = nil
config = nil
configMutex.Unlock()
return Default()
}

0 comments on commit 9c635e6

Please sign in to comment.