[corlib] Mark all promise-style task exceptions observed. Fixes #17015
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / AsyncTaskMethodBuilder_T.cs
index 71cbb214ce6a5ef2619a12584e7be4c821d24774..9d539dfc42dc4f5bc56c71e5d94b8371432b5298 100644 (file)
@@ -69,15 +69,17 @@ namespace System.Runtime.CompilerServices
                
                public static AsyncTaskMethodBuilder<TResult> Create ()
                {
-                       var task = new Task<TResult> (TaskActionInvoker.Empty, null, CancellationToken.None, TaskCreationOptions.None, null);
+                       var task = new Task<TResult> (TaskActionInvoker.Promise, null, CancellationToken.None, TaskCreationOptions.None, null);
                        task.SetupScheduler (TaskScheduler.Current);
                        return new AsyncTaskMethodBuilder<TResult> (task);
                }
 
                public void SetException (Exception exception)
                {
-                       if (!task.TrySetException (new AggregateException (exception)))
-                               throw new InvalidOperationException ("The task has already completed");
+                       if (Task.TrySetException (new AggregateException (exception), exception is OperationCanceledException, true))
+                               return;
+
+                       throw new InvalidOperationException ("The task has already completed");
                }
 
                public void SetStateMachine (IAsyncStateMachine stateMachine)