Skip to content

Commit 233f04a

Browse files
authored
use stricter mutex policy to prevent installed.json corruption (#635)
* use stricter mutex policy to prevent installed.json corruption * move locking before installed.json read
1 parent 76ab40f commit 233f04a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

‎tools/tools.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ func (t *Tools) GetLocation(command string) (string, error) {
101101

102102
// writeMap() writes installed map to the json file "installed.json"
103103
func (t *Tools) writeMap() error {
104-
t.mutex.RLock()
104+
t.mutex.Lock()
105105
b, err := json.Marshal(t.installed)
106-
t.mutex.RUnlock()
106+
defer t.mutex.Unlock()
107107
if err != nil {
108108
return err
109109
}
@@ -113,13 +113,13 @@ func (t *Tools) writeMap() error {
113113

114114
// readMap() reads the installed map from json file "installed.json"
115115
func (t *Tools) readMap() error {
116+
t.mutex.Lock()
117+
defer t.mutex.Unlock()
116118
filePath := path.Join(dir(), "installed.json")
117119
b, err := ioutil.ReadFile(filePath)
118120
if err != nil {
119121
return err
120122
}
121-
t.mutex.Lock()
122-
defer t.mutex.Unlock()
123123
return json.Unmarshal(b, &t.installed)
124124
}
125125

0 commit comments

Comments
 (0)