fs/cache: fix locking

This was causing `fatal error: sync: unlock of unlocked mutex` if a
panic ocurred in fsNewFs.
This commit is contained in:
Nick Craig-Wood 2019-06-19 10:43:26 +01:00
parent 628530362a
commit 6a2a075c14
1 changed files with 2 additions and 2 deletions

4
fs/cache/cache.go vendored
View File

@ -27,12 +27,10 @@ type cacheEntry struct {
// Get gets a fs.Fs named fsString either from the cache or creates it afresh // Get gets a fs.Fs named fsString either from the cache or creates it afresh
func Get(fsString string) (f fs.Fs, err error) { func Get(fsString string) (f fs.Fs, err error) {
fsCacheMu.Lock() fsCacheMu.Lock()
defer fsCacheMu.Unlock()
entry, ok := fsCache[fsString] entry, ok := fsCache[fsString]
if !ok { if !ok {
fsCacheMu.Unlock() // Unlock in case Get is called recursively fsCacheMu.Unlock() // Unlock in case Get is called recursively
f, err = fsNewFs(fsString) f, err = fsNewFs(fsString)
fsCacheMu.Lock()
if err != nil && err != fs.ErrorIsFile { if err != nil && err != fs.ErrorIsFile {
return f, err return f, err
} }
@ -41,8 +39,10 @@ func Get(fsString string) (f fs.Fs, err error) {
fsString: fsString, fsString: fsString,
err: err, err: err,
} }
fsCacheMu.Lock()
fsCache[fsString] = entry fsCache[fsString] = entry
} }
defer fsCacheMu.Unlock()
entry.lastUsed = time.Now() entry.lastUsed = time.Now()
if !expireRunning { if !expireRunning {
time.AfterFunc(cacheExpireInterval, cacheExpire) time.AfterFunc(cacheExpireInterval, cacheExpire)