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

Avoid adding an HttpClient.Timeout message to ConnectTimeout exceptions #72274

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,18 @@ private void HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessa
e = toThrow = new TaskCanceledException(oce.Message, oce.InnerException, cancellationToken);
}
}
else if (!pendingRequestsCts.IsCancellationRequested)
else if (cts.IsCancellationRequested && !pendingRequestsCts.IsCancellationRequested)
{
// If this exception is for cancellation, but cancellation wasn't requested, either by the caller's token or by the pending requests source,
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
// the only other cause could be a timeout. Treat it as such.
e = toThrow = new TaskCanceledException(SR.Format(SR.net_http_request_timedout, _timeout.TotalSeconds), new TimeoutException(e.Message, e), oce.CancellationToken);

// cancellationToken could have been triggered right after we checked it, but before we checked the cts.
// We must check it again to avoid reporting a timeout when one did not occur.
if (!cancellationToken.IsCancellationRequested)
{
Debug.Assert(_timeout.TotalSeconds > 0);
e = toThrow = new TaskCanceledException(SR.Format(SR.net_http_request_timedout, _timeout.TotalSeconds), new TimeoutException(e.Message, e), oce.CancellationToken);
}
}
}
else if (e is HttpRequestException && cts.IsCancellationRequested) // if cancellationToken is canceled, cts will also be canceled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,48 @@ public async Task Timeout_SetTo30AndGetResponseQuickly_Success()
}
}

[ConditionalFact(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))]
public async Task ConnectTimeout_NotWrappedInMultipleTimeoutExceptions()
{
using var handler = CreateHttpClientHandler();

SocketsHttpHandler socketsHttpHandler = GetUnderlyingSocketsHttpHandler(handler);

socketsHttpHandler.ConnectTimeout = TimeSpan.FromMilliseconds(1);
socketsHttpHandler.ConnectCallback = async (context, cancellation) =>
{
await Task.Delay(-1, cancellation);
throw new UnreachableException();
};

using var client = CreateHttpClient(handler);
client.Timeout = TimeSpan.FromSeconds(42);

TaskCanceledException e = await Assert.ThrowsAsync<TaskCanceledException>(() => client.GetAsync(CreateFakeUri()));

TimeoutException connectTimeoutException = Assert.IsType<TimeoutException>(e.InnerException);
Assert.Contains("ConnectTimeout", connectTimeoutException.Message);

Assert.Null(connectTimeoutException.InnerException);
Assert.DoesNotContain("42", e.ToString());
}

[Fact]
public async Task UnknownOperationCanceledException_NotWrappedInATimeoutException()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it a bit more, there was a broader issue here where we would classify any unknown OCE (not just ConnectTimeout) as HttpClient.Timeout.
I tweaked the checks somewhat to catch such cases as well.

{
using var client = new HttpClient(new CustomResponseHandler((request, cancellation) =>
{
throw new OperationCanceledException("Foo");
}));
client.Timeout = TimeSpan.FromSeconds(42);

OperationCanceledException e = await Assert.ThrowsAsync<OperationCanceledException>(() => client.GetAsync(CreateFakeUri()));

Assert.Null(e.InnerException);
Assert.Equal("Foo", e.Message);
Assert.DoesNotContain("42", e.ToString());
}

[Fact]
public void DefaultProxy_SetNull_Throws()
{
Expand Down