-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Open
Labels
area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Milestone
Description
We've had issues over the the years about random null refs that occur when accessing the HttpContext:
- ASP.NET Core 3.0: NullReferenceException in HttpContext.RequestAborted #17085
- DefaultHttpResponse throws null ref when trying to access Cookies, even when using null check, only from bots #25454
- System.NullReferenceException on Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query #3219
- NullReferenceException at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Items() #2799
- NullReferenceException at Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Query() #2806
- At random intervals the application fails to set RouteValues #41924
- System.NullReferenceException while initializing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.WebSessionTelemetryInitializer microsoft/ApplicationInsights-dotnet#1524
- Intermittent
NullReferenceException
fromDefaultHttpRequest.Form
#42100 - System.NullReferenceException at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Cookies() #43155
- System.NullReferenceException: at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_HasFormContentType #43339
- Object reference error from DefaultHttpRequest.set_RouteValues #47573
There are examples of concurrent access to the HttpContext. This usually happens because the internal feature references cache can be cleared after a new feature has been set. This happens on reading some properties (because it's lazily evaluated).
Instead, we should handle this case by throwing an exception saying that there was concurrent modification.
Here's a simple example that illustrates why there's a null ref:
var references = new Reference<ReferenceHolder>();
var myfeature = references.Fetch(ref references.Cache.NameFeature, () => new MyNameRequestFeature { Name = "New Feature" });
// This blows up with a null ref
var name = myfeature.Name;
Console.WriteLine(name);
struct Reference<T>
{
public T? Cache;
public R Fetch<R>(ref R? item, Func<R> factory)
{
item = factory();
// Clear the cache
Cache = default;
return item;
}
}
struct ReferenceHolder
{
public MyNameRequestFeature? NameFeature;
}
class MyNameRequestFeature
{
public string Name { get; init; } = default!;
}
jeffputz, twogood, masquinyo, GraemeSMiller, kjeske and 7 more
Metadata
Metadata
Assignees
Labels
area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions