diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-09 20:39:06 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-09 21:30:19 +1000 |
commit | 8964cedf27cbdb81f59e2400cfc89684d7458605 (patch) | |
tree | 4ec426282b79b8d803470d720bfd8464b1c7d0bf | |
parent | 14ecc15e71f648a95dab297ce9360d0badb6669a (diff) |
Use mutex on cached git config
This fixes a race condition caused by a concurrent map read and write
-rw-r--r-- | pkg/commands/git_config/cached_git_config.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/pkg/commands/git_config/cached_git_config.go b/pkg/commands/git_config/cached_git_config.go index fe3bc1eca..da18d0866 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 |