NullReferenceException in get_RequestAborted from SystemTextJsonOutputFormatter.WriteResponseBodyAsync
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
NullReferenceException insides WriteRespondeBodyAsync.
Expected Behavior
No exception
Steps To Reproduce
N/A it happens sporadically
Exceptions (if any)
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.Http.DefaultHttpContext.get_RequestAborted() at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location ---
.NET Version
8.0
Anything else?
No response
Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.
Has I said, this problem is quite sporadic. I'm not sure I'd be able to provide a minimal reproduction for this without more information.
Some more context would be useful though - like what code is trying to call this method in your application. Is it an MVC action, if so how is it defined? Are you trying to do some work in a background thread after the request has ended by accessing the HttpContext?
There's lots of things your application is doing that might cause this through incorrect usage, rather than it being a bug. Even if you can't make the bug happen, if you can provide some code showing what your application does, any incorrect usage could be spotted.
There's no much to go on in the stack trace by itself that's actionable to something specific.
Hey @martincostello the problem is that it's part of large codebase with dozens of endpoints, I'm not sure what exactly I can include. The stacktrace starts at the ResourceInvoker.
I'm more surprised there's a NRE and not a proper exception such as something like "trying to access httpcontext after it has been disposed".
It looks to me like the stack trace has been truncated, or if it isn't that it is maybe running on a background thread.
The thing that's missing here is that it isn't obvious why something is trying to access HttpContext.RequestAborted - there isn't any sign of any user code (your code) as to why something is trying to write JSON.
I would imagine that the reason there's a NRE happening is because of something to do with threading and there's a race-condition happening causing it, which is why there's not an specific exception handling around it (because it shouldn't happen in the first place).
+1 to @martincostello's comments here. I appreciate that it might be challenging to create a repro particularly for issues like this but additional details will be useful in debugging.
Are you hitting this in production? If so, do you have additional logs around the exception? Do you have any hints as to what type of requests might have resulted in this issue? How frequently does it occur?
Hey @captainsafia,
Yes, we're hitting in production. Sadly I don't have more logs and couldn't figure out the circumstances that caused the issue :/ Sorry for being so vague as I have little information about this issue. The occurrence is roughly a dozen times per week which is miniscule compared to our traffic, but still make me weary there's an underlying problem.