fix innocuous data-race when config.Load called in parallel

Locking was removed in https://github.com/docker/cli/pull/3025 which
allows for parallel calls to config.Load to modify global state.
The consequence in this case is innocuous, but it does trigger a
`DATA RACE` exception when tests run with `-race` option.

Signed-off-by: coryb <cbennett@netflix.com>
(cherry picked from commit b5f4a6e45f)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
coryb 2021-08-21 13:14:11 -07:00 committed by Sebastiaan van Stijn
parent e91ed5707e
commit ee1ac1b319
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 13 additions and 9 deletions

View File

@ -104,14 +104,18 @@ func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
return &configFile, err return &configFile, err
} }
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
var printLegacyFileWarning bool
// Load reads the configuration files in the given directory, and sets up // Load reads the configuration files in the given directory, and sets up
// the auth config information and returns values. // the auth config information and returns values.
// FIXME: use the internal golang config parser // FIXME: use the internal golang config parser
func Load(configDir string) (*configfile.ConfigFile, error) { func Load(configDir string) (*configfile.ConfigFile, error) {
printLegacyFileWarning = false cfg, _, err := load(configDir)
return cfg, err
}
// TODO remove this temporary hack, which is used to warn about the deprecated ~/.dockercfg file
// so we can remove the bool return value and collapse this back into `Load`
func load(configDir string) (*configfile.ConfigFile, bool, error) {
printLegacyFileWarning := false
if configDir == "" { if configDir == "" {
configDir = Dir() configDir = Dir()
@ -127,11 +131,11 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
if err != nil { if err != nil {
err = errors.Wrap(err, filename) err = errors.Wrap(err, filename)
} }
return configFile, err return configFile, printLegacyFileWarning, err
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
// if file is there but we can't stat it for any reason other // if file is there but we can't stat it for any reason other
// than it doesn't exist then stop // than it doesn't exist then stop
return configFile, errors.Wrap(err, filename) return configFile, printLegacyFileWarning, errors.Wrap(err, filename)
} }
// Can't find latest config file so check for the old one // Can't find latest config file so check for the old one
@ -140,16 +144,16 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
printLegacyFileWarning = true printLegacyFileWarning = true
defer file.Close() defer file.Close()
if err := configFile.LegacyLoadFromReader(file); err != nil { if err := configFile.LegacyLoadFromReader(file); err != nil {
return configFile, errors.Wrap(err, filename) return configFile, printLegacyFileWarning, errors.Wrap(err, filename)
} }
} }
return configFile, nil return configFile, printLegacyFileWarning, nil
} }
// LoadDefaultConfigFile attempts to load the default config file and returns // LoadDefaultConfigFile attempts to load the default config file and returns
// an initialized ConfigFile struct if none is found. // an initialized ConfigFile struct if none is found.
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile { func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
configFile, err := Load(Dir()) configFile, printLegacyFileWarning, err := load(Dir())
if err != nil { if err != nil {
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err) fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
} }