SocketException "Unknown error (0xffffffff)" at System.Net.Http.ConnectHelper.ConnectAsync
Type name: System.Net.Sockets.SocketException
.HResult = -2147467259
.Message = "Unknown error (0xffffffff)"
.NativeErrorCode = -1
.Source = "System.Private.CoreLib"
The stack trace for the outer exception:
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
We started receiving this error a few hours ago (after weeks / months of no problem), and a process restart was required to fix it. It occurred on every outbound HTTP request until the process was killed. Process memory use was not particularly high.
Azure App Service. Kudu versioning:

Other versions:
my\bin\folder> dotnet --version
2.2.102
From csproj:
<TargetFramework>netcoreapp2.1</TargetFramework>
I checked the TCP Connections diagnostics tool on Azure - there were only ~1000 connections being used (so I guess not port exhaustion?).
Interestingly - we're using StackExchange.Redis and at the time of the first exception, I do see that there was a connection error with Redis too.
What can I do to assist? The problem occurred in production, and while I have app-level logging, but not too much else.
Edit: the issue just re-occurred on the same server, after the process restart
@geoffkizer any idea? Is it Windows? What is the hex HResult? Did you look it up?
It's a Windows App Service, yes - the HResult is https://errorcodelookup.com/?type=hresult&code=80004005 (E_FAIL)
I just had this happen in production again on a fresh deploy, and had to restart the process, whereupon socket operations worked again. If there's anything I could or show be doing to enable some advanced logging or diagnostics - with .NET Core or Azure - let me know. Obviously I'm keen to solve this - it keeps taking out production!
I am not sure if we have that advanced production diagnostics yet. @wfurt @davidsh @geoffkizer can you please advise what kind of logs to collect to troubleshoot this?
Is there chance you can reproduce this when running under strace @kierenj ? That would tell us what is going on on OS level. (strace -f -t -e trace=network app )
Any chance you running out of system limits - like open descriptors per process?
I was also wondering about overall TCP sockets but that would not change if you restart process.
If it fails again you can use lsof or use /proc/
I’m on Windows - I used Azure tools to check # of Tcp connections and it wasnt particularly high (1000 ish?) - as above :)
@geoffkizer @stephentoub
Type name: System.Net.Sockets.SocketException .HResult = -2147467259 .Message = "Unknown error (0xffffffff)" .NativeErrorCode = -1 .Source = "System.Private.CoreLib"
Do you know where -1 could be returned from our Sockets code? Normally we would get a positive error code number from winsock.
Sorry to be a pest - but this one just hit us in production at prime-time again, another process restart was needed whereby everything started running again. Is there anything I/we can do to help track this down?
@kierenj I don't think we have actionable info. First step would be to get environment where you can sort of reproduce it (even if it takes days to hit it there). Otherwise we will have to come up with some kind of logging (potentially not existing yet) to track down more ...
It's happened on other non-production projects too in the past - if it was to reoccur and we didn't need to restart the process, is there anything I could do at that point - share enough detail to enable one of the team to jump on with a remote debugger?
Would it be any use for me to try to raise with the Azure team, in case it's an Azure thing, not a .NET Core thing?
Nothing comes to mind. After it happens it may be already too late, so remote debugger session may not be the answer anyway. Do your projects have anything in common? Given that you are the only person reporting this, I wonder if it may be some component in your projects causing it ... or you may be just lucky :( Given that it is unclear what exactly we're tracking down, I don't know who else to loop in to help ...
My general approach in mysterious things like this one, is to get some sort of repro, then instrument the binaries (Socket in this case) to gather additional info.
They all use own framework (built on top of ASP.NET Core), and they all use SE.Redis. Maybe I'll try something like having one of these apps try some socket ops, and if they don't fail, killing the process so it's restarted, until it does fail... Then I could try swapping bits out to try to narrow it down.
I'll try an Azure ticket too perhaps. I wonder if there could be some low-level anti-DDoS protection in socket code or similar that we might be falling afoul of.
I had a quick look through sockets code to see where a -1 might come from, maybe I'll do some more digging there. But if you guys aren't sure I'm not so confident :)
I have the similar issue running Lambda in AWS. The root cause is because I create HttpClient for every request. It was fixed by reusing one HttpClient instance. You may need to check your code if you create too many HttpClient instances.
Interesting, we do use separate instances but I can't understand how this would manifest itself by a fresh process either immediately and forever having this socket exception on both HTTP and Redis connections, or never (and forever) having any problems at all, with each restart rolling the dice once more?
Any ideas how this would manifest itself with this error, wouldn't it simply mean not enough free client ports not being available (which I figure would be a different exception to this) due to closed sockets remaining in CLOSE_WAIT ? Do CLOSE_WAIT sockets not persist when processes are closed (since its a part of the TCP spec at a fairly low level)?
Would you be able to share the exception/stack trace you received @nixinwang ?
As an update - I've captured some profiler tracers and sent to Azure support, who are investigating.
We can repro this easily enough on our production systems here. I'd be happy to jump on a screen-share or allow access if anyone here wanted to dig in, or look at our code.
@davidsh may have some recommendations what to look for.
Triage: We made lots of sockets changes on 3.0. There is a chance this is address. Can you please try it on 3.0?
This was intermittent and occurs only very rarely. We don't have the option of upgrading to 3.0 in this instance, unfortunately. (I'm happy to close the issue if you'd like - it is still a problem, but we automatically detect and restart the worker process when it occurs)
@karelz I have a few 3.0 upgraded services and still encounter this error

@vsadams do you have ability to reproduce it and diagnose deeper? Ideally a small transferable repro if possible ...