-
Notifications
You must be signed in to change notification settings - Fork 5k
Transient NullReferenceException when calling HttpClient.GetAsync #115179
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
Comments
My first guess would be that the header collection got corrupted due to concurrent use. Since it sounds like this is (relatively) common, would you be able to capture a memory dump at the time you hit the exception? E.g. FailFast from the first chance exception handler. |
@MihaZupan thanks for the reply! I just took a look through our code and I think you are correct that we initialize the HttpClient in the constructor of our StatelessService object in Program.cs. Then we have multiple lower-level threads that are adjusting the HttpClient Auth Header at the same time. So, the recommended fix here would be to initiate the HttpClients in the RunAsync method instead, so that it happens at thread initialization? |
The recommendation would be to not have this as a default header and instead have each call site create an HttpRequestMessage with whatever headers that request needs, isolated from every other request. The shared HttpClient should be treated as being immutable. You could create a new HttpClient at each use, but you'd want to ensure that you were sharing the same underlying HttpMessageHandler and not doing |
Got it and makes sense. Will look to define the auth headers in the request instead. Thank you very much! |
My team has been using HttpClient.GetAsync on .NET 462 without issues, but upon migrating to .NET 8, we have been getting transient null references within the GetAsync call as shown in the stack trace below. The flow that calls HttpClient.GetAsync is fairly high volume and we see that maybe 1% of our calls have the NullReferenceException whereas the other 99% are successful.
Furthermore, upon being requeued, the previously failed calls are successful, indicating that it is not an issue with specific edge cases. I was wondering if this is a known issue, as other code paths we have that use HttpClient see these NullReferenceExceptions.
Our code is fairly simple and just attaches the Auth header and then makes the GetAsync call like below:
The stack trace in the exception is:
The text was updated successfully, but these errors were encountered: