runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Address "System.Net.Sockets.SocketException: Address already in use" on K8S/Linux using HttpClient/TCP

Open yuezengms opened this issue 7 years ago • 138 comments

~Assumption: Duplicate of dotnet/runtime#27274 which was fixed by dotnet/corefx#32046 - goal: Port it (once confirmed it is truly duplicate).~ This is HttpClient/TCP spin off. UdpClient is covered fully by dotnet/runtime#27274.

Issue Title

"System.Net.Sockets.SocketException: Address already in use" on Linux

General

Our .net core(v 2.2.0) services are running on Azure Kubernettes Linux environment. Recently we experimenced a lot of error "System.Net.Http.HttpRequestException: Address already in use" while calling dependencies, e.g. Active Directory, CosmosDB and other services. Once the issue started, we kept getting the same errors and had to restart the service to get rid of it. Our http clients are using DNS address, not specific ip and port. The following is the call stack on one example. What can cause such issues and how to fix it?

System.Net.Http.HttpRequestException: Address already in use ---> 
System.Net.Sockets.SocketException: Address already in use#N#   
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)#N#   --- 
End of inner exception stack trace ---#N#   

at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)#N#   
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)#N#   
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)#N#   
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)#N#   
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)#N#   
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)#N#   
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Http.HttpClientWrapper.GetResponseAsync()#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Http.AdalHttpClient.GetResponseAsync[T](Boolean respondToDeviceAuthChallenge)#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Http.AdalHttpClient.GetResponseAsync[T]()#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.SendHttpMessageAsync(IRequestParameters requestParameters)#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.SendTokenRequestAsync()#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.CheckAndAcquireTokenUsingBrokerAsync()#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Flows.AcquireTokenHandlerBase.RunAsync()#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireTokenForClientCommonAsync(String resource, ClientKey clientKey)#N#   
at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireTokenAsync(String resource, ClientCredential clientCredential)#N#   

yuezengms avatar Jan 25 '19 20:01 yuezengms

@karelz , @davidsh - can you have a look?

leecow avatar Jan 25 '19 23:01 leecow

Duplicate of https://github.com/dotnet/corefx/issues/32027

karelz avatar Jan 25 '19 23:01 karelz

Can you please try .NET Core 3.0? It was fixed there ...

karelz avatar Jan 25 '19 23:01 karelz

I'll try upgrading. Thanks!

yuezengms avatar Jan 26 '19 00:01 yuezengms

@karelz do you plan to backport this fix to 2.2 ?

yanrez avatar Jan 26 '19 00:01 yanrez

@yanrez not unless there is some good business justification - higher number of affected customers. Is that the case? (this is 2nd ask "only" so far) Also, it would help to validate this is truly the root cause, e.g. by trying 3.0 preview/daily build.

karelz avatar Jan 26 '19 00:01 karelz

It is happening in some of the clusters. It doesn't seem to consistently repro, but some pods go into this state and stay in it until being terminated. I understand 3.0 is still few months away (I don't know actual timeline though), so my question about 2.2 was based on assumption that hotfix for 2.2 could come earlier than 3.0 release. We will look into trying to upgrade and see if it solves the issue.

yanrez avatar Jan 26 '19 00:01 yanrez

BTW: It might be good for you to register as MS employees - at least by linking your accounts: https://github.com/dotnet/core/blob/master/Documentation/microsoft-team.md ... that allows other FTEs to see you are MSFT ;)

karelz avatar Jan 26 '19 00:01 karelz

@yanrez what is the service? How large is it roughly? How often it happens? ... That may support the business justification (even if you were not MSFT ;)). If you can verify on 3.0 that would be great. Either way, we may need to verify on early 2.1/2.2 build as part of "test signoff" to make sure we are fixing the real root cause here.

karelz avatar Jan 26 '19 00:01 karelz

I will follow up offline, but in some of the regions we see it happening more often - taking down several pods in our k8s cluster per day. It's very annoying at the moment, causing us few dev-hours a day to act on it and mitigate. We are also looking into automated mitigation using liveness probe wired into check if we start getting these exceptions and signalling k8s to kill the pod. Unfortunately, it's also non-trivial amount of dev work to build and deploy. Considering we can't exactly predict frequency of the issue, risk is that liveness pod might still impact our availability and cause us missing SLA.

yanrez avatar Jan 26 '19 00:01 yanrez

I have same exception on AKS (ver. 1.11.4) , and container microsoft/dotnet:2.2-aspnetcore-runtime , Region West Europe .

arsenhovhannisyan7713 avatar Feb 04 '19 09:02 arsenhovhannisyan7713

Just chiming in to say that my business is also experiencing this issue: AKS (v1.9.6) Region: Central US Image: microsoft/dotnet:2.2-aspnetcore-runtime

antoinne85 avatar Feb 07 '19 02:02 antoinne85

We applied automated mitigation to count number of these exceptions and report negative signal to k8s liveness check. It helped us mitigate the impact. We didn't verify yet if latest builds of net core 3 would resolve the issue

yanrez avatar Feb 07 '19 03:02 yanrez

FWIW, we implemented the same liveness check but then subsequently manage to fix the issue altogether in our deployment.

For us, we had a service client that was using HttpClient internally. The class was getting instantiated for each incoming request by the DI container (resulting in a new HttpClient for each incoming request). We changed the way the client was registered such that it is only instantiated once for the entire application and the issue was resolved.

antoinne85 avatar Feb 07 '19 15:02 antoinne85

I experimenced this problem since yesterday,everything using socket throw "System.Net.Sockets.SocketException: Address already in use". Like Mysql connection, redis, httpclient.

eventhorizon-cli avatar Feb 28 '19 02:02 eventhorizon-cli

For us, we had a service client that was using HttpClient internally. The class was getting instantiated for each incoming request by the DI container (resulting in a new HttpClient for each incoming request). We changed the way the client was registered such that it is only instantiated once for the entire application and the issue was resolved.

@antoinne85 this is a common mistake, people do regarding HttpClient and one of the reasons why IHttpClientFactory was added in DotNet Core 2.1. (another being singleton HttpClients not respecting DNS changes by default)

See https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests

The original and well-known HttpClient class can be easily used, but in some cases, it isn't being properly used by many developers.

As a first issue, while this class is disposable, using it with the using statement is not the best choice because even when you dispose HttpClient object, the underlying socket is not immediately released and can cause a serious issue named ‘sockets exhaustion’. For more information about this issue, see You're using HttpClient wrong and it's destabilizing your software blog post.

kirides avatar Feb 28 '19 08:02 kirides

Same problem with NEST elasticsearch client on Linux under Core 2.2. Backporting fix to 2.2 would be nice

EvilBeaver avatar Mar 06 '19 07:03 EvilBeaver

Same here @Kirides, @karelz, we are hitting this issue also in a site with a lot of traffic requests (10-15 per second, maybe more), It worked fine in 2.1, the issue has been happening since we update to 2.2, it is happening with our HttpClients, but we are already using IHttpClientFactory.

We have to restart our docker containers to fix the problem, and it is happening at least once a day. I am also afraid to update to 3, since it is a productive site, and the official release is not ready yet.

antonioortizpola avatar Mar 25 '19 18:03 antonioortizpola

We have to restart our docker containers to fix the problem

I'm doing the same. I set docker container mode "restart=always" and in my app. Then I'm catching this SocketException. If it's caught - i'm killing the app and docker engine restarts it. Works fine, but for complex apps it should be fixed properly at dotnet level.

EvilBeaver avatar Mar 26 '19 14:03 EvilBeaver

@karelz I have an app with a large number of users affected by this. Any Dlna media app will be affected by this. A back-port would be much appreciated. Thanks.

LukePulverenti avatar Apr 05 '19 02:04 LukePulverenti