From 8755e852fa40d14338cc56e4780406c42c7c2237 Mon Sep 17 00:00:00 2001 From: Robin Nabel Date: Sun, 28 Aug 2022 10:24:33 +0100 Subject: [PATCH] Wrap accesses of c.osFilename and c.osMtime in mutex to prevent race condition. Signed-off-by: Robin Nabel --- collector/os_release.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/collector/os_release.go b/collector/os_release.go index cda1eb1f6a..01c61f81c6 100644 --- a/collector/os_release.go +++ b/collector/os_release.go @@ -59,7 +59,7 @@ type osReleaseCollector struct { os *osRelease osFilename string // file name of cached release information osMtime time.Time // mtime of cached release file - osMutex sync.Mutex + osMutex sync.RWMutex osReleaseFilenames []string // all os-release file names to check version float64 versionDesc *prometheus.Desc @@ -120,7 +120,10 @@ func (c *osReleaseCollector) UpdateStruct(path string) error { } t := stat.ModTime() - if path == c.osFilename && t == c.osMtime { + c.osMutex.RLock() + upToDate := path == c.osFilename && t == c.osMtime + c.osMutex.RUnlock() + if upToDate { // osReleaseCollector struct is already up-to-date. return nil }