vfs: fix deadlock caused by cache cleaner and upload finishing

Before this patch a deadlock could occur if the cache cleaner was
running when an object upload finished.

This fixes the problem by delaying marking the object as clean until
we have notified the VFS layer. This means that the cache cleaner
won't consider the object until **after** the VFS layer has been
notified, thus avoiding the deadlock.

See: https://forum.rclone.org/t/rclone-mount-deadlock-when-dir-cache-time-strikes/33486/
This commit is contained in:
Nick Craig-Wood 2022-10-17 16:46:59 +01:00
parent 6d0824954b
commit 4ffe9dcfef

View File

@ -590,20 +590,25 @@ func (item *Item) _store(ctx context.Context, storeFn StoreFn) (err error) {
item._updateFingerprint() item._updateFingerprint()
} }
item.info.Dirty = false // Write the object back to the VFS layer before we mark it as
err = item._save() // clean, otherwise it will become eligible for removal which
if err != nil { // can cause a deadlock
fs.Errorf(item.name, "vfs cache: failed to write metadata file: %v", err)
}
if storeFn != nil && item.o != nil { if storeFn != nil && item.o != nil {
fs.Debugf(item.name, "vfs cache: writeback object to VFS layer") fs.Debugf(item.name, "vfs cache: writeback object to VFS layer")
// Write the object back to the VFS layer as last // Write the object back to the VFS layer last with mutex unlocked
// thing we do with mutex unlocked
o := item.o o := item.o
item.mu.Unlock() item.mu.Unlock()
storeFn(o) storeFn(o)
item.mu.Lock() item.mu.Lock()
} }
// Show item is clean and is elegible for cache removal
item.info.Dirty = false
err = item._save()
if err != nil {
fs.Errorf(item.name, "vfs cache: failed to write metadata file: %v", err)
}
return nil return nil
} }