runtime icon indicating copy to clipboard operation
runtime copied to clipboard

SocketException "Unknown error (0xffffffff)" at System.Net.Http.ConnectHelper.ConnectAsync

Open kierenj opened this issue 7 years ago • 39 comments

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

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

kierenj avatar Feb 20 '19 15:02 kierenj

@geoffkizer any idea? Is it Windows? What is the hex HResult? Did you look it up?

karelz avatar Feb 21 '19 04:02 karelz

It's a Windows App Service, yes - the HResult is https://errorcodelookup.com/?type=hresult&code=80004005 (E_FAIL)

kierenj avatar Feb 21 '19 08:02 kierenj

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!

kierenj avatar Mar 08 '19 19:03 kierenj

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?

karelz avatar Mar 09 '19 04:03 karelz

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/ info.

wfurt avatar Mar 09 '19 05:03 wfurt

I’m on Windows - I used Azure tools to check # of Tcp connections and it wasnt particularly high (1000 ish?) - as above :)

kierenj avatar Mar 09 '19 06:03 kierenj

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

davidsh avatar Mar 11 '19 17:03 davidsh

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 avatar Mar 29 '19 09:03 kierenj

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

karelz avatar Mar 29 '19 11:03 karelz

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?

kierenj avatar Mar 29 '19 11:03 kierenj

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.

karelz avatar Mar 29 '19 11:03 karelz

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

kierenj avatar Mar 29 '19 12:03 kierenj

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.

nixinwang avatar Apr 02 '19 00:04 nixinwang

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 ?

kierenj avatar Apr 02 '19 13:04 kierenj

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.

kierenj avatar Jun 12 '19 09:06 kierenj

@davidsh may have some recommendations what to look for.

wfurt avatar Jun 12 '19 23:06 wfurt

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?

karelz avatar Oct 08 '19 21:10 karelz

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)

kierenj avatar Oct 09 '19 13:10 kierenj

@karelz I have a few 3.0 upgraded services and still encounter this error image

vsadams avatar Nov 25 '19 15:11 vsadams

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

karelz avatar Nov 26 '19 16:11 karelz