From 3884ba473419a93c9b6270d9d62ef1046df2518f Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 25 Aug 2020 07:53:56 +0200 Subject: [PATCH 1/2] Fix NRE introduced during nullability annotations --- .../src/System/IO/FileSystemWatcher.Linux.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 96bbb6597e42aa..91b9f14003627a 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -390,7 +390,11 @@ private void AddDirectoryWatchUnlocked(WatchedDirectory? parent, string director // of the world, but there's little that can be done about that.) if (directoryEntry.Parent != parent) { - directoryEntry.Parent?.Children!.Remove (directoryEntry); + if (directoryEntry.Parent != null) + { + directoryEntry.Parent.Children!.Remove(directoryEntry); + } + directoryEntry.Parent = parent; if (parent != null) { @@ -443,7 +447,11 @@ private void RemoveWatchedDirectory(WatchedDirectory directoryEntry, bool remove Debug.Assert (_includeSubdirectories); lock (SyncObj) { - directoryEntry.Parent?.Children!.Remove(directoryEntry); + if (directoryEntry.Parent != null) + { + directoryEntry.Parent.Children!.Remove(directoryEntry); + } + RemoveWatchedDirectoryUnlocked (directoryEntry, removeInotify); } } From 7f66f2c48116cca34ac1c3c195ed0e57d746be91 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 25 Aug 2020 14:01:28 +0200 Subject: [PATCH 2/2] Apply Jeff's suggestions from code review Co-authored-by: Jeff Handley --- .../src/System/IO/FileSystemWatcher.Linux.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 91b9f14003627a..ed4f1639d07a73 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -390,6 +390,7 @@ private void AddDirectoryWatchUnlocked(WatchedDirectory? parent, string director // of the world, but there's little that can be done about that.) if (directoryEntry.Parent != parent) { + // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected if (directoryEntry.Parent != null) { directoryEntry.Parent.Children!.Remove(directoryEntry); @@ -447,6 +448,7 @@ private void RemoveWatchedDirectory(WatchedDirectory directoryEntry, bool remove Debug.Assert (_includeSubdirectories); lock (SyncObj) { + // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected if (directoryEntry.Parent != null) { directoryEntry.Parent.Children!.Remove(directoryEntry);