-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Hang on when GetRequestStreamAsync #113409
base: main
Are you sure you want to change the base?
Fix Hang on when GetRequestStreamAsync #113409
Conversation
…sync throws for Non-Buffering Write case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses a regression that caused GetRequestStreamAsync to hang by introducing timeout logic in the internal request stream retrieval and propagating underlying exceptions. Key changes include:
- Wrapping the stream retrieval in a Task.WhenAny and WaitAsync-based timeout mechanism.
- Propagating exceptions when the underlying send request task fails.
- Adding a new unit test for requests with buffering disabled and an invalid host to verify correct exception handling.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs | Modified InternalGetRequestStream to use timeout logic and propagate exceptions from SendRequest. |
src/libraries/System.Net.Requests/tests/HttpWebRequestTest.cs | Introduced a new test to ensure that an invalid host with buffering disabled causes the expected exception. |
src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs
Outdated
Show resolved
Hide resolved
Task<Stream> getStreamTask = getStreamTcs.Task; | ||
try | ||
{ | ||
Task result = await Task.WhenAny((Task)getStreamTask, (Task)_sendRequestTask).WaitAsync(TimeSpan.FromMilliseconds(Timeout)).ConfigureAwait(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one thing that I've noticed is that, when user is using async path already they should implement their own timeout mechanism according to the doc: HttpWebRequest.Timeout - Remarks
Not sure, if we should stick to that behavior and limit propagating of Timeout here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WaitAsync
will not cancel / stop the underlying tasks. What happens with them? I think this will at minimum generate unobserved exceptions.
It would be better to pass the timeout to the tasks themselves via cancellation token and them catch the exception and remap to TimeoutException
if necessary. (we do this a lot around HttpClient
).
src/libraries/System.Net.Requests/src/System/Net/HttpWebRequest.cs
Outdated
Show resolved
Hide resolved
…com:liveans/dotnet-runtime into fix_httpwebrequest_nonbufferingonwrite_hang
Fixes #113398.
This is a regression, we should probably back port this to .NET 9.0