Take FSRepo lock way earlier
There is no way this was safe before. Be careful to unlock on the error paths.
Tommi Virtanen committed
Mar 13, 2015 at 16:18 UTC
03bc2ccb9dfe4af966d9e8ca0f2e1801b36c489b
1 file changed
+13
-5
repo/fsrepo/fsrepo.go
+13
-5
@@ -87,6 +87,18 @@ func open(repoPath string) (repo.Repo, error) {
87
path: expPath,
88
}
89
90
+ r.lockfile, err = lockfile.Lock(r.path)
91
+ if err != nil {
92
+ return nil, err
93
+ }
94
+ keepLocked := false
95
+ defer func() {
96
+ // unlock on error, leave it locked on success
97
+ if !keepLocked {
98
+ r.lockfile.Close()
99
+ }
100
+ }()
101
+
102
if !isInitializedUnsynced(r.path) {
103
return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
104
}
@@ -108,11 +120,7 @@ func open(repoPath string) (repo.Repo, error) {
120
// log.Debugf("writing eventlogs to ...", c.path)
121
configureEventLoggerAtRepoPath(r.config, r.path)
122
111
- closer, err := lockfile.Lock(r.path)
112
- if err != nil {
113
- return nil, err
114
- }
115
- r.lockfile = closer
123
+ keepLocked = true
124
return r, nil
125
}
126