diff --git a/Shoko.Server/Utilities/FileSystemWatcher/RecoveringFileSystemWatcher.cs b/Shoko.Server/Utilities/FileSystemWatcher/RecoveringFileSystemWatcher.cs index e02af52ad..84648a709 100644 --- a/Shoko.Server/Utilities/FileSystemWatcher/RecoveringFileSystemWatcher.cs +++ b/Shoko.Server/Utilities/FileSystemWatcher/RecoveringFileSystemWatcher.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Shoko.Commons.Extensions; -using Shoko.Server.Server; namespace Shoko.Server.Utilities.FileSystemWatcher; @@ -151,6 +150,7 @@ private void WatcherOnError(object sender, ErrorEventArgs e) try { + _watcher = InitWatcher(); Start(); } catch (Exception ex) @@ -159,13 +159,19 @@ private void WatcherOnError(object sender, ErrorEventArgs e) } } + private void WatcherDisposed(object sender, EventArgs e) + { + _watcher = null; + } + public void Start() { if (_watcher is { EnableRaisingEvents: true } && _recoveringTimer != null) return; _watcher ??= InitWatcher(); - _watcher.EnableRaisingEvents = true; _recoveringTimer ??= new Timer(_recoveringTimerElapsed); _recoveringTimer?.Change(_directoryRetryInterval, Timeout.InfiniteTimeSpan); + // do this last to ensure the rest is done + _watcher.EnableRaisingEvents = true; } private void _recoveringTimerElapsed(object state) @@ -181,6 +187,7 @@ private void _recoveringTimerElapsed(object state) return; } + _watcher ??= InitWatcher(); Start(); } catch (Exception ex) @@ -228,6 +235,7 @@ private System.IO.FileSystemWatcher InitWatcher() watcher.Renamed += WatcherChangeDetected; watcher.Deleted += WatcherChangeDetected; watcher.Error += WatcherOnError; + watcher.Disposed += WatcherDisposed; return watcher; }