prevent DOS in directory watchers

pull/702/head
Luke Pulverenti 11 years ago
parent d580abcd65
commit 9e91e3b2dd

@ -6,6 +6,7 @@ using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Implementations.ScheduledTasks; using MediaBrowser.Server.Implementations.ScheduledTasks;
using Microsoft.Win32;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -100,6 +101,19 @@ namespace MediaBrowser.Server.Implementations.IO
TaskManager = taskManager; TaskManager = taskManager;
Logger = logManager.GetLogger("DirectoryWatchers"); Logger = logManager.GetLogger("DirectoryWatchers");
ConfigurationManager = configurationManager; ConfigurationManager = configurationManager;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}
/// <summary>
/// Handles the PowerModeChanged event of the SystemEvents control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PowerModeChangedEventArgs"/> instance containing the event data.</param>
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Stop();
Start();
} }
/// <summary> /// <summary>
@ -288,48 +302,15 @@ namespace MediaBrowser.Server.Implementations.IO
/// </summary> /// </summary>
/// <param name="sender">The source of the event.</param> /// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ErrorEventArgs" /> instance containing the event data.</param> /// <param name="e">The <see cref="ErrorEventArgs" /> instance containing the event data.</param>
async void watcher_Error(object sender, ErrorEventArgs e) void watcher_Error(object sender, ErrorEventArgs e)
{ {
var ex = e.GetException(); var ex = e.GetException();
var dw = (FileSystemWatcher)sender; var dw = (FileSystemWatcher)sender;
Logger.ErrorException("Error in Directory watcher for: " + dw.Path, ex); 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);
} }
}
/// <summary> /// <summary>
/// Handles the Changed event of the watcher control. /// Handles the Changed event of the watcher control.

Loading…
Cancel
Save