Skip to content

Commit

Permalink
Fix NullReferenceException
Browse files Browse the repository at this point in the history
Fix `NullReferenceException` in `FileSystemWatcher` on Windows if two threads try and dispose the instance at the same time.
  • Loading branch information
martincostello committed Apr 9, 2024
1 parent ae184d5 commit 6f2b29f
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ private void StopRaisingEvents()
// increased, such that the disposal operation won't take effect and close the handle
// until that P/Invoke returns; if during that time the FSW is restarted, the IsHandleInvalid
// check will see a valid handle, unless we also null it out.
_directoryHandle.Dispose();
_directoryHandle = null;
SafeFileHandle? handle = _directoryHandle;
if (handle is not null)
{
_directoryHandle = null;
handle.Dispose();
}
}

private void FinalizeDispose()
Expand Down

0 comments on commit 6f2b29f

Please sign in to comment.