From 9e91e3b2dde25c466a82a53c2f83beae69dbc7b9 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 19 Sep 2013 11:13:34 -0400 Subject: [PATCH] prevent DOS in directory watchers --- .../IO/DirectoryWatchers.cs | 55 ++++++------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs index 560b290fc..f069d40c1 100644 --- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs +++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs @@ -6,6 +6,7 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.ScheduledTasks; +using Microsoft.Win32; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -24,7 +25,7 @@ namespace MediaBrowser.Server.Implementations.IO /// /// The file system watchers /// - private readonly ConcurrentDictionary _fileSystemWatchers = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _fileSystemWatchers = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); /// /// The update timer /// @@ -100,6 +101,19 @@ namespace MediaBrowser.Server.Implementations.IO TaskManager = taskManager; Logger = logManager.GetLogger("DirectoryWatchers"); ConfigurationManager = configurationManager; + + SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; + } + + /// + /// Handles the PowerModeChanged event of the SystemEvents control. + /// + /// The source of the event. + /// The instance containing the event data. + void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + Stop(); + Start(); } /// @@ -288,47 +302,14 @@ namespace MediaBrowser.Server.Implementations.IO /// /// The source of the event. /// The instance containing the event data. - async void watcher_Error(object sender, ErrorEventArgs e) + void watcher_Error(object sender, ErrorEventArgs e) { var ex = e.GetException(); var dw = (FileSystemWatcher)sender; Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex); - //Network either dropped or, we are coming out of sleep and it hasn't reconnected yet - wait and retry - var retries = 0; - var success = false; - while (!success && retries < 10) - { - await Task.Delay(500).ConfigureAwait(false); - - try - { - dw.EnableRaisingEvents = false; - dw.EnableRaisingEvents = true; - success = true; - } - catch (ObjectDisposedException) - { - RemoveWatcherFromList(dw); - return; - } - catch (IOException) - { - Logger.Warn("Network still unavailable..."); - retries++; - } - catch (ApplicationException) - { - Logger.Warn("Network still unavailable..."); - retries++; - } - } - if (!success) - { - Logger.Warn("Unable to access network. Giving up."); - DisposeWatcher(dw); - } + DisposeWatcher(dw); } /// @@ -348,7 +329,7 @@ namespace MediaBrowser.Server.Implementations.IO var nameFromFullPath = Path.GetFileName(e.FullPath); // Ignore certain files - if (!string.IsNullOrEmpty(nameFromFullPath) && _alwaysIgnoreFiles.Contains(nameFromFullPath, StringComparer.OrdinalIgnoreCase)) + if (!string.IsNullOrEmpty(nameFromFullPath) && _alwaysIgnoreFiles.Contains(nameFromFullPath, StringComparer.OrdinalIgnoreCase)) { return; }