-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NullReferenceException in FileSystemWatcher on Windows during dispose #100766
Conversation
Tagging subscribers to this area: @dotnet/area-system-io |
It's not thread-safe. If two threads are calling Dispose concurrently, that's a bug in the consumer. |
Sure, but trying to find where the exception was coming from it looks like both the Linux and macOS implementations of runtime/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs Lines 69 to 76 in 70e3218
runtime/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs Lines 98 to 103 in 70e3218
Windows seems to be the odd-one-out here, so seemed reasonable to me to add the null guard. I haven't looked into OTel's tests as to why it's happening - it could be an issue with their tests, it could be something in xunit as it appears to happen during test class cleanup. |
Looks like a standard xunit |
If the goal is consistency, then it'd be better to do: SafeFileHandle? handle = _directoryHandle;
if (handle is not null)
{
_directoryHandle = null;
handle.Dispose();
} That won't address other problems with this instance being erroneously used concurrently, though. |
Sure, I'll refactor that then. I went with |
Fix `NullReferenceException` in `FileSystemWatcher` on Windows if two threads try and dispose the instance at the same time.
Fix `NullReferenceException` in `FileSystemWatcher` on Windows if two threads try and dispose the instance at the same time.
Fix NullReferenceException in FileSystemWatcher on Windows if two threads try and dispose the instance at the same time.
Found in a failing test while running the CI in a PR against the OpenTelemetry libraries for net8.0 on Windows: workflow logs