Suppress unobserved task exceptions for channel Completion tasks (#81652)
authorStephen Toub <stoub@microsoft.com>
Sun, 5 Feb 2023 23:24:06 +0000 (18:24 -0500)
committerGitHub <noreply@github.com>
Sun, 5 Feb 2023 23:24:06 +0000 (18:24 -0500)
src/libraries/System.Threading.Channels/src/System/Threading/Channels/ChannelUtilities.cs

index 2650166771a6975a53b6e3b02ae80596239f7ffc..54527392b579d4491dc2dfc5f4b5da55aa086a79 100644 (file)
@@ -35,7 +35,14 @@ namespace System.Threading.Channels
             }
             else if (error != null && error != s_doneWritingSentinel)
             {
-                tcs.TrySetException(error);
+                if (tcs.TrySetException(error))
+                {
+                    // Suppress unobserved exceptions from Completion tasks, as the exceptions will generally
+                    // have been surfaced elsewhere (which may end up making a consumer not consume the completion
+                    // task), and even if they weren't, they're created by a producer who will have "seen" them (in
+                    // contrast to them being created by some method call failing as part of user code).
+                    _ = tcs.Task.Exception;
+                }
             }
             else
             {