From 24dc870c4ebbe21d31499a1231ddabaa7699096d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 9 Jul 2023 20:39:06 +1000 Subject: [PATCH] Use mutex on cached git config This fixes a race condition caused by a concurrent map read and write --- pkg/commands/git_config/cached_git_config.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/commands/git_config/cached_git_config.go b/pkg/commands/git_config/cached_git_config.go index fe3bc1eca49..da18d086663 100644 --- a/pkg/commands/git_config/cached_git_config.go +++ b/pkg/commands/git_config/cached_git_config.go @@ -3,6 +3,7 @@ package git_config import ( "os/exec" "strings" + "sync" "github.com/sirupsen/logrus" ) @@ -20,6 +21,7 @@ type CachedGitConfig struct { cache map[string]string runGitConfigCmd func(*exec.Cmd) (string, error) log *logrus.Entry + mutex sync.Mutex } func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig { @@ -31,10 +33,14 @@ func NewCachedGitConfig(runGitConfigCmd func(*exec.Cmd) (string, error), log *lo cache: make(map[string]string), runGitConfigCmd: runGitConfigCmd, log: log, + mutex: sync.Mutex{}, } } func (self *CachedGitConfig) Get(key string) string { + self.mutex.Lock() + defer self.mutex.Unlock() + if value, ok := self.cache[key]; ok { self.log.Debugf("using cache for key " + key) return value @@ -46,6 +52,9 @@ func (self *CachedGitConfig) Get(key string) string { } func (self *CachedGitConfig) GetGeneral(args string) string { + self.mutex.Lock() + defer self.mutex.Unlock() + if value, ok := self.cache[args]; ok { self.log.Debugf("using cache for args " + args) return value