Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException in get_RequestAborted from SystemTextJsonOutputFormatter.WriteResponseBodyAsync #55484

Open
1 task done
whoaskedfrfr opened this issue May 2, 2024 · 6 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Milestone

Comments

@whoaskedfrfr
Copy link

Is there an existing issue for this?

  • 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.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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label May 2, 2024
@martincostello martincostello added the Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue label May 2, 2024
@dotnet-policy-service dotnet-policy-service bot added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label May 2, 2024
@whoaskedfrfr
Copy link
Author

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.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels May 2, 2024
@martincostello
Copy link
Member

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.

@whoaskedfrfr
Copy link
Author

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".

@martincostello
Copy link
Member

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).

@captainsafia
Copy link
Member

+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?

@captainsafia captainsafia removed the Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. label May 6, 2024
@captainsafia captainsafia added this to the Backlog milestone May 6, 2024
@whoaskedfrfr
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Projects
None yet
Development

No branches or pull requests

3 participants