@@ -15,7 +15,7 @@ namespace Zio.FileSystems
1515 public class AggregateFileSystem : ReadOnlyFileSystem
1616 {
1717 private readonly List < IFileSystem > _fileSystems ;
18- private readonly List < AggregateFileSystemWatcher > _watchers ;
18+ private readonly List < Watcher > _watchers ;
1919
2020 /// <summary>
2121 /// Initializes a new instance of the <see cref="AggregateFileSystem"/> class.
@@ -34,7 +34,7 @@ public AggregateFileSystem(bool owned = true) : this(null, owned)
3434 public AggregateFileSystem ( IFileSystem fileSystem , bool owned = true ) : base ( fileSystem , owned )
3535 {
3636 _fileSystems = new List < IFileSystem > ( ) ;
37- _watchers = new List < AggregateFileSystemWatcher > ( ) ;
37+ _watchers = new List < Watcher > ( ) ;
3838 }
3939
4040 protected override void Dispose ( bool disposing )
@@ -57,10 +57,7 @@ protected override void Dispose(bool disposing)
5757 }
5858
5959 _fileSystems . Clear ( ) ;
60- }
6160
62- lock ( _watchers )
63- {
6461 foreach ( var watcher in _watchers )
6562 {
6663 watcher . Dispose ( ) ;
@@ -418,7 +415,7 @@ protected override IFileSystemWatcher WatchImpl(UPath path)
418415 {
419416 lock ( _fileSystems )
420417 {
421- var watcher = new AggregateFileSystemWatcher ( this , path ) ;
418+ var watcher = new Watcher ( this , path ) ;
422419
423420 if ( NextFileSystem != null && NextFileSystem . CanWatch ( path ) )
424421 {
@@ -438,6 +435,30 @@ protected override IFileSystemWatcher WatchImpl(UPath path)
438435 }
439436 }
440437
438+ private class Watcher : AggregateFileSystemWatcher
439+ {
440+ private readonly AggregateFileSystem _fileSystem ;
441+
442+ public Watcher ( AggregateFileSystem fileSystem , UPath path )
443+ : base ( fileSystem , path )
444+ {
445+ _fileSystem = fileSystem ;
446+ }
447+
448+ protected override void Dispose ( bool disposing )
449+ {
450+ base . Dispose ( disposing ) ;
451+
452+ if ( disposing && ! _fileSystem . IsDisposing )
453+ {
454+ lock ( _fileSystem . _fileSystems )
455+ {
456+ _fileSystem . _watchers . Remove ( this ) ;
457+ }
458+ }
459+ }
460+ }
461+
441462 // ----------------------------------------------
442463 // Internals API
443464 // Used to retrieve the correct paths
0 commit comments