From ac23ddf5cbe2aee992016f62e5706b1d4d51e105 Mon Sep 17 00:00:00 2001 From: "Alexander J. Maidak" Date: Sat, 12 Aug 2023 17:32:46 -0500 Subject: [PATCH] Fix race condition in ReloadConfig Calling ReloadConfig was not thread safe in spite of mutex. One thread could overwrite the package level cfg pointer after it was mutated by another thread. Signed-off-by: Alexander J. Maidak --- config/config.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index 713835c2..dc162d9a 100644 --- a/config/config.go +++ b/config/config.go @@ -45,8 +45,6 @@ var ( Help: "Timestamp of the last successful configuration reload.", }) - cfg *ini.File - opts = ini.LoadOptions{ // Do not error on nonexistent file to allow empty string as filename input Loose: true, @@ -97,12 +95,13 @@ func (ch *MySqlConfigHandler) ReloadConfig(filename string, mysqldAddress string } }() - if cfg, err = ini.LoadSources( + cfg, err := ini.LoadSources( opts, []byte("[client]\npassword = ${MYSQLD_EXPORTER_PASSWORD}\n"), filename, - ); err != nil { - return fmt.Errorf("failed to load %s: %w", filename, err) + ) + if err != nil { + return fmt.Errorf("failed to load config from %s: %w", filename, err) } if host, port, err = net.SplitHostPort(mysqldAddress); err != nil { @@ -135,12 +134,6 @@ func (ch *MySqlConfigHandler) ReloadConfig(filename string, mysqldAddress string TlsInsecureSkipVerify: tlsInsecureSkipVerify, } - // FIXME: this error check seems orphaned - if err != nil { - level.Error(logger).Log("msg", "failed to load config", "section", sectionName, "err", err) - continue - } - err = sec.StrictMapTo(mysqlcfg) if err != nil { level.Error(logger).Log("msg", "failed to parse config", "section", sectionName, "err", err)