From 17b5b7c36800bb03165224337dae763754d58e57 Mon Sep 17 00:00:00 2001 From: Lawrence Murray Date: Fri, 20 Dec 2024 16:54:15 +0700 Subject: [PATCH] Ensure current directory hierarchy is being watched before returning from ChangeNotify(). --- backend/local/local.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/local/local.go b/backend/local/local.go index 9d86deb3b..0198ea2de 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -1269,20 +1269,23 @@ func (file *localOpenFile) Close() (err error) { // // Close the returned channel to stop being notified. func (f *Fs) ChangeNotify(ctx context.Context, notifyFunc func(string, fs.EntryType), pollIntervalChan <-chan time.Duration) { - go func() { - // Create new watcher - watcher, err := fsnotify.NewWatcher() - if err != nil { - fs.Errorf(f, "Failed to create watcher for local filesystem") - return - } + // Create new watcher, ensuring current directory hierarchy is being watched + // before returning + watcher, err := fsnotify.NewWatcher() + if err != nil { + fs.Errorf(f, "Failed to create watcher for local filesystem") + return + } + f.watchPath(watcher, f.root) + + go func(watcher *fsnotify.Watcher) { + // Close watcher when done defer func() { err := watcher.Close() if err != nil { fs.Debugf(f, "Failed to close watcher: %v", err) } }() - f.watchPath(watcher, f.root) // Process events and errors for { @@ -1336,7 +1339,7 @@ func (f *Fs) ChangeNotify(ctx context.Context, notifyFunc func(string, fs.EntryT fs.Debugf(f, "Error: %s", err.Error()) } } - }() + }(watcher) } // Watch a path, recursively