Skip to content

Commit 419b88e

Browse files
committed
make NewMachine() safe for concurrent access
1 parent c11cf98 commit 419b88e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

netrc/netrc.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"os"
1111
"strings"
12+
"sync"
1213
"unicode"
1314
"unicode/utf8"
1415
)
@@ -37,9 +38,10 @@ var keywords = map[string]tkType{
3738
}
3839

3940
type Netrc struct {
40-
tokens []*token
41-
machines []*Machine
42-
macros Macros
41+
tokens []*token
42+
machines []*Machine
43+
macros Macros
44+
updateLock sync.Mutex
4345
}
4446

4547
// FindMachine returns the Machine in n named by name. If a machine named by
@@ -80,7 +82,9 @@ func (n *Netrc) MarshalText() (text []byte, err error) {
8082
}
8183

8284
func (n *Netrc) NewMachine(name, login, password, account string) *Machine {
83-
// TODO(bgentry): not safe for concurrency
85+
n.updateLock.Lock()
86+
defer n.updateLock.Unlock()
87+
8488
m := &Machine{
8589
Name: name,
8690
Login: login,

0 commit comments

Comments
 (0)