dotnet-httpclient35 icon indicating copy to clipboard operation
dotnet-httpclient35 copied to clipboard

HttpClient does not close RequestStream and hang

Open sonicflare opened this issue 9 years ago • 3 comments

On .net 3.5, if request have a content, the RequestStream on WebRequest is not closed/disposed. So the actual request hang indefinitely. If the copy request task is wrapped in using, everything works as expected.

Here is the demo console app https://gist.github.com/sonicflare/c53adaf904fee86a6c340b90946677de

The problem is on this line https://github.com/tunnelvisionlabs/dotnet-httpclient35/blob/master/System.Net.Http/System.Net.Http/HttpClientHandler.cs#L320

And if the code is changed to this, request is going ok

                    .Then(streamTask =>
                    {
                        using (var stream = streamTask.Result)
                        {
                            return request.Content.CopyToAsync(stream);
                        }

                    });

sonicflare avatar Aug 09 '16 14:08 sonicflare

It's great to hear someone is finding this library useful (potentially, if bugs are fixed :wink:).

I'm thinking the fix should probably look like the following. Would you agree?

.Then(streamTask => request.Content.CopyToAsync(streamTask.Result).Finally(_ => streamTask.Result.Dispose()));

sharwell avatar Aug 09 '16 15:08 sharwell

:memo: There is a reasonable chance that #10 would fix this as well.

sharwell avatar Aug 09 '16 15:08 sharwell

Yes, your fix is more correct. Yeah, its a great library, together with Tasks backport. I ported https://github.com/PlayFab/consuldotnet to 3.5, and they only use HttpClient and Task/async. It was really pleasant. Sharepoint is the only reason people pay any attention to .net 3.5 :grinning:

sonicflare avatar Aug 09 '16 15:08 sonicflare