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
HTTP2: Missing duplex steaming - HttpClient.SendAsync with HttpCompletionOption.ResponseHeadersRead #29255
HttpResponseMessage only returns when HttpRequestMessage.Content has finished sending. Even if the server has finished and returned the entire request, SendAsync still waits on the request content's SerializeToStreamAsync method completing.
publicclassPushStreamContent:HttpContent{privatereadonlyFunc<Stream,Task>_onStreamAvailable;publicPushStreamContent(Func<Stream,Task>onStreamAvailable){_onStreamAvailable=onStreamAvailable;}protectedoverrideasyncTaskSerializeToStreamAsync(Streamstream,TransportContextcontext){await_onStreamAvailable(stream);}protectedoverrideboolTryComputeLength(outlonglength){// We can't know the length of the content being pushed to the output stream.length=-1;returnfalse;}}
varhttpRequest=newHttpRequestMessage();httpRequest.Version=newVersion(2,0);httpRequest.RequestUri=newUri("https://localhost:50051"+path);httpRequest.Method=HttpMethod.Post;httpRequest.Content=newPushStreamContent(async stream =>{Console.WriteLine("Sending message 1");awaitstream.WriteAsync(Encoding.UTF8.GetBytes("Hello world 1"));// removing this delay will allow headers and content to return as soon as they are availableawaitTask.Delay(TimeSpan.FromSeconds(10));Console.WriteLine("Sending message 2");awaitstream.WriteAsync(Encoding.UTF8.GetBytes("Hello world 2"));});
(btw let me know if this is the wrong way to stream content from the client to the server)
HttpResponseMessage only returns when HttpRequestMessage.Content has finished sending. Even if the server has finished and returned the entire request, SendAsync still waits on the request content's SerializeToStreamAsync method completing.
That is how HttpClient works even with HTTP/1.1. While HttpCompletionOption.ResponseHeadersRead won't download the response body, the general architecture of HttpClient and it's handler architecture (HttpMessageHandler.SendAsync) requires that the request body be fully sent before a response can be received, even just response headers.
Note that we'd had support for it in SocketsHttpHandler for HTTP/1.1, but removed it in dotnet/corefx#27341 prior to .NET Core 2.1 shipping due to a lack of strong need for it and complication it incurred / lack of robustness in the implementation.
furt@Ubuntu16:~/repro/ConsoleApp1$ /home/furt/repro/ConsoleApp1/bin/Debug/netcoreapp3.0/linux-x64/publish/ConsoleApp1
Starting HTTP/2 call - https://localhost:50051/response-body-flushasync
Sending message 1
Headers received - 0.26169719999999996
Sending message 2
Content received - 10.2539346
Starting HTTP/2 call - https://localhost:50051/response-startasync
Sending message 1
Headers received - 5.0222359999999995
Sending message 2
Content received - 10.021061699999999
Starting HTTP/2 call - https://localhost:50051/response-bodywriter-flushasync
Sending message 1
Headers received - 0.0209085
Sending message 2
Content received - 10.023050099999999
Press any key to exit
One thing I'm not sure about is how to propagate errors from request body if we return response back before finished. We can probably raise exception on response content but that does not seems quite right. We can also simply ignore them if server was happy to give response before complete request.
Yes that looks like what I would have expected. I'd like to ensure content is being sent and received on both sides. I will extend the test app to have the server to react to content from the client and stream it back to the client, and have more logging.
We can probably raise exception on response content but that does not seems quite right.
The client app is writing content to the stream in HttpContent.SerializeToStream, so I would expect errors in sending the request come from writing to that stream. I think that would work well for gRPC because the gRPC client's call.RequestStream.WriteAsync(message) call would write to that stream, so an error thrown from it would propagate the error immediately to the user.
httpRequest.Content=newPushStreamContent(async stream =>{awaitstream.WriteAsync(Encoding.UTF8.GetBytes("Hello world 1"));awaitTask.Delay(TimeSpan.FromSeconds(10));// An error sending request content would be raised from WriteAsyncawaitstream.WriteAsync(Encoding.UTF8.GetBytes("Hello world 2"));});
Will reading the response stream error after HttpResponseMessage is returned? If so then writing to the request stream throwing an error would be a nice symmetry.
varresponse=awaithttpClient.SendAsync(request,HttpCompletionOption.ResponseHeadersRead);varms=newMemoryStream();varstream=awaitresponse.ReadAsStreamAsync();awaitstream.CopyToAsync(ms);// Will this error today?
Note that we'd had support for it in SocketsHttpHandler for HTTP/1.1, but removed it in dotnet/corefx#27341 prior to .NET Core 2.1 shipping due to a lack of strong need for it and complication it incurred / lack of robustness in the implementation.
This makes me sad, I thought we left that support in there. Streaming is one of the important for GRPC so I hope we can fix this.
changed the title [-]HttpClient.SendAsync with HTTP/2 and HttpCompletionOption.ResponseHeadersRead[/-][+]HTTP2: Missing duplex steaming - HttpClient.SendAsync with HttpCompletionOption.ResponseHeadersRead[/+]on Apr 25, 2019
Activity
davidsh commentedon Apr 14, 2019
@wfurt
JamesNK commentedon Apr 14, 2019
I've narrowed down when it happens.
HttpResponseMessage only returns when HttpRequestMessage.Content has finished sending. Even if the server has finished and returned the entire request, SendAsync still waits on the request content's
SerializeToStreamAsyncmethod completing.(btw let me know if this is the wrong way to stream content from the client to the server)
Repo of this issue: WebApplication37.zip
This issue blocks using HttpClient for HTTP/2 duplex streaming.
davidsh commentedon Apr 14, 2019
That is how HttpClient works even with HTTP/1.1. While HttpCompletionOption.ResponseHeadersRead won't download the response body, the general architecture of HttpClient and it's handler architecture (HttpMessageHandler.SendAsync) requires that the request body be fully sent before a response can be received, even just response headers.
@stephentoub @geoffkizer
stephentoub commentedon Apr 15, 2019
Note that we'd had support for it in SocketsHttpHandler for HTTP/1.1, but removed it in dotnet/corefx#27341 prior to .NET Core 2.1 shipping due to a lack of strong need for it and complication it incurred / lack of robustness in the implementation.
wfurt commentedon Apr 16, 2019
Do you expect something like this @JamesNK
One thing I'm not sure about is how to propagate errors from request body if we return response back before finished. We can probably raise exception on response content but that does not seems quite right. We can also simply ignore them if server was happy to give response before complete request.
JamesNK commentedon Apr 16, 2019
Yes that looks like what I would have expected. I'd like to ensure content is being sent and received on both sides. I will extend the test app to have the server to react to content from the client and stream it back to the client, and have more logging.
The client app is writing content to the stream in HttpContent.SerializeToStream, so I would expect errors in sending the request come from writing to that stream. I think that would work well for gRPC because the gRPC client's
call.RequestStream.WriteAsync(message)call would write to that stream, so an error thrown from it would propagate the error immediately to the user.Will reading the response stream error after HttpResponseMessage is returned? If so then writing to the request stream throwing an error would be a nice symmetry.
davidfowl commentedon Apr 17, 2019
This makes me sad, I thought we left that support in there. Streaming is one of the important for GRPC so I hope we can fix this.
JamesNK commentedon Apr 17, 2019
@wfurt I've updated the test app to have the client stream data to the server and the server to stream it back in response.
WebApplication37.zip
Could you try it out with your changes and post the console output? (the client and the server output would be useful to see)
The results I hope to see is the client sending and receiving data with the server.
wfurt commentedon Apr 17, 2019
Ok, I'll give it try @JamesNK. I need to polish error handling on my experiment.
[-]HttpClient.SendAsync with HTTP/2 and HttpCompletionOption.ResponseHeadersRead[/-][+]HTTP2: Missing duplex steaming - HttpClient.SendAsync with HttpCompletionOption.ResponseHeadersRead[/+]JamesNK commentedon May 15, 2019
Interop test repo:
Currently the test will hang with this output:
Expected behavior is the client sends and receives messages with the server in the context of a gRPC duplex streaming call.
Replace step 4 of repo with this command to see the test succeed:
`