1
votes

Some of the users of my application experience strange crashes from different exceptions in System.Runtime.CompilerServices.AsyncServices

There is none of my code in the stack trace and I don't have a clue where to search for a cause or how to add additional logging to find the reason of the crash. Googling did not help.

Examples of exceptions are:

String reference not set to an instance of a String. Parameter name: s

Void System.Runtime.CompilerServices.AsyncServices.b__0(System.Object) 
System.Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
System.Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
Void System.Windows.Threading.DispatcherOperation.InvokeImpl() 
Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) 
Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
Void System.Windows.Threading.DispatcherOperation.Invoke() 
Void System.Windows.Threading.Dispatcher.ProcessQueue() 
IntPtr System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
IntPtr MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
System.Object MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 
System.Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
System.Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
System.Object System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 
IntPtr MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 
IntPtr MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 
Void System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 
Void System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 
Void System.Windows.Threading.Dispatcher.Run() 
System.Object System.Windows.Application.RunDispatcher(System.Object) 
Int32 System.Windows.Application.RunInternal(System.Windows.Window) 
Int32 System.Windows.Application.Run(System.Windows.Window) 
Int32 System.Windows.Application.Run() 
App.g.cs - Void MyProject.App.Main()
Object reference not set to an instance of an object.

Void System.Runtime.CompilerServices.AsyncServices.b__0(System.Object) 
System.Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
System.Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
Void System.Windows.Threading.DispatcherOperation.InvokeImpl() 
Void System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) 
Void System.Threading.ExecutionContext.runTryCode(System.Object) 
Void System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object) 
Void System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 
Void System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 
System.Object System.Windows.Threading.DispatcherOperation.Invoke() 
Void System.Windows.Threading.Dispatcher.ProcessQueue() 
IntPtr System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
IntPtr MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 
System.Object MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 
System.Object System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 
System.Object MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 
System.Object System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 
IntPtr MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 
IntPtr MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 
Void System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 
Void System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 
System.Object System.Windows.Application.RunDispatcher(System.Object) 
Int32 System.Windows.Application.RunInternal(System.Windows.Window) 
Int32 System.Windows.Application.Run(System.Windows.Window) 
Int32 System.Windows.Application.Run() 
App.g.cs - Void MyProject.App.Main() 

Additional information: I am targeting NET 4.0 and using Microsoft.Bcl.Async. First of the exceptions described above happened on a machine with Win 7 / NET 4.0

Update: I'll try to clarify the question.

It seems that these stack traces can appear if some exception happens in async method. Is there a way to find the place where original exception happened? Because it is very hard to debug such issues without knowing anything but the exception message.

1
Do you do anything that could involve an async callback of some kind? any TPL / Task? any BeginInvoke? any ThreadPool? You mention Bcl.Async - have you checked whether you are catching all worker exceptions?Marc Gravell♦
I am using async/await calls, Tasks and TPL. Yes I am catching worker exceptions but there is lot of code and I think I could miss something. How can I find out where to look for the reason of this issue? This stack trace does not give much :(mephisto123
These stack traces don't look right. For one, the method that triggers the crash should be called AsyncServices.<ThrowAsync>b__0(). Do make sure that your logging code isn't borken.Hans Passant
Logging code is not broken for sure. It was there for ages and always worked correctly. I actually refactored a large application to start using async/await instead of callbacks.mephisto123

1 Answers

4
votes

Since it seems that noone knows a better solution, I'll post the one I used.

I found out that the original exception that caused one of these crashes is always triggering FirstChanceException event first.

So to know what is the cause of the exception I did the following:

  • Logging all FirstChanceException events and the details of corresponding exceptions.
  • Including few last logged FirstChanceExceptions data in the crash report.