Skip to content

HttpClient Http/2 doesn't flush frames when hitting the window limit #52451

Closed

Description

@deathchurch

I have come across a strange issue when sending json log events using Serilog using the HTTP sink, but the issue will only appear if the receiving ASP.NET API is behind an F5 load balancer. The issue is that not all the data will be sent to the VIP on the F5 and the sending will hang until a timeout is reached (be that in IIS, httpClient timeout or F5 timeout).

I was able to get some POST's request to work by modifying the HTTP/2 profile on the F5 (https://support.f5.com/csp/article/K04412053), altering the Receive Window setting from the default 32kb, they only support a maximum of 128KB in this setting. That will work if I am sending say 50 events, but the default for the sink and what we would like to use is 1000 events. Due to the unknown size of what a list of events may be, I can not guarantee adjusting this receive window size would be a reasonable fix.

In summary:-

Sending API ->(HTTP/2)-> Receiving API - Works OK
Sending API ->(HTTP/1.1)-> Receiving API - Works OK

Sending API ->(HTTP/2)-> F5 VIP --> F5 Server Side ->(HTTP/2)-> Receiving API - Does Not Work
Sending API ->(HTTP/1.1)-> F5 VIP --> F5 Server Side ->(HTTP/2)-> Receiving API - Works OK

I have raised a case with F5 and they seem reluctant to investigate as they have used nghttp and are able to easily send 1000 events using HTTP/2 and indeed I can use curl to do the same, so they are saying this is pointing to aspnet rather than F5.

Example curl command that works fine
curl -vkX POST -H "Content-Type: application/json" -d @Events100.json https://IPADDRESS_HERE --http2

The original setup is using aspnetcore 3.1 web api that logs using serilog with the http sink to another aspnetcore 3.1 web api that will receive these serilog events and store them in a database.

For further testing I have created a simple dotnetcore 3.1 console application that will read a file (which just contains the events serilog would have been sending) and uses a httpClient to send to a basic aspnetcore 3.1 web api that will receive the data (not using IIS but just plain Kestrel) and write out to a file, this testing also fails with the timeout scenario.

This is all running on Windows Server 2019 Core.

For reference the events I am talking about are like this:-

{
  "events": [
    {
      "Timestamp": "2021-05-05T10:25:32.5505666+00:00",
      "Level": "Information",
      "MessageTemplate": "HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms",
      "RenderedMessage": "START - HTTP \"GET\" \"/weatherforecast\" responded 200 in 2.1560 ms",
      "Properties": {
        "RequestMethod": "GET",
        "RequestPath": "/weatherforecast",
        "StatusCode": 200,
        "Elapsed": 2.156,
        "SourceContext": "Serilog.AspNetCore.RequestLoggingMiddleware",
        "RequestId": "80000812-0006-f800-b63f-84710c7967bb",
        "SpanId": "|478ab71d-48fb709e612eba23.",
        "TraceId": "478ab71d-48fb709e612eba23",
        "ParentId": "",
        "MachineName": "HOSTNAME_HERE",
        "ThreadId": 9,
        "Application": "APP_NAME_HERE"
      },
      "Renderings": {
        "Elapsed": [
          {
            "Format": "0.0000",
            "Rendering": "2.1560"
          }
        ]
      }
    },
   !-- x amount of more events here --!
  ]
}

I do have wireshark traces that I have sent to F5 and can easily reproduce the issue. Before going on to that, is this the right place to raise this issue?

Also, is this going to be a battle of vendors where each one blames the other and the customer (me) doesn't get anywhere?

Activity

  1. Tratcher commented on May 5, 2021

    @Tratcher
    Member

    Can you share the repro apps in a github repo so we can check for usage issues?

  2. deathchurch commented on May 5, 2021

    @deathchurch
    Author

    Unfortunately I will not be able to put the original API's, but I have knocked together two test apps (Sender and Receiver) that when used together with the F5 demonstrate the same issue. Bear in mind the original API's are nothing like this and are more production focused rather than rough and ready as these two are, the repo can be found here:-

    https://github.com/deathchurch/F5ASPNETISSUE

    Thanks

  3. Tratcher commented on May 5, 2021

    @Tratcher
    Member

    Notes from the repro:

    • It's a little odd to use StreamContent on the server, I'd have expected a StreamReader instead. Shouldn't hurt anything though.
    • Nothing else stands out as an obvious cause of this.

    Sending API ->(HTTP/2)-> F5 VIP --> F5 Server Side ->(HTTP/2)-> Receiving API - Does Not Work
    Sending API ->(HTTP/1.1)-> F5 VIP --> F5 Server Side ->(HTTP/2)-> Receiving API - Works OK

    The fact that it works when you change the client to use HTTP/1.1 to F5 implies an issue with the HttpClient, not the ASP.NET Server in the receiving API.

    Next steps:

    • Are you able to repro this using .NET 5? There were a lot of HTTP/2 improvements in 5.
    • Have you found any way to reproduce this without the F5 component? We don't have one of those 😁.
    • What if you tested this combo? Sending API ->(HTTP/2)-> F5 VIP --> F5 Server Side ->(HTTP/1.1)-> Receiving API

    I do have wireshark traces that I have sent to F5 and can easily reproduce the issue.

    Are they decrypted? Encrypted traces aren't going to help here.

    To rule out any issues on the server side you can also collect debug level logs from the receiving API, including decrypted wire content using the connection logging component in Kestrel: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-5.0#connection-logging

    webBuilder.ConfigureKestrel(serverOptions =>
    {
        serverOptions.Listen(IPAddress.Any, 8000, listenOptions =>
        {
            listenOptions.UseHttps();
            listenOptions.UseConnectionLogging();
        });
    });

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#configure-logging

    If we don't see any obvious issues on the server side then I'll transfer you to https://github.com/dotnet/runtime for more HttpClient help.

  4. removed their assignment
    on May 6, 2021
  5. deathchurch commented on May 6, 2021

    @deathchurch
    Author

    It's a little odd to use StreamContent on the server, I'd have expected a StreamReader instead. Shouldn't hurt anything though.

    Thanks for the feedback, the test repo I threw together quickly to try and make it as simple as possible in the hope I could coerce F5 into testing with the same components rather than them just using curl/nghttp and saying everything was fine.

    Are you able to repro this using .NET 5? There were a lot of HTTP/2 improvements in 5

    I can try using .NET 5

    The fact that it works when you change the client to use HTTP/1.1 to F5 implies an issue with the HttpClient, not the ASP.NET Server in the receiving API.

    The more I look at this, I think I am coming to the same conclusion.

    Have you found any way to reproduce this without the F5 component? We don't have one of those 😁.

    aspnetcore direct to aspnetcore works as expected with no issues.

    What if you tested this combo? Sending API ->(HTTP/2)-> F5 VIP --> F5 Server Side ->(HTTP/1.1)-> Receiving API

    Same issue, the Sending API and the VIP will not transfer all the event data and the request will hang until a timeout is fired.

    Are they decrypted? Encrypted traces aren't going to help here.

    Yes they are decrypted, the F5 can capture the whole end to end conversation (with keys) from Sending API to Receiving API. If you would like to look at them what is the best way of sending the traces to you?

    To rule out any issues on the server side you can also collect debug level logs from the receiving API, including decrypted wire content using the connection logging component in Kestrel

    I will give that a go yes.

    Thanks for your time on this.

  6. deathchurch commented on May 6, 2021

    @deathchurch
    Author

    Just to add I have tried the same tests with .NET 5 and the same issue occurs, I also have the debug logging from Kestrel enabled and also have wireshark traces that can be decrypted.

  7. Tratcher commented on May 6, 2021

    @Tratcher
    Member

    You can attach the logs and traces to this issue or send them to the email in my profile.

  8. Tratcher commented on May 6, 2021

    @Tratcher
    Member

    Logs:

    2021-05-06T10:30:54.6098507+01:00 0HM8GE6F0JTHQ:00000001 [INF] Request starting HTTP/2 POST https://10.53.251.225/ application/json;+charset=utf-8 49292 (ca22a1cb)
    ...
    2021-05-06T10:30:54.6153387+01:00 0HM8GE6F0JTHQ:00000001 [DBG] Connection id ""0HM8GE6F0JTHQ"", Request id ""0HM8GE6F0JTHQ:00000001"": started reading request body. (55b8f554)
    ...
    2021-05-06T10:31:04.4677711+01:00  [DBG] Connection id ""0HM8GE6F0JTHQ"" received FIN. (acf58720)
    ...
    2021-05-06T10:31:04.4742142+01:00  [DBG] Connection id ""0HM8GE6F0JTHQ"" is closed. The last processed stream ID was 1. (f595f1da)
    2021-05-06T10:31:04.4743285+01:00  [DBG] Connection id ""0HM8GE6F0JTHQ"" sending FIN because: ""The client closed the connection."" (59510695)
    2021-05-06T10:31:04.4748191+01:00 0HM8GE6F0JTHQ:00000001 [INF] Executed endpoint '"/ HTTP: POST"' (99874f2b)
    2021-05-06T10:31:04.4966804+01:00 0HM8GE6F0JTHQ:00000001 [ERR] Connection id ""0HM8GE6F0JTHQ"", Request id ""0HM8GE6F0JTHQ:00000001"": An unhandled exception was thrown by the application. (560e7d32)
    System.Net.Http.HttpRequestException: Error while copying content to a stream.
     ---> System.IO.IOException: The request stream was aborted.
     ---> Microsoft.AspNetCore.Connections.ConnectionAbortedException: The HTTP/2 connection faulted.
       --- End of inner exception stack trace ---
       at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
       at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
       at System.IO.Pipelines.Pipe.GetReadAsyncResult()
       at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2MessageBody.ReadAsync(CancellationToken cancellationToken)
       at System.IO.Pipelines.PipeReader.CopyToAsyncCore[TStream](TStream destination, Func`4 writeAsync, CancellationToken cancellationToken)
       at System.Net.Http.StreamToStreamCopy.<CopyAsync>g__DisposeSourceAsync|1_0(Task copyTask, Stream source)
       at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
       --- End of inner exception stack trace ---
       at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
       at System.Net.Http.HttpContent.WaitAndReturnAsync[TState,TResult](Task waitTask, TState state, Func`2 returnFunc)
       at Receiver.Startup.<>c__DisplayClass1_0.<<Configure>b__2>d.MoveNext() in C:\Visual Studio\Visual Studio 2019\Projects\F5ASPNETISSUEv5\Receiver\Startup.cs:line 48
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
    

    Summary: The client (F5) aborts the connection mid body. There's no indication of an issue on the server. It looks like the server received 24648 bytes of body data before the connection died. That's way under kestrel's window limit of 96kb.

    Failed Trace: tcp.stream eq 3

    • F5 sets its connection window size to 32768
    • The client sets its window size to 67043329(?)
    • POST Content-Length 49292
    • DATA 16384
    • 9s pause?
    • DATA 16384
    • RST_STREAM (cancel) from the client.
    • From the timing it's not clear why the client aborted at this point, nor why it waited 9s to send the second data frame.
    • The connection window would be full at this point and F5 hasn't sent any window updates.

    Success Trace: tcp.stream eq 1

    • F5 sets its connection window size to 131072
    • The client sets its window size to 67043329(?)
    • POST Content-Length 49292
    • DATA 16384
    • DATA 16384
    • DATA 16384
    • DATA 140
    • DATA 0 END STREAM
    • F5 responds with a Window Update of 114689 for the connection (not the stream) (why is this more than 49292?)
    • 200 OK response, content-length 0, END STREAM
    • RST_STREAM (cancel) from the client.
    • From the timing it's not clear why the client aborted at this point, nor why it waited 9s to send the second data frame.
    • The connection window would be full at this point and F5 hasn't sent any window updates.
    • The connection is aborted without either side sending a GoAway(?)

    So it looks like F5 isn't sending incremental Window Updates for the stream or connection, and the client is correctly waiting for those updates. This is clearly an issue with the F5 proxy. I'd be curious to see a matching trace from CURL.

  9. deathchurch commented on May 7, 2021

    @deathchurch
    Author

    Thanks for the feedback, the RESET after 9 seconds (its closer to 10 seconds) is down to the fact in the Sender console app the httpClient is set by default to a timeout of 10 seconds, I can set this to a different value and it will just increase the delay in the traces. This is what we were seeing in the production API's, by default the httpClient didnt have a timeout and it was hitting the IIS timeout of ~110 seconds. As you say though, its not clear why there is a pause up to a timeout before sending the second data frame.

    I will do a trace with curl and email over you the results.

  10. Tratcher commented on May 7, 2021

    @Tratcher
    Member

    Curl trace: tcp.stream eq 0

    • F5 sets its connection window size to 32768
    • The client sets its window size to 33554432(?)
    • The client sets sends a connection window update to 33488897
    • POST Content-Length 46486 (smaller than HttpClient?)
    • DATA 16384
    • DATA 16384
    • F5 sends a stream window update of 32768
    • DATA 13718
    • 200 OK response

    So F5 is sending a window update in this scenario, which allows the client to continue the request.

    Maybe that HttpClient 9s pause is the root issue here, it's failing to flush that data frame for some reason and F5 doesn't send a window update until the window is full.

    Using .NET 5.0 I was able to reproduce this with Kestrel using a larger request body (100kb) to exceed kestrel's 96kb stream window size. I made the server avoid consuming the data and sending window updates, an the client times out.

            static async Task Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                using var client = new HttpClient();
                client.Timeout = TimeSpan.FromSeconds(10);
                client.DefaultRequestVersion = HttpVersion.Version20;
                var response = await client.PostAsync("https://localhost:5001", new ByteArrayContent(new byte[100 * 1024]));
                Console.WriteLine(response);
            }
                    endpoints.Map("/", async context =>
                    {
                        await Task.Delay(TimeSpan.FromSeconds(15));
                        await context.Response.WriteAsync("Hello World!");
                    });
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received HEADERS frame for stream ID 1 with length 28 and flags END_HEADERS
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
          Request starting HTTP/2 POST https://localhost:5001/  102400
    info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
          Executing endpoint '/'
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received DATA frame for stream ID 1 with length 16384 and flags NONE
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
          Connection id "0HM8HE4K0ED0D" received RST_STREAM frame for stream ID 1 with length 4 and flags 0x0
    trce: Microsoft.AspNetCore.Server.Kestrel[37]
    

    The client sends 5 full frames, 80kb, and then waits for the timeout. Then it sends one more 16kb frame and the RST_STREAM together. 16kb would have put it right at the 96kb limit, but it still should have sent the data. It sounds like it doesn't flush if it hits the window limit.

    I'm going to transfer this over to the HttpClient owners.

  11. changed the title [-]Issues sending POST requests using HTTP/2 via F5 Load Balancer[/-] [+]HttpClient Http/2 doesn't flush frames when hitting the window limit[/+] on May 7, 2021
  12. transferred this issue fromdotnet/aspnetcoreon May 7, 2021
  13. 8 remaining items

  14. added
    enhancement
    tenet-performance
    and removed
    untriaged
    on May 11, 2021
  15. karelz commented on May 11, 2021

    @karelz
    Member

    Triage: This is very weird F5 load balancer behavior. We should look at the code and if we think it affects other scenarios / other servers, then we should fix it. Otherwise move it to Future.

  16. geoffkizer commented on May 12, 2021

    @geoffkizer
    Contributor

    It may be weird behavior from F5, but it's still legal. It seems like we could hit this in other cases as well, it's just not nearly as likely.

    It sounds like @Tratcher has diagnosed the problem:

    The client sends 5 full frames, 80kb, and then waits for the timeout. Then it sends one more 16kb frame and the RST_STREAM together. 16kb would have put it right at the 96kb limit, but it still should have sent the data. It sounds like it doesn't flush if it hits the window limit.

    If this analysis is correct, we should definitely fix this.

  17. ghost added
    in-pr
    on May 15, 2021
  18. deathchurch commented on May 21, 2021

    @deathchurch
    Author

    Thanks @geoffkizer I think F5 were of the same opinion, they quoted https://datatracker.ietf.org/doc/html/rfc7540#section-5.2.1, and that on the client side they wait for the window to be filled before sending a window update, also they queried why they would they be expected to update the window if its not being used. I think they were open to any suggestions from Microsoft as to what they could improve.

    I see you have a PR, hopefully that will resolve the problem, would that be targeted only to net6.0 or would that be available for net5.0 and netcore3.1?

  19. ghost removed
    in-pr
    on May 21, 2021
  20. Tratcher commented on May 21, 2021

    @Tratcher
    Member

    @karelz for the backport question. Since this is a client stall and an interop issue it is a good candidate for backport.

  21. geoffkizer commented on May 21, 2021

    @geoffkizer
    Contributor

    @deathchurch The current F5 behavior will result in poor performance. The client will send up to the window size, then it will need to wait until it gets a window update from the server. This will result in a stall in sending approx equal to the round trip time.

    To improve performance, they should send window updates more frequently. Exactly how frequently is a somewhat complex question. For HttpClient, we send a window update for every approx (total window size / 8) bytes consumed by the app. I believe ASP.NET does something similar. But we're also looking at changing our receive window size heuristics for 6.0. There's not necessarily a perfect answer here, but what they are currently doing is definitely suboptimal.

  22. karelz commented on May 22, 2021

    @karelz
    Member

    Given the above (the fact F5 uses something that leads to more problems), I wonder if we should backport our fix or not.
    @deathchurch how much is it blocking you? If the fix is only in 6.0, are you able to consume it and use it to unblock yourself?

    If we have more customers hitting it, I am all for backporting.

  23. deathchurch commented on May 24, 2021

    @deathchurch
    Author

    @karelz our current policy is to be on the LTS version of framework, so moving to 6.0 would be a 2022 task, probably looking at 12-18 months of staying on 3.1. At the moment for a workaround wherever we have seen this issue, or potentially could see the issue we are having to explicitly set the client side profile on the F5 to only offer HTTP/1.1, the server side can still be HTTP/2, but it would be better to have the whole call chain as HTTP/2.

    @geoffkizer I will pass those suggestions onto F5, in fact they might have already viewed this thread.

  24. karelz commented on May 25, 2021

    @karelz
    Member

    Thanks @deathchurch for the context. Let's see if more customers are hit by this then.

  25. ghost locked as resolved and limited conversation to collaborators on Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.Httpenhancementtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions