From 6d074746fda26af1eb10d49c98128e6e4897429d Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 9 Mar 2023 13:01:59 -0500 Subject: [PATCH] Swap order of OnError in FileSystemWatcher.ReadDirectoryChangesCallback on Windows (#83121) If the directory watching encounters a failure error code other than abort, the Windows implementation raises the Error event and sets EnableRaisingEvents to false. However, because it does so in that order, the Error event handler may or may not see the FSW's EnableRaisingEvents as false, based on whether there's a SynchronizingObject that results in the callback being queued. By swapping the order to always disable first, we can ensure consistency regardless of SynchronizingObject, which also means the Error handler can choose based on the error to set EnableRaisingEvents to true again if it desires. --- .../src/System/IO/FileSystemWatcher.Win32.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs index ebf8f5facb3..8508ea648b3 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs @@ -218,8 +218,8 @@ private void ReadDirectoryChangesCallback(uint errorCode, uint numBytes, AsyncRe const int ERROR_OPERATION_ABORTED = 995; if (errorCode != ERROR_OPERATION_ABORTED) { - OnError(new ErrorEventArgs(new Win32Exception((int)errorCode))); EnableRaisingEvents = false; + OnError(new ErrorEventArgs(new Win32Exception((int)errorCode))); } return; } -- GitLab