Error: Unable to read data from the transport connection: Connection reset by peer when using Httpclient in Dotnet core api
Description
Unable to read data from the transport connection: Connection reset by peer when using Httpclient in Dotnet core api
Error stacktrace:
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithNtAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean isProxyAuth, HttpConnection connection, HttpConnectionPool connectionPool, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
Reproduction Steps
Running Dotnet core api in Docker hosted in AKS with HPA scaling, it's an intermittent issue happens for 1-10/1000 Http calls to external api endpoints.
Followed Httpclient best practices.
Code Used:
DefaultHttpClientFactory httpClientFactory = new(null, new SocketsHttpHandler
{
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
MaxConnectionsPerServer = 10
});
client = httpClientFactory.CreateClient("test");
await client.SendAsync(request, cancellationToken.Token).ConfigureAwait(false);
Expected behavior
Successful http request with 200/error code
Actual behavior
Getting Error: Unable to read data from the transport connection: Connection reset by peer
Regression?
This was working in .Net Framework 4.7, from which we migrated to dotnet core 6 and deployed in AKS .
Known Workarounds
When we redeploy the service this error is reduced to some level for 8 hours and again it start to give the error intermittently
Configuration
.NET 6 Linux Ubuntu Kubernetes version: 1.25.0 Networking Azure CNI Nat gateway
Other information
No SNAT failures in NAT gateway and no Packet drops/Network errors in Grafana/Prometheus dashboard for the cluster level the endpoint accessed - is responding within milliseconds, but we get this error.
Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.
Issue Details
Description
Unable to read data from the transport connection: Connection reset by peer when using Httpclient in Dotnet core api
Error stacktrace:
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithNtAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean isProxyAuth, HttpConnection connection, HttpConnectionPool connectionPool, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, Boolean async, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
Reproduction Steps
Running Dotnet core api in Docker hosted in AKS with HPA scaling, it's an intermittent issue happens for 1-10/1000 Http calls to external api endpoints.
Followed Httpclient best practices.
Code Used:
DefaultHttpClientFactory httpClientFactory = new(null, new SocketsHttpHandler
{
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
MaxConnectionsPerServer = 10
});
client = httpClientFactory.CreateClient("test");
await client.SendAsync(request, cancellationToken.Token).ConfigureAwait(false);
Expected behavior
Successful http request with 200/error code
Actual behavior
Getting Error: Unable to read data from the transport connection: Connection reset by peer
Regression?
This was working in .Net Framework 4.7, from which we migrated to dotnet core 6 and deployed in AKS .
Known Workarounds
When we redeploy the service this error is reduced to some level for 8 hours and again it start to give the error intermittently
Configuration
.NET 6 Linux Ubuntu
Other information
No response
| Author: | SSanjeevi |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
You should get packet captures @SSanjeevi. This is most likely coming from the server and if server resets the connection there is really nothing the client can do so it will surface the error. I would suggest to look at the flow and times when connection is idle. Often the connection is cut out by some intermediate gear - like load balancer or firewall.
This issue has been marked needs-author-action and may be missing some important information.
I have packet data now, how can i share it to your team?
We have created a ticket with Azure Networking team with packet data shared with them, sharing the ticket id here for reference: 2305310060005914
We don't have access to that. You can put it somewhere in cloud and drop me message to my GH email. More importantly, you can also look at the differences. I would home that something would be obvious.
This issue has been marked needs-author-action and may be missing some important information.
Can you please share your MSFT email to my email id? For security concerns we can't share to personal email. my email id : [email protected]
@SSanjeevi just to clarify, that GH is not official support channel. We are more than happy to help here, but it is without SLAs and not an official help.
I have packet data now, how can i share it to your team?
In general, we do not scale to look at random traces. If you find interesting parts in the traces and compare them between .NET 4.7 and .NET 6.0 / 7.0, that would be reasonable for us to look at.
My best guess is that something on the network indeed sends connection reset. .NET Framework might be doing some automatic retries in such case (e.g. during connection establishing), while .NET (Core) does not have such automatic retries and relies on user-library retries (e.g. Polly). Do you have user-level retry logic like Polly? If not, can you try and see if it would help?
Or alternatively, check the logs and confirm if connection reset is indeed sent over the wire. If yes, then it is the likely explanation for the behavior. I'd be interested in details of the reset.
@karelz thanks for the info, we are seeing this after migrating to Kubernetes in azure - AKS.
Am sharing the screenshot during the timeout - we are seeing these requests with message TCP retransmissions:
this is too late. You should look what happens before just before this block. Sometimes some firewall or load balancer cuts communication and then that leads to stalling TCP.
This issue has been marked needs-author-action and may be missing some important information.
Triage: this is not actionable for us as it is now. There are too many moving parts that might be the cause for this: environment, external server, load-balancers, proxies, firewall, ... We might try to help troubleshoot or give guidance how to diagnose this, but otherwise there's nothing that would suggest there's bug in our HTTP stack atm.
This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.
Not actionable, closing. Feel free to reopen if there is more actionable data available. Thanks!