You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HttpStress: connection failures with HTTP 1.1 caused by Windows Firewall #50854
Update 1: I no longer think this is an issue with TIME_WAIT, see #50854 (comment).
Update 2: Disabling the Windows Firewall seems to fix the issue
While running these tests, there are thousands of Kestrel sockets in lingering in TIME_WAIT. This may lead to port exhaustion, which I believe explains the CI failures.
It doesn't look like the stress client's ClientOperations are doing anything unconventional -- HttpRequestMessage-es seem to be properly disposed.
Things I tried:
Reducing -cancelRate to 0, but it did not change the amount of lingering sockets
(Unless I made a mistake in my experiment) After limiting MaxConnectionsPerServer to 100 there were still thousands of lingering sockets
I'm not familiar enough with SocketsHttpHandler codebase and Kestrel (or webservers in general), so I can't assess if what I'm seeing is extreme or not in a stress scenario.
I assume the TIME_WAIT is on server side? e.g. Kestrel closed the socket but HttpClient did not. Can you please confirm?
HttpClient will not see the EOF unless it tries to read from the socket. We may set more aggressive idle timeout policy and/or check if we are not disposing when we should. We should be able to find the particular HttpClient instance from the socket.
As I understand the closer of the socket moves to TIME_WAIT and other side moves to CLOSE_WAIT; so if the server has lots of TIME_WAIT it would suggest either lots of bad requests with server terminating them or a non-clean shutdown on the client (e.g. just exiting the process without closing the socket normally); hence why I was asking if it was being disposed, though it looks like it is (not sure what the underlying handler does when the client is disposed though)
As I understand the closer of the socket moves to TIME_WAIT and other side moves to CLOSE_WAIT; so if the server has lots of TIME_WAIT it would suggest either lots of bad requests with server terminating them or a non-clean shutdown on the client (e.g. just exiting the process without closing the socket normally);
Yeah, exactly.
We need to understand why Kestrel is closing the connections on their end.
Ok, seeing the server sockets at the first part of at my netstat -b output confused me. Here is the actual full story:
Normally, there are a few hundred lingering sockets on the server, and about 15k on the client, which means that we are opening lots of HTTP 1.1 connections in the stress test.
Setting MaxConnectionsPerServer = 2flips this around (16k on the server, and just a few client sockets)
Setting MaxConnectionsPerServer = 50balances the situation (sg like 5k on server 10k on client)
It seems like there are always around 16k sockets lingering in total, which is the size of the ephemeral port range on Windows.
Seeing both server and client sockets lingering for a while after the connections have been terminated is a normal thing AFAIK, even if the sockets are properly closed. I can't decide if what we are seeing is normal, and it's just that the CI machine is unable to handle this kind of load, or if there is actual space for improvement either in stress test implementation or in SocketsHttpHandler itself (or maybe even in Kestrel).
changed the title [-]HttpStress: connection failures with HTTP 1.1 -- thousands of server sockets in TIME_WAIT [/-][+]HttpStress: connection failures with HTTP 1.1 -- 16k sockets lingering in TIME_WAIT [/+]on Apr 8, 2021
@benaadams not a bad idea. Note that in our stress test design, there is only one Kestrel instance on a pre-configured endpoint, and only one HttpClient instance on the client side. We may consider overriding ConnectCallback to round-robin over an IP range, so the client sockets are bound to a different IP addresses.
As a first step, I'm really looking for a confirmation from more experienced folks if this is just a normal HTTP 1.1 behavior we need to accept and workaround in the tests, or do we think this might be a potential customer issue we need to address in the product?
The problem is that I'm starting to question my original assumptions.
A port exhaustion should end with WSAEADDRINUSE on Windows. This is deterministically reproducible with an application using up the port range by connecting & closing socket pairs. Seeing WSAETIMEDOUT and WSAECONNREFUSED in a manner that is not reproducible across machines is very weird.
What made me think it's port exhaustion is that if I get rid of the connection abortion scenarios, the issue disappears. But the whole picture tells me that there is some further weirdness in the OS I fail to understand.
I think, I have a proof now, that this is very unlikely an issue with TIME_WAIT:
By reducing cancelRate and the amount of GET Aborted by an order of magnitude, there are only a few thousand sockets lingering, but I'm still seeing WSAETIMEDOUT (docker) and WSAECONNREFUSED (outside of docker). From what I see, the probability of those errors happening scales with the amount of connection-recreations with a non-linear manner. They disappear when cancelRate=0 and P(GET Aborted)=0, but even with very small rate of connection recreations there is a significant chance for them to happen.
By increasing cancelRate to a very high value, while also setting TcpTimedWaitDelay = 5 minutes, actual port exhaustion starts to happen after a while, with the expected WSAEADDRINUSE error as a symptom.
The next step would be to collect ETW traces and packet captures from the test lab VM, however this gonna be difficult chore work, since the disk space is limited there, and the data we need to analyze is of several 10s of GB in scale.
changed the title [-]HttpStress: connection failures with HTTP 1.1 -- 16k sockets lingering in TIME_WAIT [/-][+]HttpStress: connection failures with HTTP 1.1 -- related to high connection recreation rate[/+]on Apr 23, 2021
@antonfirsov just an idea: What about collecting ETW traces (and hopefully as well packet capture) into circular buffer? I know ETW has the option ...
If we modify the tests/runtime to FailFast as soon as it happens, that might work ...
Disabling the Windows firewall makes the connection failures disappear, which practically rules out the possibility of a product bug for this failure type.
The probability of the issue does not scale linearly with the amount of connection recreation, so even with a very low (~0.5%) cancellation/server abortion rate there will be a significant chance for connection failures.
We need a quick, temporary workaround to remove this failure type ASAP, since the high amount of these false negatives renders our stress test reports practically useless. Considering other priorities, I would go with the easiest solution, and disable cancellation and GET Aborted for HTTP 1.1, until we'll be able to find a more sophisticated fix by root causing the firewall issue with the help of the Windows firewall team. @geoffkizer@ManickaP@alnikola agreed?
Instead of completely turning off those scenarios, could we rather catch and filter the exception by the known error codes for this issues? So that we don't miss potentially different problems.
But I don't know how hard would it be to achieve, so if it's too complicated, don't bother, it's probably not worth the effort.
@ManickaP this is the second thing I was thinking about, it might be actually a better solution. Could be done with a switch -doNotFailOnConnectionTimeout which would make us filter out SocketException(10060). My main problem with this approach is not the extra effort, but rather the (otherwise unnecessary) arbitrary complexity we would add to the test code.
// If this exception is due to the underlying TCP connection going away, treat as a normal close// rather than a fatal exception.COMExceptionce=exasCOMException;if(ce!=null){switch((uint)ce.ErrorCode){// These are the three error codes we've seen in testing which can be caused by the TCP connection going away unexpectedly.case0x800703e3:case0x800704cd:case0x80070026:returnfalse;}}
changed the title [-]HttpStress: connection failures with HTTP 1.1 -- related to high connection recreation rate[/-][+]HttpStress: connection failures with HTTP 1.1 caused by Windows Firewall[/+]on May 6, 2021
Activity
benaadams commentedon Apr 7, 2021
Non-clean shutdown of the sockets from the client?
Does the
HttpClientever get disposed or is the client just terminated?wfurt commentedon Apr 7, 2021
All the code lives in https://github.com/dotnet/runtime/tree/main/src/libraries/System.Net.Http/tests/StressTests/HttpStress @benaadams .
I assume the
TIME_WAITis on server side? e.g. Kestrel closed the socket but HttpClient did not. Can you please confirm?HttpClient will not see the EOF unless it tries to read from the socket. We may set more aggressive idle timeout policy and/or check if we are not disposing when we should. We should be able to find the particular HttpClient instance from the socket.
benaadams commentedon Apr 7, 2021
As I understand the closer of the socket moves to
TIME_WAITand other side moves toCLOSE_WAIT; so if the server has lots ofTIME_WAITit would suggest either lots of bad requests with server terminating them or a non-clean shutdown on the client (e.g. just exiting the process without closing the socket normally); hence why I was asking if it was being disposed, though it looks like it is (not sure what the underlying handler does when the client is disposed though)ManickaP commentedon Apr 7, 2021
Hmmm, could the partial reads on the client side be a culprit here?
runtime/src/libraries/System.Net.Http/tests/StressTests/HttpStress/ClientOperations.cs
Lines 197 to 209 in 740121d
I'd maybe try the test without these "ugly" operations and see if it reproduces.
edit: Not saying the code is wrong per se, just that there might be error in client/server that manifests under these conditions.
geoffkizer commentedon Apr 7, 2021
Yeah, exactly.
We need to understand why Kestrel is closing the connections on their end.
antonfirsov commentedon Apr 8, 2021
Ok, seeing the server sockets at the first part of at my
netstat -boutput confused me. Here is the actual full story:MaxConnectionsPerServer = 2flips this around (16k on the server, and just a few client sockets)MaxConnectionsPerServer = 50balances the situation (sg like 5k on server 10k on client)It seems like there are always around 16k sockets lingering in total, which is the size of the ephemeral port range on Windows.
Seeing both server and client sockets lingering for a while after the connections have been terminated is a normal thing AFAIK, even if the sockets are properly closed. I can't decide if what we are seeing is normal, and it's just that the CI machine is unable to handle this kind of load, or if there is actual space for improvement either in stress test implementation or in SocketsHttpHandler itself (or maybe even in Kestrel).
@geoffkizer @benaadams @scalablecory @ManickaP any further insights / ideas / suggestions?
Edit: Removing
GET Partialas suggested in #50854 (comment) does not change the picture.[-]HttpStress: connection failures with HTTP 1.1 -- thousands of server sockets in TIME_WAIT [/-][+]HttpStress: connection failures with HTTP 1.1 -- 16k sockets lingering in TIME_WAIT [/+]benaadams commentedon Apr 8, 2021
Could you increase the ip addresses used to increase the ports?
e.g. if its localhost
127.0.0.1; can actually use127.0.0.1to127.255.255.255antonfirsov commentedon Apr 8, 2021
@benaadams not a bad idea. Note that in our stress test design, there is only one Kestrel instance on a pre-configured endpoint, and only one
HttpClientinstance on the client side. We may consider overridingConnectCallbackto round-robin over an IP range, so the client sockets are bound to a different IP addresses.As a first step, I'm really looking for a confirmation from more experienced folks if this is just a normal HTTP 1.1 behavior we need to accept and workaround in the tests, or do we think this might be a potential customer issue we need to address in the product?
27 remaining items
antonfirsov commentedon Apr 20, 2021
The problem is that I'm starting to question my original assumptions.
A port exhaustion should end with
WSAEADDRINUSEon Windows. This is deterministically reproducible with an application using up the port range by connecting & closing socket pairs. SeeingWSAETIMEDOUTandWSAECONNREFUSEDin a manner that is not reproducible across machines is very weird.What made me think it's port exhaustion is that if I get rid of the connection abortion scenarios, the issue disappears. But the whole picture tells me that there is some further weirdness in the OS I fail to understand.
antonfirsov commentedon Apr 23, 2021
I think, I have a proof now, that this is very unlikely an issue with
TIME_WAIT:By reducing cancelRate and the amount of
GET Abortedby an order of magnitude, there are only a few thousand sockets lingering, but I'm still seeingWSAETIMEDOUT(docker) andWSAECONNREFUSED(outside of docker). From what I see, the probability of those errors happening scales with the amount of connection-recreations with a non-linear manner. They disappear whencancelRate=0andP(GET Aborted)=0, but even with very small rate of connection recreations there is a significant chance for them to happen.By increasing
cancelRateto a very high value, while also settingTcpTimedWaitDelay = 5 minutes, actual port exhaustion starts to happen after a while, with the expectedWSAEADDRINUSEerror as a symptom.The next step would be to collect ETW traces and packet captures from the test lab VM, however this gonna be difficult chore work, since the disk space is limited there, and the data we need to analyze is of several 10s of GB in scale.
[-]HttpStress: connection failures with HTTP 1.1 -- 16k sockets lingering in TIME_WAIT [/-][+]HttpStress: connection failures with HTTP 1.1 -- related to high connection recreation rate[/+]karelz commentedon Apr 23, 2021
@antonfirsov just an idea: What about collecting ETW traces (and hopefully as well packet capture) into circular buffer? I know ETW has the option ...
If we modify the tests/runtime to FailFast as soon as it happens, that might work ...
antonfirsov commentedon May 4, 2021
Disabling the Windows firewall makes the connection failures disappear, which practically rules out the possibility of a product bug for this failure type.
The probability of the issue does not scale linearly with the amount of connection recreation, so even with a very low (~0.5%) cancellation/server abortion rate there will be a significant chance for connection failures.
We need a quick, temporary workaround to remove this failure type ASAP, since the high amount of these false negatives renders our stress test reports practically useless. Considering other priorities, I would go with the easiest solution, and disable cancellation and
GET Abortedfor HTTP 1.1, until we'll be able to find a more sophisticated fix by root causing the firewall issue with the help of the Windows firewall team. @geoffkizer @ManickaP @alnikola agreed?ManickaP commentedon May 4, 2021
Instead of completely turning off those scenarios, could we rather catch and filter the exception by the known error codes for this issues? So that we don't miss potentially different problems.
But I don't know how hard would it be to achieve, so if it's too complicated, don't bother, it's probably not worth the effort.
antonfirsov commentedon May 4, 2021
@ManickaP this is the second thing I was thinking about, it might be actually a better solution. Could be done with a switch
-doNotFailOnConnectionTimeoutwhich would make us filter outSocketException(10060). My main problem with this approach is not the extra effort, but rather the (otherwise unnecessary) arbitrary complexity we would add to the test code.I wonder about other opinions?
benaadams commentedon May 4, 2021
Reminds me of this SignalR/SignalR/../WebSocketHandler.cs#L245-L255
antonfirsov commentedon May 6, 2021
@benaadams this is interesting, do you know more details about that story? Do those COMException error codes map to WSA error codes?
/cc @davidfowl
[-]HttpStress: connection failures with HTTP 1.1 -- related to high connection recreation rate[/-][+]HttpStress: connection failures with HTTP 1.1 caused by Windows Firewall[/+]davidfowl commentedon May 6, 2021
@GrabYourPitchforks wrote that code a long time ago 😄
HttpStress: Disable firewall in Windows runs (#52381)