[FileSystemWatcher] Kill race condition (fixes BXC#10205)
authorAndrés G. Aragoneses <knocte@gmail.com>
Thu, 3 Dec 2015 05:35:54 +0000 (13:35 +0800)
committerAndrés G. Aragoneses <knocte@gmail.com>
Thu, 3 Dec 2015 05:35:54 +0000 (13:35 +0800)
As seen in the stacktraces provided in [1],
a thread could modify the Files hashtable
while other thread is enumerating it, so this
can be prevented by using a lock{} in the parts
of the code that access and modify this field.

Patch licensed as MIT/X11.

[1] https://bugzilla.xamarin.com/show_bug.cgi?id=10205

mcs/class/System/System.IO/DefaultWatcher.cs

index d2d8b28d86cef4ebbeeff516308503d14cf10fe2..d08e60984161333f17f2b4ebab9bb5dc8b75c5c6 100644 (file)
@@ -43,6 +43,8 @@ namespace System.IO {
                public bool Enabled;
                public bool NoWildcards;
                public DateTime DisabledTime;
+
+               public object FilesLock = new object ();
                public Hashtable Files;
        }
 
@@ -211,6 +213,13 @@ namespace System.IO {
                                        files = NoStringsArray;
                        }
 
+                       lock (data.FilesLock) {
+                               IterateAndModifyFilesData (data, directory, dispatch, files);
+                       }
+               }
+
+               void IterateAndModifyFilesData (DefaultWatcherData data, string directory, bool dispatch, string[] files)
+               {
                        /* Set all as untested */
                        foreach (string filename in data.Files.Keys) {
                                FileData fd = (FileData) data.Files [filename];