aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

NullReferenceException in IISHttpContext

Open martincostello opened this issue 7 years ago • 38 comments

Describe the bug

Since updating a website running in Azure App Service to use ASP.NET Core 2.2.0 with in-process hosting, I've observed a small number of NullReferenceExceptions being thrown via Application Insights.

System.NullReferenceException:
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.GetOriginalPath (Microsoft.AspNetCore.Server.IIS, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.InitializeContext (Microsoft.AspNetCore.Server.IIS, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1+<ProcessRequestAsync>d__2.MoveNext (Microsoft.AspNetCore.Server.IIS, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer+<HandleRequest>d__27.MoveNext (Microsoft.AspNetCore.Server.IIS, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)

To Reproduce

Application experiencing the issue was built from this commit: martincostello/website@ee2ca4ba02823b97062924b7dc8f313e6278a3f8

Expected behavior

HTTP request is processed successfully.

Screenshots

image

Additional context

Add any other context about the problem here. Include the output of dotnet --info

martincostello avatar Dec 21 '18 08:12 martincostello

Looking at the code in master, it would seem that GetRawUrlInBytes() saw NativeRequest->pRawUrl to be null or empty, and returned null, which then isn't handled by GetOriginalPath().

https://github.com/aspnet/AspNetCore/blob/85e2147ff037cc9950249fbb9d095ad47ec4184a/src/Shared/HttpSys/RequestProcessing/NativeRequestContext.cs#L141-L151

https://github.com/aspnet/AspNetCore/blob/3c09d644cccdb21801f7a79e1188a1a1212de5d9/src/Servers/IIS/src/Microsoft.AspNetCore.Server.IIS/Core/IISHttpContext.cs#L185-L186

martincostello avatar Dec 21 '18 09:12 martincostello

Do you know which request URL causes the issue?

pakrym avatar Dec 21 '18 16:12 pakrym

I’ll dig further through AI later today for you to find out.

martincostello avatar Dec 21 '18 16:12 martincostello

AI doesn't appear to have captured any other telemetry events for the specific exceptions (I guess it's happening too early in the request flow for managed code for any route matching to have run), but from looking at request logs, the best I can tell is that it was a request to the root of the website.

martincostello avatar Dec 21 '18 18:12 martincostello

Same issue over here. Any workaround available ? Are we sure this is related to the In/OutOfProcess option ?

bartdk-be avatar Dec 27 '18 10:12 bartdk-be

For me this only started once I switched to in-process hosting after the roll out to Azure App Service of the hosting bundle was completed.

martincostello avatar Dec 27 '18 10:12 martincostello

Do you have timestamps of these exceptions? Feels a bit like AppServices warmup request.

pakrym avatar Dec 27 '18 16:12 pakrym

Four timestamps for these exceptions are:

  • 20/12/2018, 18:24:13
  • 21/12/2018, 00:37:07
  • 21/12/2018, 13:45:57
  • 25/12/2018, 15:38:01

martincostello avatar Dec 27 '18 17:12 martincostello

@martincostello do you have Always On enabled?

pakrym avatar Dec 27 '18 20:12 pakrym

In case you meant me, yes I do.

martincostello avatar Dec 27 '18 20:12 martincostello

The frequency of requests there looks like it is coming from the app warmup request after IIS restarts from Always On. To confirm, can you turn off app warmup for a period and see if exceptions are still being thrown?

If that’s the case, the null ref is coming from the warmup request. This behavior is “fine” as the request was fake and it is supposed to startup Program.Main. However, we should more gracefully handle this type of request and not throw.

Get Outlook for iOShttps://aka.ms/o0ukef


From: Martin Costello [email protected] Sent: Thursday, December 27, 2018 12:27 PM To: aspnet/AspNetCore Cc: Subscribed Subject: Re: [aspnet/AspNetCore] NullReferenceException in IISHttpContext (#6075)

In case you meant me, yes I do.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faspnet%2FAspNetCore%2Fissues%2F6075%23issuecomment-450225392&data=02%7C01%7CJustin.Kotalik%40microsoft.com%7Ce47a17cf79c14ed8965508d66c39c6a7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636815392732641734&sdata=rXZLEMMAusc4ZBH47OXhlM1sQaNAzPMZuUSHa4a7H2w%3D&reserved=0, or mute the threadhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAH6uFVu8-uWuEVZkvZG2WobO1z3nwajcks5u9S1HgaJpZM4ZdnOe&data=02%7C01%7CJustin.Kotalik%40microsoft.com%7Ce47a17cf79c14ed8965508d66c39c6a7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636815392732651747&sdata=c%2BBvm7xm2865OIYE3l20Cp3c14zwpX9OMzwm%2BENkVWU%3D&reserved=0.

jkotalik avatar Dec 27 '18 21:12 jkotalik

I've disabled Always On and will leave it for a few days to see if it happens again, but it only happened a handful of times in the last 7 days as it is.

martincostello avatar Dec 28 '18 13:12 martincostello

@jkotalik or we need to find the module that makes this request and make sure it sets pRawUrl.

@martincostello thank you for your help in figuring this out.

pakrym avatar Dec 28 '18 16:12 pakrym

I have also the similar problem, with get_RemoteIpAddress

An unhandled exception has occurred while executing the request. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.InitializeRemoteEndpoint()
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.Microsoft.AspNetCore.Http.Features.IHttpConnectionFeature.get_RemoteIpAddress()
   at Microsoft.AspNetCore.Http.Internal.DefaultConnectionInfo.get_RemoteIpAddress()
   at Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware.ApplyForwarders(HttpContext context)
   at Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

Besides, my app was deployed at Azure app service with autoscale mode.

alyiox avatar Jan 08 '19 02:01 alyiox

@qqbuby do you only see a single request failing per application startup?

jkotalik avatar Jan 08 '19 16:01 jkotalik

@jkotalik I guess the error is coming from the app warmup request cause it doesn't happen frequently and I only see a few of error messages per day.

alyiox avatar Jan 09 '19 01:01 alyiox

I talked to AppServices folks and this is not how "Always On" works. It's implemented using load balancer sending requests to the app.

pakrym avatar Jan 24 '19 17:01 pakrym

I am seeing this error also localhost. Is it possibly related to the IIS Application Pool setting AlwaysRunning?

bgiftge avatar Jan 25 '19 16:01 bgiftge

@bgiftge possibly, which variation of the error do you see? InitializeRemoteEndpoint or GetOriginalPath ?

pakrym avatar Jan 25 '19 16:01 pakrym

hmm.. maybe I spoke too early. I was convinced that I saw the GetOriginalPath error a couple of times, but I am not finding it in my logs.

bgiftge avatar Jan 25 '19 16:01 bgiftge