Finding out where unobservedTaskException is thrown: stacktrace is null

那年仲夏 提交于 2021-01-01 08:00:05

问题


I have a large C# appication using third party libraries and somewhere in the application a task is run which is not awaited and throws an exception.

So I added an event handler to TaskScheduler.UnobservedTaskException:

TaskScheduler.UnobservedTaskException += (sender, args) =>
{
    _logger.Error(args.Exception);
    args.SetObserved();
};

However, when this handler is executed, I get some exception with a stacktrace of null:

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. 
    ---> Grpc.Core.RpcException: Status(StatusCode=Cancelled, Detail="Cancelled")
    --- End of inner exception stack trace ---
---> (Inner Exception #0) Grpc.Core.RpcException: Status(StatusCode=Cancelled, Detail="Cancelled")<---

When I debug this code, the stacktrace of args.Exception is null and I don't know where it comes from. I tried to catch all exceptions of type System.AggregateException using visual studio 2017, but none was caught. I tried to catch all exceptions of type Grpc.Core.RpcException, but a lot of those are thrown and just handled in user code (in the order of 10 per second), while my uncaught exception happens only once every few hours so it is not feasible to skip manually through all the thousands of RpcExceptions thrown in the application.

How can I ever find out where this uncaught exception happens? I tried adding try/catch blocks in all tasks/async methods, but to no avail, though I might have missed some. I tried to remove all fire-and-forget tasks from my code, but I don't even know if this exception is from my code or from external code (in which case I could report to the code's developer).

unawaited async methods (async void) in my app occur in event handlers, and there I tried to put try/catch blocks to catch the exception. But still this issue persists.

What else could I do to figure out when and where this exception is thrown and where the unobserved task could be?


回答1:


To solve this sort of problem, you can use a memory profiler like dotMemory and collect allocations (https://www.jetbrains.com/help/dotmemory/dotMemory_Profiler_Options.html)!

When the unhandled exception happens, take a snapshot, find the task (can be done by its type and id) and have a look at the creation stack trace of that instance (https://www.jetbrains.com/help/dotmemory/Creation_Stack_Trace_instance.html).

Here's some code to get you started:

TaskScheduler.UnobservedTaskException += (sender, args) => 
    MessageBox.Show($"Almost there, now take a snapshot and look for the creation stacktrace of {sender.GetType()}, Id={((Task)sender).Id}!");

Hope this works for you, it did for me :)



来源:https://stackoverflow.com/questions/57769098/finding-out-where-unobservedtaskexception-is-thrown-stacktrace-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!