现在我知道首先使用Marshal.GetExceptionCode()
是一个hack,但问题不在于它(Visual Studio调试器也检测到活动异常)
private static async Task TestAsync()
{
Log("TestAsync.Before");
await HandleExceptionAsync();
Log("TestAsync.After");
}
private static async Task HandleExceptionAsync()
{
try
{
Log("HandleExceptionAsync.Try");
await ThrowAsync();
}
catch (InvalidOperationException)
{
Log("HandleExceptionAsync.Catch");
}
Log("HandleExceptionAsync.AfterCatch");
}
private static async Task ThrowAsync()
{
await Task.Delay(1000);
throw new InvalidOperationException("Delayed exception");
}
private static void Log(string step)
{
Console.WriteLine($"{step}: {Marshal.GetExceptionCode()}");
}
输出
TestAsync.Before: 0
HandleExceptionAsync.Try: 0
Exception thrown: 'System.InvalidOperationException' in Interactive.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.ni.dll
HandleExceptionAsync.Catch: -532462766
HandleExceptionAsync.AfterCatch: -532462766
TestAsync.After: -532462766
The thread 9292 has exited with code 0 (0x0).
该异常在整个等待链中保持活动状态,即使它已被捕获。
我检查了生成的代码,它没有提供为什么会发生这种情况的相关部分(为MoveNext
状态机生成HandleExceptionAsync
):
void IAsyncStateMachine.MoveNext()
{
int num1 = this.\u003C\u003E1__state;
try
{
if (num1 == 0)
;
try
{
TaskAwaiter awaiter;
int num2;
if (num1 != 0)
{
Program.Log("HandleExceptionAsync.Try");
awaiter = Program.ThrowAsync().GetAwaiter();
if (!awaiter.IsCompleted)
{
this.\u003C\u003E1__state = num2 = 0;
this.\u003C\u003Eu__1 = awaiter;
Program.\u003CHandleExceptionAsync\u003Ed__1 stateMachine = this;
this.\u003C\u003Et__builder.AwaitUnsafeOnCompleted<TaskAwaiter, Program.\u003CHandleExceptionAsync\u003Ed__1>(ref awaiter, ref stateMachine);
return;
}
}
else
{
awaiter = this.\u003C\u003Eu__1;
this.\u003C\u003Eu__1 = new TaskAwaiter();
this.\u003C\u003E1__state = num2 = -1;
}
awaiter.GetResult();
awaiter = new TaskAwaiter();
}
catch (InvalidOperationException ex)
{
Program.Log("HandleExceptionAsync.Catch");
}
Program.Log("HandleExceptionAsync.AfterCatch");
}
catch (Exception ex)
{
this.\u003C\u003E1__state = -2;
this.\u003C\u003Et__builder.SetException(ex);
return;
}
this.\u003C\u003E1__state = -2;
this.\u003C\u003Et__builder.SetResult();
}
我没有看到这与同步上下文有关(在这种情况下它是一个控制台应用程序,所以在池上安排了延续),我最好的猜测是有一些调用堆栈操作发生,但我找不到任何很好的信息。
如果有人能够解释为什么会这样,我会很感激并提供文档的链接,解释如何在CLR /编译器中实现它
UPD1 :添加了VS调试器的屏幕截图,显示异步中的活动异常,未显示任何内容同步
答案 0 :(得分:6)
如果你在Log("HandleExceptionAsync.AfterCatch");
放置一个断点,那么callstack会解释这个诀窍:
ConsoleApp1.exe!ConsoleApp1.Program.Log(string step) Line 107 C#
ConsoleApp1.exe!ConsoleApp1.Program.HandleExceptionAsync() Line 95 C#
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() Unknown
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask) Unknown
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations() Unknown
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree() Unknown
mscorlib.dll!System.Threading.Tasks.Task.FinishStageTwo() Unknown
mscorlib.dll!System.Threading.Tasks.Task.Finish(bool bUserDelegateExecuted) Unknown
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.VoidTaskResult>.TrySetException(object exceptionObject) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Threading.Tasks.VoidTaskResult>.SetException(System.Exception exception) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetException(System.Exception exception) Unknown
ConsoleApp1.exe!ConsoleApp1.Program.ThrowAsync() Line 101 C#
... (continues until the timer of Task.Delay)
看到底部框架?尽管我们正在HandleExceptionAsync
进行日志记录,但我们仍处于ThrowAsync状态。怎么可能?答案也在callstack中:
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.VoidTaskResult>.TrySetException(object exceptionObject) Unknown
简单来说,由于await
关键字,您的HandleExceptionAsync
方法会被切断,如下所示:
void HandleExceptionAsync1()
{
Log("HandleExceptionAsync.Try");
}
void HandleExceptionAsync2()
{
Log("HandleExceptionAsync.AfterCatch");
}
当然,这比这更复杂。事实上,这种方法并没有被切断,只是简单地转化为状态机。但是,对于这个演示,这是明智的等价。
HandleExceptionAsync2
需要在ThrowAsync
之后执行。因此,HandleExceptionAsync2
将被链接为延续。类似的东西:
ThrowAsync().ContinueWith(HandleExceptionAsync2);
(同样,这比那更复杂。我只是简化了解释)
&#34;问题&#34;是,当运行时完成ThrowAsync返回的任务时:
System.Threading.Tasks.Task<System.Threading.Tasks.VoidTaskResult>.TrySetException(object exceptionObject)
继续实际上将内联,并在同一个callstack中执行(参见上面的框架)。出于性能原因,这是TPL经常进行的优化。因此,在调用Log("HandleExceptionAsync.AfterCatch");
,时,您实际上仍然在ThrowAsync
的捕获区中,因此您看到了这种行为。