Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / TaskAwaiter.cs
index 061d790942941ef001a5f2382d0d853a591835c1..07110cf2b75406173394e05f1380e1533c9359a5 100644 (file)
@@ -62,15 +62,18 @@ namespace System.Runtime.CompilerServices
 
                internal static Exception HandleUnexpectedTaskResult (Task task)
                {
+                       var slot = task.ExceptionSlot;
                        switch (task.Status) {
                        case TaskStatus.Canceled:
                                // Use original exception when we have one
-                               if (task.ExceptionSlot.Exception != null)
+                               if (slot.Exception != null)
                                        goto case TaskStatus.Faulted;
 
                                return new TaskCanceledException (task);
                        case TaskStatus.Faulted:
-                               return task.ExceptionSlot.Exception.InnerException;
+                               // Mark the exception as observed when GetResult throws
+                               slot.Observed = true;
+                               return slot.Exception.InnerException;
                        default:
                                throw new ArgumentException (string.Format ("Unexpected task `{0}' status `{1}'", task.Id, task.Status));
                        }
@@ -78,18 +81,18 @@ namespace System.Runtime.CompilerServices
 
                internal static void HandleOnCompleted (Task task, Action continuation, bool continueOnSourceContext, bool manageContext)
                {
-                       if (continueOnSourceContext && SynchronizationContext.Current != null) {
+                       if (continueOnSourceContext && SynchronizationContext.Current != null && SynchronizationContext.Current.GetType () != typeof (SynchronizationContext)) {
                                task.ContinueWith (new SynchronizationContextContinuation (continuation, SynchronizationContext.Current));
                        } else {
                                IContinuation cont;
                                Task cont_task;
-                               if (TaskScheduler.Current != TaskScheduler.Default) {
+                               if (continueOnSourceContext && !TaskScheduler.IsDefault) {
                                        cont_task = new Task (TaskActionInvoker.Create (continuation), null, CancellationToken.None, TaskCreationOptions.None, null);
                                        cont_task.SetupScheduler (TaskScheduler.Current);
                                        cont = new SchedulerAwaitContinuation (cont_task);
                                } else {
                                        cont_task = null;
-                                       cont = new ActionContinuation (continuation);
+                                       cont = new AwaiterActionContinuation (continuation);
                                }
 
                                //
@@ -104,7 +107,7 @@ namespace System.Runtime.CompilerServices
                                        cont_task.SetupScheduler (TaskScheduler.Current);
                                }
 
-                               cont_task.Schedule ();
+                               cont_task.Schedule (true);
                        }
                }