Skip to content

HttpStress: connection failures with HTTP 1.1 caused by Windows Firewall #50854

Closed

Description

@antonfirsov

Currently, the the vast majority of our HttpStress failures (#42211) are HTTP 1.1 Socket connection failures. (Socket error 10060 running inside, 10061 running outside of Docker):
https://dev.azure.com/dnceng/public/_build/results?buildId=1045441&view=logs&j=2d2b3007-3c5c-5840-9bb0-2b1ea49925f3&t=ac0b0b0f-051f-52f7-8fb3-a7e384b0dde9&l=1244
https://gist.github.com/antonfirsov/dc7af2d9cb4213c835a41f59f09a0775#file-type1-txt

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.

@wfurt @geoffkizer @scalablecory @halter73 @stephentoub thoughts?

Activity

  1. ghost added
    untriaged
    on Apr 7, 2021
  2. benaadams commented on Apr 7, 2021

    @benaadams
    Member

    Non-clean shutdown of the sockets from the client?

    Does the HttpClient ever get disposed or is the client just terminated?

  3. wfurt commented on Apr 7, 2021

    @wfurt
    Member

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

  4. benaadams commented on Apr 7, 2021

    @benaadams
    Member

    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)

  5. ManickaP commented on Apr 7, 2021

    @ManickaP
    Member

    Hmmm, could the partial reads on the client side be a culprit here?

    ("GET Partial",
    async ctx =>
    {
    using var req = new HttpRequestMessage(HttpMethod.Get, "/slow");
    int expectedLength = ctx.SetExpectedResponseContentLengthHeader(req.Headers, minLength: 2);
    using HttpResponseMessage m = await ctx.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
    ValidateStatusCode(m);
    using (Stream s = await m.Content.ReadAsStreamAsync())
    {
    s.ReadByte(); // read single byte from response and throw the rest away
    }

    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.

  6. geoffkizer commented on Apr 7, 2021

    @geoffkizer
    Contributor

    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.

  7. antonfirsov commented on Apr 8, 2021

    @antonfirsov
    ContributorAuthor

    Ok, seeing the server sockets at the first part of at my netstat -b output confused me. Here is the actual full story:

    1. 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.
    2. Setting MaxConnectionsPerServer = 2 flips this around (16k on the server, and just a few client sockets)
    3. Setting MaxConnectionsPerServer = 50 balances 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 Partial as suggested in #50854 (comment) does not change the picture.

  8. 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
  9. benaadams commented on Apr 8, 2021

    @benaadams
    Member

    Could you increase the ip addresses used to increase the ports?

    e.g. if its localhost 127.0.0.1; can actually use 127.0.0.1 to 127.255.255.255

  10. antonfirsov commented on Apr 8, 2021

    @antonfirsov
    ContributorAuthor

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

  11. 27 remaining items

  12. antonfirsov commented on Apr 20, 2021

    @antonfirsov
    ContributorAuthor

    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.

  13. antonfirsov commented on Apr 23, 2021

    @antonfirsov
    ContributorAuthor

    I think, I have a proof now, that this is very unlikely an issue with TIME_WAIT:

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

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

  14. 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
  15. karelz commented on Apr 23, 2021

    @karelz
    Member

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

  16. antonfirsov commented on May 4, 2021

    @antonfirsov
    ContributorAuthor

    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?

  17. ManickaP commented on May 4, 2021

    @ManickaP
    Member

    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.

  18. antonfirsov commented on May 4, 2021

    @antonfirsov
    ContributorAuthor

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

    I wonder about other opinions?

  19. benaadams commented on May 4, 2021

    @benaadams
    Member

    Reminds me of this SignalR/SignalR/../WebSocketHandler.cs#L245-L255

    // If this exception is due to the underlying TCP connection going away, treat as a normal close
    // rather than a fatal exception.
    COMException ce = ex as COMException;
    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.
            case 0x800703e3:
            case 0x800704cd:
            case 0x80070026:
                return false;
        }
    }
  20. antonfirsov commented on May 6, 2021

    @antonfirsov
    ContributorAuthor

    @benaadams this is interesting, do you know more details about that story? Do those COMException error codes map to WSA error codes?

    /cc @davidfowl

  21. 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
  22. davidfowl commented on May 6, 2021

    @davidfowl
    Member

    @GrabYourPitchforks wrote that code a long time ago 😄

  23. ghost removed
    in-pr
    on May 22, 2021
  24. ghost locked as resolved and limited conversation to collaborators on Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Net.Httptest-bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions