Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IFileSystemWatcher instances not being removed from AggregateFSWs #32

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Zio/FileSystems/AggregateFileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void RemoveFrom(IFileSystem fileSystem)
}

UnregisterEvents(watcher);
_children.Remove(watcher);
_children.RemoveAt(i);
watcher.Dispose();
}
}
Expand All @@ -100,7 +100,7 @@ public void Clear(IFileSystem excludeFileSystem = null)
}

UnregisterEvents(watcher);
_children.Remove(watcher);
_children.RemoveAt(i);
watcher.Dispose();
}
}
Expand Down
46 changes: 21 additions & 25 deletions src/Zio/FileSystems/MountFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ protected override void Dispose(bool disposing)

lock (_mounts)
{
lock (_aggregateWatchers)
{
foreach (var watcher in _aggregateWatchers)
{
watcher.Dispose();
}

_aggregateWatchers.Clear();
}

if (Owned)
{
foreach (var kvp in _mounts)
Expand All @@ -60,16 +70,6 @@ protected override void Dispose(bool disposing)

_mounts.Clear();
}

lock (_aggregateWatchers)
{
foreach (var watcher in _aggregateWatchers)
{
watcher.Dispose();
}

_aggregateWatchers.Clear();
}
}

/// <summary>
Expand Down Expand Up @@ -112,7 +112,7 @@ public void Mount(UPath name, IFileSystem fileSystem)
if (fileSystem.CanWatch(remainingPath))
{
var internalWatcher = fileSystem.Watch(remainingPath);
watcher.Add(new Watcher(this, name, remainingPath, internalWatcher));
watcher.Add(new Watcher(fileSystem, name, remainingPath, internalWatcher));
}
}
}
Expand Down Expand Up @@ -170,15 +170,15 @@ public IFileSystem Unmount(UPath name)
throw new ArgumentException($"The mount with the name `{name}` was not found");
}

_mounts.Remove(name);
}

lock (_aggregateWatchers)
{
foreach (var watcher in _aggregateWatchers)
lock (_aggregateWatchers)
{
watcher.RemoveFrom(mountFileSystem);
foreach (var watcher in _aggregateWatchers)
{
watcher.RemoveFrom(mountFileSystem);
}
}

_mounts.Remove(name);
}

return mountFileSystem;
Expand Down Expand Up @@ -704,14 +704,14 @@ protected override IFileSystemWatcher WatchImpl(UPath path)
if (kvp.Value.CanWatch(remainingPath))
{
var internalWatcher = kvp.Value.Watch(remainingPath);
watcher.Add(new Watcher(this, kvp.Key, remainingPath, internalWatcher));
watcher.Add(new Watcher(kvp.Value, kvp.Key, remainingPath, internalWatcher));
}
}

if (NextFileSystem != null && NextFileSystem.CanWatch(path))
{
var internalWatcher = NextFileSystem.Watch(path);
watcher.Add(new Watcher(this, null, path, internalWatcher));
watcher.Add(new Watcher(NextFileSystem, null, path, internalWatcher));
}

_aggregateWatchers.Add(watcher);
Expand All @@ -724,14 +724,10 @@ private class Watcher : WrapFileSystemWatcher
{
private readonly UPath _mountPath;

public IFileSystem MountFileSystem { get; }

public Watcher(MountFileSystem fileSystem, UPath mountPath, UPath path, IFileSystemWatcher watcher)
public Watcher(IFileSystem fileSystem, UPath mountPath, UPath path, IFileSystemWatcher watcher)
: base(fileSystem, path, watcher)
{
_mountPath = mountPath;

MountFileSystem = watcher.FileSystem;
}

protected override UPath? TryConvertPath(UPath pathFromEvent)
Expand Down