问题
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