Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lambdageek
Copy link
Member

@lambdageek lambdageek commented Mar 9, 2020

In task.GetAwaiter().OnCompleted(action) if action throws, the TaskContinuation.RunOrScheduleAction method will catch the exception and then call RuntimeAugments.ReportUnhandledException. We must not simply throw the exception again - in that case we will lose all the frames leading up to the catch in RunOrScheduleAction. Instead use ExceptionDispatchInfo to capture the original throw.

Addresses #19166

@lambdageek
Copy link
Member Author

With this PR, the example from the bug report has a much more complete stack trace:

System.ArgumentException: Lets have an argument
  at MainProgram.Program.Bar () [0x0003b] in <242631dbabb943ca80db553f55aa3f84>:0 
  at MainProgram.Program.TheCallback (System.IAsyncResult res) [0x00013] in <242631dbabb943ca80db553f55aa3f84>:0 
  at BugReport.System.Threading.Tasks.TaskToApm+<>c__DisplayClass3_0.<InvokeCallbackWhenTaskCompletes>b__0 () [0x00000] in <242631dbabb943ca80db553f55aa3f84>:0 
  at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x0001a] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:676 
--- End of stack trace from previous location where exception was thrown ---

  at Internal.Runtime.Augments.RuntimeAugments.ReportUnhandledException (System.Exception exception) [0x00006] in /Users/alklig/work/mono/mcs/class/corlib/corert/RuntimeAugments.cs:12 
  at System.Threading.Tasks.AwaitTaskContinuation.ThrowAsyncIfNecessary (System.Exception exc) [0x00000] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:709 
  at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00022] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:680 
  at System.Threading.Tasks.Task.FinishContinuations () [0x00162] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:3239 
  at System.Threading.Tasks.Task.FinishStageThree () [0x0003c] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2222 
  at System.Threading.Tasks.Task`1[TResult].TrySetResult (TResult result) [0x0004f] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:422 
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].SetResult (TResult result) [0x0003d] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:608 
  at MainProgram.Program.Foo () [0x00097] in <242631dbabb943ca80db553f55aa3f84>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1092 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:968 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:910 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00024] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1073 
  at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x0001a] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:676 
--- End of stack trace from previous location where exception was thrown ---

  at Internal.Runtime.Augments.RuntimeAugments.ReportUnhandledException (System.Exception exception) [0x00006] in /Users/alklig/work/mono/mcs/class/corlib/corert/RuntimeAugments.cs:12 
  at System.Threading.Tasks.AwaitTaskContinuation.ThrowAsyncIfNecessary (System.Exception exc) [0x00000] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:709 
  at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00022] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:680 
  at System.Threading.Tasks.Task.FinishContinuations () [0x00052] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:3165 
  at System.Threading.Tasks.Task.FinishStageThree () [0x0003c] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2222 
  at System.Threading.Tasks.Task`1[TResult].TrySetResult (TResult result) [0x0004f] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:422 
  at System.Threading.Tasks.Task+DelayPromise.Complete () [0x00034] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:5349 
  at System.Threading.Tasks.Task+<>c.<Delay>b__247_1 (System.Object state) [0x00000] in /Users/alklig/work/mono/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:5311 
  at System.Threading.Timer+Scheduler.TimerCB (System.Object o) [0x00007] in /Users/alklig/work/mono/mcs/class/corlib/System.Threading/Timer.cs:369 
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00015] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1337 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:899 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in /Users/alklig/work/mono/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1261 

@lambdageek
Copy link
Member Author

@alexischr alexischr changed the title [corlib] Capture the ExceptionDispathInfo when rethrowing from TaskContinuation [corlib] Capture the ExceptionDispatchInfo when rethrowing from TaskContinuation Mar 9, 2020
…ontinuation

In `task.GetAwaiter().OnCompleted(action)` if `action` throws, the
`TaskContinuation.RunOrScheduleAction method will catch the exception and then
call `RuntimeAugments.ReportUnhandledException`.  We must not simply throw the
exception again - in that case we will lose all the frames leading up to the
`catch` in `RunOrScheduleAction`.  Instead use ExceptionDispatchInfo to
caputure the original throw.

Addresses mono#19166
@lambdageek
Copy link
Member Author

We need to backport this all the way back to 2019-10 for @kdubau

For annoying reasons we can't run this as a simple NUnit test because the
exception is unhandled on some arbitrary threadpool worker thread, and those
don't show up within an NUnit setup.

Additionally because the unhandled exception in the root domain would normally
terminate the application, we need to run this in a new appdomain.
@lambdageek
Copy link
Member Author

@monojenkins backport to 2020-02
@monojenkins backport to 2019-12
@monojenkins backport to 2019-10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants