fixes #234 - Server Crash on Wake

This commit is contained in:
Luke Pulverenti 2013-05-05 09:40:44 -04:00
parent 7adc623930
commit f9807c618b

View file

@ -23,7 +23,7 @@ namespace MediaBrowser.Server.Implementations.IO
/// <summary> /// <summary>
/// The file system watchers /// The file system watchers
/// </summary> /// </summary>
private ConcurrentBag<FileSystemWatcher> _fileSystemWatchers = new ConcurrentBag<FileSystemWatcher>(); private ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string,FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
/// <summary> /// <summary>
/// The update timer /// The update timer
/// </summary> /// </summary>
@ -209,12 +209,19 @@ namespace MediaBrowser.Server.Implementations.IO
newWatcher.Error += watcher_Error; newWatcher.Error += watcher_Error;
try try
{
if (_fileSystemWatchers.TryAdd(path, newWatcher))
{ {
newWatcher.EnableRaisingEvents = true; newWatcher.EnableRaisingEvents = true;
_fileSystemWatchers.Add(newWatcher);
Logger.Info("Watching directory " + path); Logger.Info("Watching directory " + path);
} }
else
{
Logger.Info("Unable to add directory watcher for {0}. It already exists in the dictionary." + path);
newWatcher.Dispose();
}
}
catch (IOException ex) catch (IOException ex)
{ {
Logger.ErrorException("Error watching path: {0}", ex, path); Logger.ErrorException("Error watching path: {0}", ex, path);
@ -232,9 +239,9 @@ namespace MediaBrowser.Server.Implementations.IO
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
private void StopWatchingPath(string path) private void StopWatchingPath(string path)
{ {
var watcher = _fileSystemWatchers.FirstOrDefault(f => f.Path.Equals(path, StringComparison.OrdinalIgnoreCase)); FileSystemWatcher watcher;
if (watcher != null) if (_fileSystemWatchers.TryGetValue(path, out watcher))
{ {
DisposeWatcher(watcher); DisposeWatcher(watcher);
} }
@ -251,11 +258,18 @@ namespace MediaBrowser.Server.Implementations.IO
watcher.EnableRaisingEvents = false; watcher.EnableRaisingEvents = false;
watcher.Dispose(); watcher.Dispose();
var watchers = _fileSystemWatchers.ToList(); RemoveWatcherFromList(watcher);
}
watchers.Remove(watcher); /// <summary>
/// Removes the watcher from list.
/// </summary>
/// <param name="watcher">The watcher.</param>
private void RemoveWatcherFromList(FileSystemWatcher watcher)
{
FileSystemWatcher removed;
_fileSystemWatchers = new ConcurrentBag<FileSystemWatcher>(watchers); _fileSystemWatchers.TryRemove(watcher.Path, out removed);
} }
/// <summary> /// <summary>
@ -283,6 +297,11 @@ namespace MediaBrowser.Server.Implementations.IO
dw.EnableRaisingEvents = true; dw.EnableRaisingEvents = true;
success = true; success = true;
} }
catch (ObjectDisposedException)
{
RemoveWatcherFromList(dw);
return;
}
catch (IOException) catch (IOException)
{ {
Logger.Warn("Network still unavailable..."); Logger.Warn("Network still unavailable...");
@ -501,9 +520,7 @@ namespace MediaBrowser.Server.Implementations.IO
LibraryManager.ItemAdded -= LibraryManager_ItemAdded; LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved; LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
FileSystemWatcher watcher; foreach (var watcher in _fileSystemWatchers.Values.ToList())
while (_fileSystemWatchers.TryTake(out watcher))
{ {
watcher.Changed -= watcher_Changed; watcher.Changed -= watcher_Changed;
watcher.EnableRaisingEvents = false; watcher.EnableRaisingEvents = false;