Repository navigation
Description
Update & re-scoping of the issue
#1511 (comment)
Remaining work: We added an internal AllowDuplex property on HttpContent. We should consider making this public.
Old Update
As part of 3.0 we essentially disabled bidirectional HttpContent, implementing only what was needed for gRPC support. We now need to, as part of 5.0/Future, consider how to enable general-purpose bidirectional support.
Original issue
dotnet/corefx#36884 (HTTP/2 100 Continue) also snuck in bidirectional support (https://github.com/dotnet/corefx/issues/36855) for HTTP/2. This works pretty well but does introduce an ambiguous scenario when the server completes the response before the client completes the request.
Server code: (Kestrel 3.0.0-preview7)
var response = "hello, world";
context.Response.ContentLength = response.Length;
await context.Response.WriteAsync(response);
await context.Features.Get<IHttpResponseCompletionFeature>().CompleteAsync();
await Task.Delay(TimeSpan.FromSeconds(5));
// Drain the request body
await context.Request.Body.CopyToAsync(Stream.Null);Yes this server code is a bit unusual, it was written this way to make the error visible from CopyToAsync. You'll get similar behavior if you just write Hello World and let the request unwind.
Client code:
var client = new HttpClient();
client.DefaultRequestVersion = new Version(2, 0);
using var file = File.OpenRead(@"A200MbFile.txt");
var content = new StreamContent(file);
var response = await client.PostAsync("https://localhost:5001", content);- In HTTP/1.x this works fine, PostAsync won't complete until the request stream has been completely uploaded.
- In HTTP/2 it's ambiguous because when PostAsync completes you have a fully buffered response body but you have no idea what the state of the request is nor a way to be notified of its completion. Trying to perform any operations on that file can cause errors in the client code because it's still in use. If you close the file now the request will fail, but the error will only be visible on the server.
- HttpCompletionOptions do not have any affect on the request, they only affect the response.
- None of the built in HttpContent implementations provide any visibility on their progress. PushStreamContent from WebApi is the only HttpContent example I've seen that would surface this state to users.
Mitigations:
- Servers usually wait for the request body to end before ending the response body. However the client can't always rely on the server behaving a specific way, ending the response early is within spec.
- For >= 300 responses the HTTP/2 stream will be reset and the request stream abandon. These are the most common scenarios where the server would end the response without draining the request. This doesn't surface any errors directly to the caller unless they have a custom HttpContent, they have to infer the request body is done/abandon from the status code.
These mitigations make the issue occur infrequently enough that it will cause some very non-obvious issues when it it does happen (e.g. locked files, InvalidOperationExceptions, multi-threaded stream re-use, etc.).
Proposals:
A) At a minimum there needs to be guidance given that this might happen and how the user might handle it. I don't know what guidance could be given beyond implementing a custom HttpContent.
B) Add something to HttpContent to make progress more visible. E.g. something they could await or DisposeAsync that could wait for things to finish first. This wouldn't be very discoverable and would need to be accompanied by guidance.
C) Only enable bidirectional support when using HttpCompletionOptions.ResponseHeadersRead (or a new flag?). Most people don't use this flag so they wouldn't be impacted by the behavior change (e.g. PostAsync doesn't support that mode). I'm not sure how you implement this without B.
Activity
wfurt commentedon Jun 14, 2019
The original implementation did C) but consensus through review was to do it al the time.
cc: @stephentoub
stephentoub commentedon Jun 14, 2019
How about:
D) Don't let the response stream yield EOF until all request content has finished sending.
Would that break anything? It'd also give us a place to propagate exceptions we're currently just logging.
Tratcher commentedon Jun 14, 2019
D is certainly simpler, and doesn't depend on any of the other proposals. It might break the gRPC scenario where they send an error status in the trailers. However they usually follow that with a Reset so that might unblock it anyways. @JamesNK ?
JamesNK commentedon Jun 14, 2019
What do you mean by this?
I'll put together some gRPC streaming scenarios so we can judge if changes will break anything.
JamesNK commentedon Jun 14, 2019
This is what gRPC's HttpContent looks like:
In a client or duplex streaming call gRPC will write to the stream until the client closes it (either explicitly, or by the response ending).
Tratcher commentedon Jun 14, 2019
This part that might break you. Proposal D is for the response stream not to end until the request stream ends. Looks like a classic deadlock for you.
JamesNK commentedon Jun 14, 2019
That seems problematic. We need to read to the end of the response stream. Its completion is the only indication we have that HttpResponseMessage.TrailingHeaders has been populated with values.
I'm going to experiment with Grpc.Core (native client and server) to see what they do when the response has been completed while the client stream is still open.
JamesNK commentedon Jun 15, 2019
Ok, here is what Grpc.Core does.
Scenario 1: Client explicitly sends the request stream, completes it, and awaits a response. The server reads the entire request stream before sending a response.
Client sends an DATA END_STREAM=true frame.
Server responds with HEADER, DATA, HEADER. The final HEADER (trailers) has END_STREAM=true.
Scenario 2: Client explicitly sends the request stream, completes it, and awaits a response. The server does not read the entire request stream before sending a response.
Client never sends a DATA END_STREAM=true frame.
Server responds with HEADER, DATA, HEADER, RST_STREAM. The final HEADER (trailers) has END_STREAM=true. RST_STREAM has NO_ERROR.
JamesNK commentedon Jun 15, 2019
tldr: the server sends a RST_STREAM (NO_ERROR) frame to the client if it never reads an END_STREAM=true frame in the request stream.
@wfurt @stephentoub In option D, will the RST_STREAM force the client's request stream to close, which allow's the response stream to end?
@Tratcher Does Kestrel automatically send a RST_STREAM if the response finishes without reading the entire request? Is this behavior part of the standard? I worry about option D breaking HttpClient if a server implementation never resets the request stream when it finishes.
JamesNK commentedon Jun 15, 2019
https://tools.ietf.org/html/rfc7540#section-8.1
@Tratcher Looks optional:
Tratcher commentedon Jun 15, 2019
Yes, kestrel does Reset if the request body has not been completely read.
https://github.com/aspnet/AspNetCore/blob/04bf1bf32e13d35d4d7f61f9cbeb9bfa22e0c617/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs#L102
However, I need to confirm it doesn't try to drain the request body first:
https://github.com/aspnet/AspNetCore/blob/f652c222028ebd504203dce6a68e67b416465aab/src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs#L705
wfurt commentedon Jun 15, 2019
If HttpClient gets RST_STREAM before EOF, it will rest response stream and throw exception. We put protocol error to inner exception.
JamesNK commentedon Jun 15, 2019
In situation 2 above the response stream has been ended with END_STREAM=true. The RST_STREAM comes afterwards to tell the client to end the request stream (if it is still open).
davidsh commentedon Jun 21, 2019
@Tratcher @JamesNK Can you summarize where we are on this issue? What do we need to do for .NET Core 3.0 release? Is this now a documentation issue? Or do we need to change product code/behavior?
JamesNK commentedon Jun 25, 2019
@Tratcher is the person to talk to about this.
(also, I'm on holiday beginning of this week 🌴🌞)
32 remaining items
ryan-motive commentedon Feb 22, 2022
Has there been any movement on this issue? More generally, is there any way to wrestle HttpClient to work as a truly bi-directional HTTP/2 connection? We're talking to an HTTP/2 service and at a bit of a loss for how best to tackle this in C#. Are there any options besides building out a custom implementation using TcpClient?
alexandrehtrb commentedon Sep 5, 2022
+1
I think WebSockets is an alternative...
ryan-motive commentedon Sep 6, 2022
Yep, using Websockets currently and it's working well, but it feels like a placeholder until .NET gets proper support for HTTP/2 streaming.
karelz commentedon Sep 7, 2022
@ryan-motive can you share some details about your scenario? Why do you need it? Is it server requirement you need to match?
ryan-motive commentedon Sep 7, 2022
It's not really relevant why I need it--the spec was written in 2015 and yet this part of the spec is still not supported by the .NET networking library. This seems like a pretty significant missing piece.
karelz commentedon Sep 7, 2022
@ryan-motive I respectfully disagree. There are things RFCs specify which are rarely used and not all stacks implement them. We believe this might be one of them in HTTP/2 RFC.
In general, we prioritize work based on customer demand -- when we know about user scenarios requiring the features and/or when there are lots of +1s. If you want to help us prioritize the work in upcoming releases, then sharing details about your scenario will help us with that.
I also suggest everyone who wishes to have this feature to upvote top post. Thanks!
ryan-motive commentedon Sep 7, 2022
Then at a minimum make this the official policy and include it in the documentation so that we don't spend days digging through Github forums trying to figure out why the feature is not there.
stephentoub commentedon Sep 7, 2022
I'm a little confused. This is already possible with HTTP/2, has been since .NET Core 3.0; that's how gRPC works. What specifically isn't working? The core of this issue was addressed as described in #1511 (comment).
ryan-motive commentedon Sep 7, 2022
I don't see how this solves the problem but maybe I'm not understanding that comment? Basically I just need a stream for writing and a stream for reading a duplex connection. I wasn't able to find a solution and this thread suggested there wasn't one. I do recall reading your comment when I was trying to solve this, but frankly I didn't really understand it 😆
You're correct though--the use cases here are gRPC and there are a number of AWS services that use HTTP/2 streaming. Right now it's not possible to use C# to access those so we're having to go through a C++ wrapper which adds complexity.
davidfowl commentedon Sep 7, 2022
I wanted to reply with what @stephentoub said but I was waiting for a code sample to show what the problem was. I wrote this a long time ago, what am I missing https://github.com/davidfowl/StreamingSample?
stephentoub commentedon Sep 7, 2022
Right, what David just said. A custom HttpContent-derived type set on the request supports copying concurrently with the HttpContent on the response stream. So you can author an HttpContent that's the send side that you write to for the duration of the operation, and consume from the response HttpContent for the duration of the operation. The comments in this thread about it not working were from before we allowed it to work with HTTP/2.
ryan-motive commentedon Sep 7, 2022
Wow, I wish I'd seen this! It's long in the past now, but this is helpful to know for the future.
karelz commentedon Sep 7, 2022
@stephentoub what is left to track on this issue then?
stephentoub commentedon Sep 7, 2022
#1511 (comment)
"Notes re remaining work: We added an internal "AllowDuplex" property on HttpContent. We should consider making this public."
karelz commentedon Sep 8, 2022
Thanks @stephentoub! Top post updated to clarify for future.