Stop a disposing file watcher from crashing the process

A Windows CI leg failed with every test passing. FileLiberator.Tests
exited 0xE0434352:

  Unhandled exception. System.InvalidOperationException: The collection has
  been marked as complete with regards to additions.
     at BlockingCollection`1.Add(T item)
     at FileManager.BackgroundFileSystem.FileSystemWatcher_Changed(...)
     at FileSystemWatcher.ReadDirectoryChangesCallback(...)

Stop() disposes the watcher and then completes the collection, on the
assumption that disposing stops events. It does not stop the ones the OS
has already buffered, and on Windows those arrive on a native completion
callback, where an exception is not a failed call - it is a dead process.
So a Libation run that reinitialises or shuts down its file cache while
the Books directory is busy can take the app with it, which is the same
race the tests hit.

Adding to a completed collection is now caught and the event dropped.
That is the right answer rather than a swallow: whoever called Stop() is
either reinitialising, which rebuilds the cache from disk, or disposing.
Stop() also detaches its handlers before disposing and clears the fields,
which narrows the window and makes a second Stop() harmless - the
collection field is cleared only after CompleteAdding, since the
background scanner is waiting on that.

First tests for the class, since it had none: dispose under a flood of
events, dispose twice, and find a file created before and after
construction. They cannot prove this fix - the crash does not reproduce on
Linux even with the original code, because inotify does not deliver
post-dispose events the way Windows does. Windows CI is the only place
that can, so the guard is aimed there.

Co-authored-by: rmcrackan <rmcrackan@gmail.com>
This commit is contained in:
Cursor Agent
2026-08-17 13:33:52 +00:00
parent b9aa51cd00
commit d27445a128
2 changed files with 135 additions and 5 deletions

View File

@@ -85,8 +85,18 @@ public class BackgroundFileSystem : IDisposable
}
private void Stop()
{
//Stop raising events
fileSystemWatcher?.Dispose();
//Stop raising events. Detach first so a handler cannot run after this returns, and clear the field so a
//second Stop() is harmless.
var watcher = fileSystemWatcher;
fileSystemWatcher = null;
if (watcher is not null)
{
watcher.Created -= FileSystemWatcher_Changed;
watcher.Deleted -= FileSystemWatcher_Changed;
watcher.Renamed -= FileSystemWatcher_Changed;
watcher.Error -= FileSystemWatcher_Error;
watcher.Dispose();
}
try
{
@@ -99,8 +109,12 @@ public class BackgroundFileSystem : IDisposable
//Wait for background scanner to terminate before reinitializing.
backgroundScanner?.Wait();
//Dispose of directoryChangesEvents after backgroundScanner exists.
directoryChangesEvents?.Dispose();
//Dispose of directoryChangesEvents after backgroundScanner exists. Clear the field first so a late event
//has nothing to add to. CompleteAdding has to have happened while it was still set, or the scanner would
//still be blocked waiting for it.
var events = directoryChangesEvents;
directoryChangesEvents = null;
events?.Dispose();
lock (fsCacheLocker)
fsCache.Clear();
@@ -113,7 +127,18 @@ public class BackgroundFileSystem : IDisposable
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
directoryChangesEvents?.Add(e);
try
{
directoryChangesEvents?.Add(e);
}
// Stop() completes and disposes the collection, but events the OS had already buffered still arrive after
// that. On Windows they arrive on a native completion callback, where an exception does not fail a call -
// it takes the process down. Dropping them is correct: whoever called Stop() is either reinitializing,
// which rebuilds the cache from disk, or disposing.
//
// Covers both ways the collection refuses: completed for additions, and disposed, whose
// ObjectDisposedException derives from this.
catch (InvalidOperationException) { }
}
#region Background Thread