Skip to content
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 HTTP/1.1 concurrent request content read race condition #86715

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ internal sealed partial class HttpConnection : HttpConnectionBase
private bool _inUse;
private bool _detachedFromPool;
private bool _canRetry;
private bool _startedSendingRequestBody;
private bool _connectionClose; // Connection: close was seen on last response

private const int Status_Disposed = 1;
Expand Down Expand Up @@ -524,7 +523,6 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, boo
HttpMethod normalizedMethod = HttpMethod.Normalize(request.Method);

_canRetry = false;
_startedSendingRequestBody = false;

// Send the request.
if (NetEventSource.Log.IsEnabled()) Trace($"Sending request: {request}");
Expand Down Expand Up @@ -630,7 +628,7 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, boo
// The server shutdown the connection on their end, likely because of an idle timeout.
// If we haven't started sending the request body yet (or there is no request body),
// then we allow the request to be retried.
if (!_startedSendingRequestBody)
if (request.Content is null || allowExpect100ToContinue is not null)
{
_canRetry = true;
}
Expand Down Expand Up @@ -825,7 +823,11 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, boo
cancellationRegistration.Dispose();

// Make sure to complete the allowExpect100ToContinue task if it exists.
allowExpect100ToContinue?.TrySetResult(false);
if (allowExpect100ToContinue is not null && !allowExpect100ToContinue.TrySetResult(false))
{
// allowExpect100ToContinue was already signaled and we may have started sending the request body.
_canRetry = false;
}

if (_readAheadTask != default)
{
Expand Down Expand Up @@ -939,9 +941,6 @@ private CancellationTokenRegistration RegisterCancellation(CancellationToken can

private async ValueTask SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, bool async, CancellationToken cancellationToken)
{
// Now that we're sending content, prohibit retries of this request by setting this flag.
_startedSendingRequestBody = true;

Debug.Assert(stream.BytesWritten == 0);
if (HttpTelemetry.Log.IsEnabled()) HttpTelemetry.Log.RequestContentStart();

Expand Down
Loading