Skip to content

refactor: reworked the FileSystemWatcherMock - #1275

Open
pw-sgr wants to merge 27 commits into
Testably:mainfrom
pw-sgr:rewrite-filesystemwatcher-mock
Open

pw-sgr wants to merge 27 commits into
Testably:mainfrom
pw-sgr:rewrite-filesystemwatcher-mock

Conversation

@pw-sgr

@pw-sgr pw-sgr commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

While trying to fix #1227 a lot of follow up issues arose and their fixes had a lot of regressions (see #1232).

I also prepared a smaller fix, which only includes the buffer overflow, without the other fixes see #1274

To fix everything in one go, I copied the dotnet FileSystemWatcher and modified it to fit our mocked file system.
FileSystemWatcherMock.cs contains mostly the dotnet copied code
FileSystemWatcherMock.Testably.cs contains the platform specific implementation.
I intentionally kept these separate so changes on the dotnet one can be easier mirrored.

Removing of "Deep" in the events table, originally I added it because Windows had Changed events on it, but we didn't implement them, since we couldn't reliably get them to work on different windows versions, so Deep was equal to Nested.

pw-sgr and others added 6 commits November 30, 2026 17:03
…mWatcher

The mock now copies the runtime watcher and only replaces the platform part, so changes outside the watched path no longer fill the buffer and the watcher reacts like the real one per platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ent and prefix siblings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fter Dispose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e overflow gap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mergify

mergify Bot commented Dec 1, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5 · Fix concurrent event subscription updates and satisfy the explicit repository requirements before merging.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect event delivery and reporting across the mock file watcher feature.

Fix concurrent event subscription updates and satisfy the explicit repository requirements before merging.

Findings

  1. P1 Concurrent subscriptions lose handlers ▶
  2. P2 Interface documentation links removed ▶
  3. P2 Exceptions bypass shared helpers ▶
  4. P2 Existing encoding marker removed ▶

Summary

Reworks FileSystemWatcherMock into a shared watcher implementation and a mock-specific partial class.

  • Stopped watcher sessions drop their pending events.
  • Watchers now react to a watched directory's deletion or move by platform.
  • Filters and rename events now match framework and platform rules.

pw-sgr explicitly documents intentional gaps for process crashes from handlers, recursive deletion on macOS, Windows directory locks, buffer-overflow timing, and inconsistent Windows change events after directory renames. These were not reported as defects.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Mock file change] --> B[Check watched location and NotifyFilter]
  B --> C[Bounded event queue]
  C --> D[Read loop for this session]
  D --> E{Watched directory removed?}
  E -->|Yes| F[Apply platform behavior and stop if needed]
  F --> G[Raise Error when required]
  E -->|No| H[Match names and map rename events]
  H --> I[Call event handlers]
  I -->|Handler throws| J[Stop session]
  J --> K[Raise Error on Linux]
Loading

Reviews (1) · Last reviewed commit: "test(watcher): match the disposed object..." · Reviewed by Greptile

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Initialize (N=100) 341,418 B 341,466 B 🔴 +48 B (±0%)
RoundTrip (Size=65536) 198,043 B 198,029 B 🟢 -14 B (±0%)
MoveTree 128,265 B 128,284 B 🔴 +19 B (±0%)

Time

Benchmark main PR Δ
Initialize (N=10) 35.97 μs 30.75 μs 🟢 -15%
Initialize (N=100) 277.29 μs 244.84 μs 🟢 -12%
CreateMock 1.16 μs 1.57 μs 🔴 +35%
RoundTrip (Size=1024) 8.94 μs 6.33 μs 🟢 -29%
Inspect 1.53 μs 1.20 μs 🟢 -22%
Chunks 43.72 μs 35.57 μs 🟢 -19%
CompleteTestBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 15.38 μs 0.800 μs 0.709 μs 1.00 39.9 KB 1.00
SystemIOAbstractions 52.91 μs 0.536 μs 0.418 μs 3.45 166.7 KB 4.18
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.44GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

InitializationBenchmarks
Initialize N Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 10 30.75 μs 0.425 μs 0.398 μs 1.00 42.42 KB 1.00
SystemIOAbstractions 10 31.07 μs 0.224 μs 0.187 μs 1.01 48.59 KB 1.15
Testably 100 244.84 μs 1.935 μs 1.810 μs 1.00 333.46 KB 1.00
SystemIOAbstractions 100 249.97 μs 1.137 μs 1.008 μs 1.02 392.78 KB 1.18
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.76GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1.569 μs 0.0477 μs 0.0446 μs 1.00 5.21 KB 1.00
SystemIOAbstractions 3.837 μs 0.0653 μs 0.0611 μs 2.45 8.15 KB 1.56
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

FileRoundTripBenchmarks
RoundTrip Size Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1024 6.331 μs 2.2824 μs 2.1350 μs 1.14 4.26 KB 1.00
SystemIOAbstractions 1024 8.199 μs 2.7367 μs 2.5599 μs 1.48 16.54 KB 3.88
Testably 65536 94.309 μs 0.8028 μs 0.7116 μs 1.00 193.39 KB 1.00
SystemIOAbstractions 65536 65.631 μs 1.1452 μs 1.0712 μs 0.70 408.49 KB 2.11
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=4096  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

FileInfoBenchmarks
Inspect Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1.204 μs 0.1483 μs 0.1387 μs 1.01 1.52 KB 1.00
SystemIOAbstractions 1.101 μs 0.0153 μs 0.0120 μs 0.93 2.55 KB 1.69
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.22GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=65536  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

FileStreamBenchmarks
Chunks Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 35.57 μs 1.243 μs 1.163 μs 1.00 201.51 KB 1.00
SystemIOAbstractions 72.99 μs 1.245 μs 1.040 μs 2.05 326.67 KB 1.62
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=2048  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

DirectoryTreeBenchmarks
MoveTree Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 250.20 μs 16.754 μs 14.852 μs 1.00 125.28 KB 1.00
SystemIOAbstractions 61.94 μs 1.142 μs 1.068 μs 0.25 106.62 KB 0.85
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.92GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=512  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

Enumeration10Benchmarks
Enumerate N Pattern Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 10 ***** 5.396 μs 0.1880 μs 0.1468 μs 1.00 4.31 KB 1.00
SystemIOAbstractions 10 * 5.337 μs 0.1123 μs 0.0995 μs 0.99 3.93 KB 0.91
Testably 10 *.txt 5.276 μs 0.0640 μs 0.0567 μs 1.00 3.88 KB 1.00
SystemIOAbstractions 10 *.txt 8.290 μs 0.0215 μs 0.0179 μs 1.57 4.24 KB 1.09
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon Platinum 8370C CPU 2.80GHz (Max: 3.46GHz), 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=16384  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

Enumeration1000Benchmarks
Enumerate N Pattern Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1000 ***** 669.4 μs 8.63 μs 7.65 μs 1.00 263.55 KB 1.00
SystemIOAbstractions 1000 * 227.7 μs 0.39 μs 0.32 μs 0.34 70.42 KB 0.27
Testably 1000 *.txt 641.2 μs 3.18 μs 2.66 μs 1.00 213.2 KB 1.00
SystemIOAbstractions 1000 *.txt 468.7 μs 1.15 μs 0.90 μs 0.73 66.87 KB 0.31
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=512  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

pw-sgr and others added 13 commits December 2, 2026 12:59
…ET Framework

Matches the real .NET Framework watcher, which does not flow the ExecutionContext of the caller (test ported from the dotnet runtime). The mock tests now capture the test cancellation token outside of the handlers.
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
…irectories

Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
…threading

Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
…her kind

Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests).
…ing object

Mirrors the dotnet runtime InternalBufferSize_SynchronizingObject test, which only runs on Windows, for the mock on every simulated platform.
Ported from the dotnet runtime FileSystemWatcher tests (src/libraries/System.IO.FileSystem.Watcher/tests). The behavior was measured on Linux, macOS and Windows for .NET 8 to 11.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pw-sgr

pw-sgr commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Added the dotnet tests for the watcher too and the fixes

@vbreuss vbreuss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pw-sgr: I prefer the approach in this PR and really appreciate your effort, but I have some comments.

);
}

private static Task RunDelivery(Func<Task> delivery, CancellationToken token)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: On .NET Framework the delivery thread registers property reads in the statistics

Since the delivery no longer flows the ExecutionContext on .NET Framework, it also loses the AsyncLocal gate of the statistics that the EnableRaisingEvents setter holds while it starts the task. Every public property that the delivery thread reads is now recorded: after one delivered Created event, Statistics.FileSystemWatcher[path] contains SynchronizingObject{get;}, and a rename adds IncludeSubdirectories{get;}. On main and on .NET the statistics stay clean.

Suggestion: read the backing state instead of the public properties inside the mock: SynchronizingObject in InvokeOn, OnError and OnRenamed (Mock.cs:619, :761, :789), IncludeSubdirectories in IsInWatchedPath and the rename methods (Testably.cs:692, :846 and following), NotifyFilter in the predicate (Testably.cs:144) and InternalBufferSize in StartRaisingEvents (Testably.cs:87). That also saves the registration overhead on every change.

Repro test for Tests/Testably.Abstractions.Testing.Tests/Statistics/FileSystem/FileSystemWatcherStatisticsTests.cs (fails on net48 with "contained more than one property access: [EnableRaisingEvents{set;}, SynchronizingObject{get;}]", passes on net10.0):

[Test]
public async Task DeliveredEvent_ShouldNotRegisterPropertyAccess()
{
	MockFileSystem sut = new();
	sut.Initialize().WithSubdirectory("foo");
	using IFileSystemWatcher fileSystemWatcher = sut.FileSystemWatcher.New("foo");
	using ManualResetEventSlim ms = new();
	fileSystemWatcher.Created += (_, _) => ms.Set();
	fileSystemWatcher.EnableRaisingEvents = true;

	sut.Directory.CreateDirectory(sut.Path.Combine("foo", "bar"));

	await That(ms.Wait(10000)).IsTrue();
	await That(sut.Statistics.FileSystemWatcher["foo"])
		.OnlyContainsPropertySetAccess(nameof(IFileSystemWatcher.EnableRaisingEvents));
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fix it, could you check?


No platform reports a change of the watched directory itself, neither when its timestamps or attributes change nor when it is recreated after a deletion, also with `IncludeSubdirectories` and every `NotifyFilters` value. The mock ignores these changes as well.

## Known gaps

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Renames in a subdirectory of a watched symbolic link are not reported yet. I would either fix it or report it as a known gap.

…r is full

A move of the watched directory no longer takes a place in the buffer, and each queued change carries the location it was recorded under. Buffers, overflows and the removal of the watched directory now follow the measured behavior of Windows, Linux and macOS per .NET version.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileSystemWatcherMock: Buffer overflows from changes outside the watched path

2 participants