Repository navigation
Description
Assumption: Duplicate of #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 #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#
Activity
[-]"System.Net.Sockets.SocketException: Address already" in use on Linux[/-][+]"System.Net.Sockets.SocketException: Address already in use" on Linux[/+]leecow commentedon Jan 25, 2019
@karelz , @davidsh - can you have a look?
karelz commentedon Jan 25, 2019
karelz commentedon Jan 25, 2019
Can you please try .NET Core 3.0? It was fixed there ...
yuezengms commentedon Jan 26, 2019
I'll try upgrading. Thanks!
yanrez commentedon Jan 26, 2019
@karelz do you plan to backport this fix to 2.2 ?
karelz commentedon Jan 26, 2019
@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.
yanrez commentedon Jan 26, 2019
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.
karelz commentedon Jan 26, 2019
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 commentedon Jan 26, 2019
@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.
yanrez commentedon Jan 26, 2019
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.
arsenhovhannisyan7713 commentedon Feb 4, 2019
I have same exception on AKS (ver. 1.11.4) , and container microsoft/dotnet:2.2-aspnetcore-runtime
, Region West Europe .
antoinne85 commentedon Feb 7, 2019
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
yanrez commentedon Feb 7, 2019
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
antoinne85 commentedon Feb 7, 2019
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
HttpClientinternally. The class was getting instantiated for each incoming request by the DI container (resulting in a newHttpClientfor 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.123 remaining items
stephentoub commentedon Sep 5, 2019
Yes, the
clientin theGetPagemethod in that sample should be disposed. Thanks for pointing that out.cc: @glennc, @rynowak
MrZoidberg commentedon Oct 1, 2019
@karelz I experience this problem with .net core 2.2. Is it possible to get private libs to test the backport?
karelz commentedon Oct 1, 2019
@wfurt can you please create a private build against 2.2 for @MrZoidberg to test?
rockstardev commentedon Oct 22, 2019
UPDATE: Disregard this comment... the
System.Net.Sockets.SocketException: Address already in usebug kept happening in both cases until. I've only solved it by moving my class fromservices.AddScopedintoservices.AddSingletonPREVIOUS COMMENT (again, disregard):
I'm getting this error a lot now in situation where I've switched from:
to:
I'm disposing of
httpResponseafter deserializing response in both cases. Any opinion on the code @karelz @stephentoub? I'm on .NET Core 2.2 and would prefer not upgrading to .NET Core 3.0, unless it would help you further debug and solve the problem.antonioortizpola commentedon Oct 22, 2019
The problem with the socket comes from the
HttpClient, notHttpRequestMessage, how are you generating the HttpClient? are you using DI, HttpClientFactory?adelhelal commentedon Oct 23, 2019
Have you registered your
HttpClientat bootstrap ? i.e. something like this:Then from your
MyServiceinject it in:Implementation should be as simple as:
Since you're using the
GETmethod theGetAsync(url)function should be sufficient.This lets .NET Core handle all your connection pooling for you so you don't have to worry about managing it yourself.
rockstardev commentedon Oct 25, 2019
I'm creating
HttpClientby myself in both cases. However if I use itHttpRequestMessageandSendAsync- problems withSystem.Net.Sockets.SocketException: Address already in usestart.If I just use GetAsync - no problems.
@karelz @stephentoub can you please review this and provide any feedback? I'm hoping if you investigate in this direction that you can potentially solve the problem for others as well.
stephentoub commentedon Oct 25, 2019
Can you share the code you use with SendAsync? Are you calling it with ResponseHeadersRead?
OpenSourceAries commentedon Oct 25, 2019
GetAsyncis just a wrapper aroundSendAsync.https://github.com/dotnet/corefx/blob/092ede257ff5521c6bae20b27f1334536b1b985c/src/System.Net.Http/src/System/Net/Http/HttpClient.cs#L300-L304
draco111 commentedon Dec 25, 2019
i got same error in production too(.netcore 2.2 ubuntu k8s)
we follow the sample code with aspnet suggest doc and here is the demo code:
in Startup.cs
here is the AddressClient.cs
here is the use of httpclient:
i think we do as the doc suggest to
rockstardev commentedon Jan 2, 2020
Thanks for your help @stephentoub and @OpenSourceAries ... in the end I solved my problem in different fashion since I was still getting errors... updated original post: https://github.com/dotnet/corefx/issues/37044#issuecomment-545038295
chrisdrobison commentedon Apr 1, 2020
I'm also seeing this exception with MongoDB driver in K8s, which isn't using the HttpClient. So is this fix this a thing or is the scope larger than originally anticipated?