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>
/// The file system watchers
/// </summary>
private ConcurrentBag<FileSystemWatcher> _fileSystemWatchers = new ConcurrentBag<FileSystemWatcher>();
private ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string,FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The update timer
/// </summary>
@ -209,12 +209,19 @@ namespace MediaBrowser.Server.Implementations.IO
newWatcher.Error += watcher_Error;
try
{
if (_fileSystemWatchers.TryAdd(path, newWatcher))
{
newWatcher.EnableRaisingEvents = true;
_fileSystemWatchers.Add(newWatcher);
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)
{
Logger.ErrorException("Error watching path: {0}", ex, path);
@ -232,9 +239,9 @@ namespace MediaBrowser.Server.Implementations.IO
/// <param name="path">The path.</param>
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);
}
@ -251,11 +258,18 @@ namespace MediaBrowser.Server.Implementations.IO
watcher.EnableRaisingEvents = false;
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>
@ -283,6 +297,11 @@ namespace MediaBrowser.Server.Implementations.IO
dw.EnableRaisingEvents = true;
success = true;
}
catch (ObjectDisposedException)
{
RemoveWatcherFromList(dw);
return;
}
catch (IOException)
{
Logger.Warn("Network still unavailable...");
@ -501,9 +520,7 @@ namespace MediaBrowser.Server.Implementations.IO
LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
FileSystemWatcher watcher;
while (_fileSystemWatchers.TryTake(out watcher))
foreach (var watcher in _fileSystemWatchers.Values.ToList())
{
watcher.Changed -= watcher_Changed;
watcher.EnableRaisingEvents = false;