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

Always include the original exception in HttpClient #84717

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
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 @@ -599,7 +599,7 @@ private void HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessa
// Massage things so that the cancellation exception we propagate appropriately contains the caller's token (it's possible
// multiple things caused cancellation, in which case we can attribute it to the caller's token, or it's possible the
// exception contains the linked token source, in which case that token isn't meaningful to the caller).
e = toThrow = new TaskCanceledException(oce.Message, oce.InnerException, cancellationToken);
e = toThrow = new TaskCanceledException(oce.Message, oce, cancellationToken);
}
}
else if (cts.IsCancellationRequested && !pendingRequestsCts.IsCancellationRequested)
Expand All @@ -620,7 +620,7 @@ private void HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessa
{
// If the cancellation token source was canceled, race conditions abound, and we consider the failure to be
// caused by the cancellation (e.g. WebException when reading from canceled response stream).
e = toThrow = new OperationCanceledException(cancellationToken.IsCancellationRequested ? cancellationToken : cts.Token);
e = toThrow = CancellationHelper.CreateOperationCanceledException(e, cancellationToken.IsCancellationRequested ? cancellationToken : cts.Token);
}

if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Security;
using System.Net.Sockets;
using System.Net.Test.Common;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1477,6 +1478,98 @@ public async Task Send_RequestContentNotDisposed()
await content.ReadAsStringAsync(); // no exception
}
}

public enum ExceptionScenario
{
OperationCanceledException_WithOriginalCancellationToken,
OperationCanceledException_UnknownCancellationToken,
HttpRequestException,
HttpRequestException_DuringCancellation,
UnknownException,
UnknownException_DuringCancellation,
}

public static IEnumerable<object[]> Send_InnerHandlerThrows_OuterExceptionIsCaptured_MemberData() =>
Enum.GetValues<ExceptionScenario>().Select(e => new object[] { e });

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[MemberData(nameof(Send_InnerHandlerThrows_OuterExceptionIsCaptured_MemberData))]
public async Task Send_InnerHandlerThrows_OriginalExceptionInformationIsCaptured(ExceptionScenario scenario)
{
const string InterestingExceptionMessage = "Useful information";
const string InterestingInnerExceptionMessage = "Useful inner exception information";

var reachedCustomHandler = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

bool shouldWaitForCancellation = scenario
is ExceptionScenario.OperationCanceledException_WithOriginalCancellationToken
or ExceptionScenario.OperationCanceledException_UnknownCancellationToken
or ExceptionScenario.HttpRequestException_DuringCancellation
or ExceptionScenario.UnknownException_DuringCancellation;

using var handler = new CustomResponseHandler(async (_, cancellationToken) =>
{
reachedCustomHandler.SetResult();

if (shouldWaitForCancellation)
{
try
{
await Task.Delay(-1, cancellationToken);
}
catch { }
}

var innerEx = new Exception(InterestingInnerExceptionMessage);

if (scenario == ExceptionScenario.OperationCanceledException_WithOriginalCancellationToken)
{
InterestingMethodInTheStackTrace(() => new OperationCanceledException(InterestingExceptionMessage, innerEx, cancellationToken));
}

if (scenario == ExceptionScenario.OperationCanceledException_UnknownCancellationToken)
{
InterestingMethodInTheStackTrace(() => new OperationCanceledException(InterestingExceptionMessage, innerEx, new CancellationTokenSource().Token));
}

if (scenario == ExceptionScenario.HttpRequestException ||
scenario == ExceptionScenario.HttpRequestException_DuringCancellation)
{
InterestingMethodInTheStackTrace(() => new HttpRequestException(InterestingExceptionMessage, innerEx));
}

if (scenario == ExceptionScenario.UnknownException ||
scenario == ExceptionScenario.UnknownException_DuringCancellation)
{
InterestingMethodInTheStackTrace(() => new Exception(InterestingExceptionMessage, innerEx));
}

throw new UnreachableException();
});

using var client = new HttpClient(handler);

using var cts = new CancellationTokenSource();

Task sendAsyncTask = client.SendAsync(TestAsync, new HttpRequestMessage(HttpMethod.Get, "http://foo"), cts.Token);

if (shouldWaitForCancellation)
{
await reachedCustomHandler.Task;
cts.Cancel();
}

Exception ex = await Assert.ThrowsAnyAsync<Exception>(() => sendAsyncTask);

string exceptionText = ex.ToString();

Assert.Contains(InterestingExceptionMessage, exceptionText);
Assert.Contains(InterestingInnerExceptionMessage, exceptionText);
Assert.Contains(nameof(InterestingMethodInTheStackTrace), exceptionText);

[MethodImpl(MethodImplOptions.NoInlining)]
static void InterestingMethodInTheStackTrace(Func<Exception> exceptionFactory) => throw exceptionFactory();
}
}
}

Expand Down