6

I have a .NET 4.0 web project and use Microsoft.Bcl.Async to support async/await feature. I've noticed that in production environment IIS worker process continiously crashes due to NullReferenceException which is thrown at System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1(). It seems that the exception is thrown some place else in my code and is rethrown at AsyncServices.ThrowAsync() method. Unfortunately any exception details (including original stack trace) are lost so I don't know where the exception has been thrown originally. Moreover, I've tried to catch this exception using AppDomain.CurrentDomain.UnhandledException and TaskScheduler.UnobservedTaskException events but these event are never fired.

There are all error details I have (from EventLog):

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace:    at System.Runtime.CompilerServices.AsyncServices.<ThrowAsync>b__1(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

There are no obvious places in my code that may throw this exception and process crashes without any output.

How do I debug this? Are there any ways to find the source of this exception?

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Albert
  • 63
  • 1
  • 4

1 Answers1

3

You cannot use Microsoft.Bcl.Async on IIS; it's entirely unsupported. Sorry.

To summarize from the referenced blog post: the behavior of async / await is undefined in ASP.NET unless the aspnet:UseTaskFriendlySynchronizationContext app setting is set to true or the httpRuntime.targetFramework attribute is set to 4.5.

And those options don't affect the ASP.NET 4 runtime.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • I've migrated my project to .NET4.5 and this seems to fix the issue. Thanks a lot! – Albert Oct 17 '13 at 05:56
  • @Stephen I am getting the same issue in a Windows Service Project. All other conditions are the same. Still. Is there any way to get better StackTrace infos without updating to .Net 4.5. – rudimenter Mar 12 '14 at 12:50
  • @rudimenter: You could try my [AsyncDiagnostics library](http://www.nuget.org/packages/Nito.AsyncEx.AsyncDiagnostics/). – Stephen Cleary Mar 12 '14 at 13:00
  • @StephenCleary Looks like your library is only for .NET 4.5. I am running 4.0. – rudimenter Mar 12 '14 at 13:13
  • @rudimenter: You are correct; sorry about that. AFAIK, there's no good way to get async stack traces on .NET 4.0. – Stephen Cleary Mar 12 '14 at 13:51