As part of the bug fix in https://github.com/dotnet/corefx/commit/dotnet/corefx@f2720abb758b50a9debb5ec146939ef6f5cef82d , a regression was introduced, one that we didn't catch until recently due to not having a test that stressed stopping and quickly restarting an FSW doing monitoring.
At the time of that fix, the active asynchronous monitoring operation didn't have its own copy of the directory handle; instead, it just used the single field on the FSW instance. As such, in a race condition between the asynchronous monitoring operation trying to use that instance and the main thread stopping the FSW and nulling out the field, the asynchronous monitoring operation could experience a NullReferenceException. The fix was to stop nulling out the field and just rely on closing the handle being enough to signal that monitoring should stop.
However, this causes a problem in a particular race condition. While a SafeHandle is in use by a P/Invoke operation, its reference count is temporarily incremented so as to prevent it from being closed while in use. This means that if the directory handle is disposed of while it's being used by ReadDirectoryChangesW, the handle's IsClosed won't return true until after ReadDirectoryChangesW returns. When the FSW is stopped, Dispose is called on the handle, and then when it's restarted, the handle's IsClosed is checked to see if there's currently an active monitoring operation, and if there is, the FSW assumes it doesn't need to restart anything. But if the stop and the start both occur during the call to ReadDirectoryChangesW, then the start won't actually start anything.
My reliability/performance fixes in https://github.com/dotnet/corefx/commit/dotnet/corefx@b6c478ccc9729e22ac7b9bf2248b644b51883db2 eliminated the reliance on the shared directory handle field: the asynchronous monitoring operation has its own copy. Thus, there's no longer a problem in nulling out the directory handle field. The fix for the regression is simply to do so.
(Long explanation for a fix that's simply to null out a field :-)
Commit migrated from https://github.com/dotnet/corefx/commit/6568da5ad7b4e5d516613f6a80865d5e88d72b31
The FSW tests still take ~10 seconds on my machine, most of which is spent waiting for events to not fire. This change just separates the tests out so that they're not all on the same class, letting xunit parallelize them. They now take ~3.5 seconds on my machine.
Commit migrated from https://github.com/dotnet/corefx/commit/2fbabe417accb662a6ed63a40506dea0268cf8e2
At least on Windows 10, writing to a file can cause directories in its parent directory structure to also be noted as modified, such that events will be raised for those directories. This is causing the FileSystemWatcher_Changed_PreSeededNestedStructure test to consistently fail. I've updated the test to instead look for and modify file attributes, instead.
Commit migrated from https://github.com/dotnet/corefx/commit/784403a8cb1afa56136cdc4abd79a5425653c7d8