Skip to content

HTTP2: Hang when sending multiple requests from a HttpClient #30295

Closed

Description

@JamesNK

I'm not sure what the cause of this hang is, it appears to happen at HttpClient.SendAsync when the client has made at least one previous call.

Repro:

  1. git clone https://github.com/JamesNK/grpc-dotnet.git
  2. git checkout jamesnk/hang-multiple-calls
  3. dotnet test test\FunctionalTests --filter Name~DuplexStream_SendToUnimplementedMethodAfterResponseReceived_Hang

Logic in the test is like:

  1. Client makes HTTP/2 duplex call to the server
  2. Server immediately returns 200 and ends request.
  3. Client inspects headers.
  4. Client completes request stream.
  5. Go to 1.

It consistently hangs making the second or third call.

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview8-013248
 Commit:    135af55c46

Activity

  1. JamesNK commented on Jul 18, 2019

    @JamesNK
    MemberAuthor

    The network traffic in Wireshark is weird

    In this example the third call hung.

    image

    1. Stream 3 - Server sends RST_STREAM to the client.
    2. Stream 3 - Client sends an empty DATA to the server.
    3. Stream 5 - Client sends request HEADERS and RST_STREAM (CANCEL) to the server at the same time.

    3 is where the hang occurs.

  2. stephentoub commented on Jul 18, 2019

    @stephentoub
    Member
  3. geoffkizer commented on Jul 18, 2019

    @geoffkizer
    Contributor

    Yep, it's very likely related to those issues. The trace you posted is pretty weird, but we have some weird coordination issues here. I'm hopeful this will get addressed as part of the overall fix here.

  4. JamesNK commented on Jul 27, 2019

    @JamesNK
    MemberAuthor

    This is still broken.

  5. geoffkizer commented on Jul 27, 2019

    @geoffkizer
    Contributor

    Likely the same issue as dotnet/corefx#39819

  6. eiriktsarpalis commented on Jul 29, 2019

    @eiriktsarpalis
    Member

    I'm able to reproduce the issue with the latest nightly. However, I'm observing a different behaviour when manually copying the latest corefx build artifacts to the framework's shared folder:

    image

    The client is consistently timing out at the first iteration because it's failing to establish new streams. It might be because I copied corefx files inappropriately, however I'm not observing any runtime errors on the client side. @geoffkizer any hints?

  7. geoffkizer commented on Jul 29, 2019

    @geoffkizer
    Contributor

    I don't think this is the same issue. In the trace you provided, we're getting a GOAWAY from the server.

    We need to investigate anyway, though. How do you repro this?

  8. geoffkizer commented on Jul 30, 2019

    @geoffkizer
    Contributor

    I was able to repro with the original code from above.

    @JamesNK, am I correcting in thinking that you are not sending any request body at all here? Because it looks like we're writing the HEADERS for the second request into our buffer, but we're never flushing them because you never send any body data. So the server never receives anything and the test times out.

    If you really want to just send the headers and no data, then to ensure they get sent, you need to call FlushAsync on the request body.

  9. JamesNK commented on Jul 30, 2019

    @JamesNK
    MemberAuthor

    am I correcting in thinking that you are not sending any request body at all here?

    Yes. I came across this issue by accident: someone uses the gRPC client to call an address that is not implemented, and so immediately returns.

    If you really want to just send the headers and no data, then to ensure they get sent, you need to call FlushAsync on the request body.

    How come the first request succeeds? The hang only happens after two or three requests.

    When a HTTP/2 client streaming request starts should the gRPC client always start with a FlushAsync on the stream from HttpContext?

  10. geoffkizer commented on Jul 30, 2019

    @geoffkizer
    Contributor

    How come the first request succeeds? The hang only happens after two or three requests.

    The first request is getting lucky, basically. We may need to flush the buffer internally for a variety of reasons. In this case, it looks like it's because we're sending a SETTINGS ACK frame for the initial SETTINGS from the server.

    When a HTTP/2 client streaming request starts should the gRPC client always start with a FlushAsync on the stream from HttpContext?

    You only need to call FlushAsync when you want to ensure that any pending bits, including the headers themselves, are actually written out to the wire.

    I assume the common case is that you issue a POST, get the request body stream, write some bits into it, and then flush it. In which case you should be fine. In fact, that's why we do this sort of buffering logic, to try to do a single write to the wire that includes HEADERS and DATA for the request body.

    If you aren't going to immediately send request body data, but you do want the request to be sent to the server immediately, then you need to call FlushAsync on the request body to guarantee this.

  11. 10 remaining items

  12. davidsh commented on Aug 2, 2019

    @davidsh
    Contributor

    @karelz I'm waiting for approval from @stephentoub; if you want me to merge without that I can do so.

    The release/3.0 branch is locked until at least Monday/Tuesday next week. So, if you need @stephentoub to review changes on that PR then that's fine.

  13. karelz commented on Aug 3, 2019

    @karelz
    Member

    Parts of the fixes were in master (5.0) in PR dotnet/corefx#39638 and also in release/3.0 branch (3.0) in PR dotnet/corefx#39775.

  14. karelz commented on Aug 3, 2019

    @karelz
    Member

    @geoffkizer it seems that PR dotnet/corefx#39638 (in release/3.0 branch PR dotnet/corefx#39775) didn't really fix this issue.
    I will update their text to mention it as "incomplete fix for".
    Please let me know if it is wrong. Thanks!

  15. karelz commented on Aug 3, 2019

    @karelz
    Member

    The bug was fixed in master (5.0) in PR dotnet/corefx#39834.
    Port to release/3.0 is pending in PR dotnet/corefx#39874.

  16. geoffkizer commented on Aug 3, 2019

    @geoffkizer
    Contributor

    @karelz There were multiple issues here -- two in HttpClient, one in GRPC.

    dotnet/corefx#39638 fixed the first issue and enabled discovering the other issues.
    dotnet/corefx#39882 is the PR to fix the second HttpClient issue.

  17. geoffkizer commented on Aug 3, 2019

    @geoffkizer
    Contributor

    Perhaps we should have an issue specifically for the second HttpClient issue (re RST_STREAM with NO_ERROR). That makes for a better historical record here of what was actually fixed.

  18. karelz commented on Aug 3, 2019

    @karelz
    Member

    Yes, we should have filed separate issues. Let's call it incomplete fix at this moment as this issue tracks technically multiple bugs ...

  19. geoffkizer commented on Aug 5, 2019

    @geoffkizer
    Contributor

    reopen for 3.0

  20. karelz commented on Aug 8, 2019

    @karelz
    Member

    Fixed for 3.0 in PR dotnet/corefx#40112 and in master (5.0) in PR dotnet/corefx#39882.

  21. transferred this issue fromdotnet/corefxon Feb 1, 2020
  22. added this to the 3.0 milestone on Feb 1, 2020
  23. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions