dotnet / dotnet/aspnetcore

Random NullReferenceException at several Microsoft.AspNetCore.Http.DefaultHttpRequest properties

Open
#56,951 0 comments 1 reaction 0 assignees View on GitHub
area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Periodically we get a NullReferenceException thrown from within `DefaultHttpRequest.get_Query`, `DefaultHttpRequest.set_RouteValues`, `DefaultHttpRequest.get_HasFormContentType`, even though we don't have any concurrent logic around reading these properties.
As recommended in issues linked to #42040 we tried to get a dump with full stack trace, but our app is hosted in Azure, so instead we used [Microsoft.Diagnostics.Runtime](https://devblogs.microsoft.com/dotnet/net-crash-dump-and-live-process-inspection) package to get stack trace for all threads in case we catch this exception in our logging middleware. (We read stack trace for all threads using approach similar [to this one](https://devblogs.microsoft.com/dotnet/net-crash-dump-and-live-process-inspection/#clrmd-features-and-functionality) ).
But unfortunately, we didn't get much use out of it and didn't see any indication of using HttpRequest's properties in parallel.

We have investigated full stack traces for all threads, investigated usage of HttpRequest's properties for each frame in the stack trace, tried to search similar issues in GitHub for packages persisting in the stack trace, but so far haven't found anything useful. Do you have any additional suggestions for fixing this problem other than dump, or any existing workarounds?

### Expected Behavior

No unexpected exceptions

### Steps To Reproduce

It is hard to reproduce as this happens under race conditions and it is unclear what the source of the problem is

### Exceptions (if any)

```
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Query()
```
```
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.set_RouteValues(RouteValueDictionary value)
```
```
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_HasFormContentType()
```

### .NET Version

6.0.424

### Anything else?

Attaching log files that may help:

- [1.1 Stack trace from main thread.log](https://github.com/user-attachments/files/16351376/1.1.Stack.trace.from.main.thread.log) contains the stack trace for the thread where the exception occured
- [1.2 Exception stack trace of all other threads.log](https://github.com/user-attachments/files/16351377/1.2.Exception.stack.trace.of.all.other.threads.log) contains the stack trace for all other managed threads for the same exception as in the previous point
- [2. Other stack traces from main thread.log](https://github.com/user-attachments/files/16351378/2.Other.stack.traces.from.main.thread.log) contains unique stack traces from main threads we encountered so far

Additionally, here are some of the packages we use and seen in stack traces for other threads:
- Microsoft.AspNetCore.OData 7.5.12
- Microsoft.ApplicationInsights 2.21.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.