Skip to content

Commit 24dc870

Browse files
committed
Use mutex on cached git config
This fixes a race condition caused by a concurrent map read and write
1 parent aaf3ea5 commit 24dc870

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/commands/git_config/cached_git_config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git_config
33
import (
44
"os/exec"
55
"strings"
6+
"sync"
67

78
"github.com/sirupsen/logrus"
89
)
@@ -20,6 +21,7 @@ type CachedGitConfig struct {
2021
cache map[string]string
2122
runGitConfigCmd func(*exec.Cmd) (string, error)
2223
log *logrus.Entry
24+
mutex sync.Mutex
2325
}
2426

2527
func NewStdCachedGitConfig(log *logrus.Entry) *CachedGitConfig {
@@ -31,10 +33,14 @@ func NewCachedGitConfig(runGitConfigCmd func(*exec.Cmd) (string, error), log *lo
3133
cache: make(map[string]string),
3234
runGitConfigCmd: runGitConfigCmd,
3335
log: log,
36+
mutex: sync.Mutex{},
3437
}
3538
}
3639

3740
func (self *CachedGitConfig) Get(key string) string {
41+
self.mutex.Lock()
42+
defer self.mutex.Unlock()
43+
3844
if value, ok := self.cache[key]; ok {
3945
self.log.Debugf("using cache for key " + key)
4046
return value
@@ -46,6 +52,9 @@ func (self *CachedGitConfig) Get(key string) string {
4652
}
4753

4854
func (self *CachedGitConfig) GetGeneral(args string) string {
55+
self.mutex.Lock()
56+
defer self.mutex.Unlock()
57+
4958
if value, ok := self.cache[args]; ok {
5059
self.log.Debugf("using cache for args " + args)
5160
return value

0 commit comments

Comments
 (0)