runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Error: Unable to read data from the transport connection: Connection reset by peer when using Httpclient in Dotnet core api

Open VidhyaSanjeevi opened this issue 3 years ago • 15 comments

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.

VidhyaSanjeevi avatar May 30 '23 12:05 VidhyaSanjeevi

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:

area-System.Net.Http

Milestone: -

ghost avatar May 30 '23 12:05 ghost

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.

wfurt avatar May 31 '23 07:05 wfurt

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar May 31 '23 09:05 ghost

I have packet data now, how can i share it to your team?

VidhyaSanjeevi avatar Jun 01 '23 09:06 VidhyaSanjeevi

We have created a ticket with Azure Networking team with packet data shared with them, sharing the ticket id here for reference: 2305310060005914

VidhyaSanjeevi avatar Jun 01 '23 15:06 VidhyaSanjeevi

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.

wfurt avatar Jun 02 '23 07:06 wfurt

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar Jun 05 '23 15:06 ghost

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]

VidhyaSanjeevi avatar Jun 08 '23 13:06 VidhyaSanjeevi

@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 avatar Jun 08 '23 15:06 karelz

@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:

image

VidhyaSanjeevi avatar Jun 09 '23 13:06 VidhyaSanjeevi

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.

wfurt avatar Jun 09 '23 13:06 wfurt

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar Jun 12 '23 13:06 ghost

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.

ManickaP avatar Jun 12 '23 13:06 ManickaP

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.

ghost avatar Jul 03 '23 15:07 ghost

Not actionable, closing. Feel free to reopen if there is more actionable data available. Thanks!

karelz avatar Jul 03 '23 18:07 karelz