0

I have a C# webapi project that is communicating as a signalr client with another application. Periodically it throws this exception which causes it to crash. I can reproduce the exception by running some load tests on it, but despite searching high and low I can't pinpoint where this exception is occurring and prevent it.

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. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter()
at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action)
at System.Web.Util.SynchronizationHelper.<>c__DisplayClass22_0.<QueueAsynchronous>b__0(Task _)
at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.HttpApplication.System.Web.Util.ISyncContext.Enter()
at System.Web.Util.SynchronizationHelper.SafeWrapCallback(Action action)
at System.Web.Util.SynchronizationHelper.<>c__DisplayClass22_0.<QueueAsynchronous>b__0(Task _)
at System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()
at System.Threading.Tasks.Task.Execute()<---

My application is .net 4.7 and the signalr hub is running .net 4.7. I'm pretty sure the server is not involved. If I restart the client, it picks up where it left off and continues working.

There are a number of async methods in the webapi client, and I've confirmed that all the ones called internally are awaited and they all have exception handlers in them. The webapi endpoints are also marked async.

I've spent a few days trying to get to the bottom of this exception. Any tips would be gratefully accepted! Happy to elaborate with more info as needed.

dylanT
  • 1,080
  • 1
  • 13
  • 26
  • 1
    According to your inner exception it seems you code is not correctly creating an instance somewhere. Would put various breakpoints and try to see where its breaking. If you have some try catch try commenting it out for testing and see what happens. – AliK Jun 18 '19 at 01:38
  • I actually put a breakpoint into every catch block in my application, and didn't hit any of them before or when this issue occurred. – dylanT Jun 18 '19 at 10:27

1 Answers1

0

Seems like you have ThrowUnobservedTaskExceptions set to true. The recommended setting (and the default for .NET 4.7) is false.

From the stack traces, I would guess that ASP.NET (pre-Core) is depending on unobserved task exceptions being ignored. While this is not ideal, it's also not terribly surprising.

I recommend removing the config that enables ThrowUnobservedTaskExceptions.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • This suggestion does not address the root cause. – PepitoSh Jun 18 '19 at 01:48
  • I've looked into this. Thanks for the suggestion by the way! I can't see this setting anywhere in my application, so I assume it is using the default behavior and still crashing anyway. I will try setting it to false and see if the issue goes away. – dylanT Jun 18 '19 at 10:27
  • @dylanT: It's not the default behavior [unless you're running in quirks mode](https://devblogs.microsoft.com/aspnet/all-about-httpruntime-targetframework/). – Stephen Cleary Jun 18 '19 at 16:46