Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async/await throws NullReferenceException how can we diagnose where we messed it up?

we have started using async/await in asp.net application, now we are getting the famous exception in our production

An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/376/ROOT

Process ID: 3796

Exception: System.NullReferenceException

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

StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state) at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.AwaitTaskContinuation.b__1(Object s) 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()

Is there any way we can get more information about the code/task that makes problem?

Second question: we tried to reproduce the exception locally in a simple test webform application

        protected void Page_Load(object sender, EventArgs e)
    {
        LogMessageToFile("before_task");
        var t = Test();

        tasks.Add(t);
    }
    async Task Test()
    {
        await Task.Run(() =>
       {
           LogMessageToFile("inside_task");
           Thread.Sleep(1000);
       }
            );
        this.Title = "test";
        LogMessageToFile("after_task");

        //  throw new Exception("");
    }

but we never get the exception in our test page seems that the code after await in Test function is never called and the tasks state are WaitingForActivation, why we do not get exception in this code?

like image 952
Mojtaba Avatar asked Jun 30 '16 09:06

Mojtaba


People also ask

What is a possible situation that leads to the NullReferenceException?

A NullReferenceException happens when you try to access a reference variable that isn't referencing any object. If a reference variable isn't referencing an object, then it'll be treated as null .

How do I resolve NullReferenceException?

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.


1 Answers

The legacy type (LegacyAspNetSynchronizationContext) in your call stack indicates that your web.config settings are incorrect. Set targetFramework to 4.5 or higher.

async/await cause undefined behavior on earlier versions of ASP.NET.

why we do not get exception in this code?

Because you probably updated the broken application to 4.5+ (which turned on "quirks mode", rendering await unusable), but created a new test application for 4.5+ (which turns off "quirks mode", allowing await to work).

like image 158
Stephen Cleary Avatar answered Oct 07 '22 01:10

Stephen Cleary