Skip to content

Commit

Permalink
Wrap accesses of c.osFilename and c.osMtime in
Browse files Browse the repository at this point in the history
mutex to prevent race condition.

Signed-off-by: Robin Nabel <rnabel@ucdavis.edu>
  • Loading branch information
RNabel authored and discordianfish committed Sep 6, 2022
1 parent 4ad86b3 commit 8755e85
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions collector/os_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8755e85

Please sign in to comment.