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

Classify a ConnectTimeout as RequestTimedOut #1653

Merged
merged 1 commit into from
Apr 19, 2022
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
11 changes: 8 additions & 3 deletions src/ReverseProxy/Forwarder/HttpForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,18 @@ private async ValueTask<ForwarderError> HandleRequestFailureAsync(HttpContext co
{
if (requestException is OperationCanceledException)
{
if (!context.RequestAborted.IsCancellationRequested && requestCancellationSource.IsCancellationRequested)
if (context.RequestAborted.IsCancellationRequested)
{
return await ReportErrorAsync(ForwarderError.RequestTimedOut, StatusCodes.Status504GatewayTimeout);
return await ReportErrorAsync(ForwarderError.RequestCanceled, StatusCodes.Status502BadGateway);
}
else
{
return await ReportErrorAsync(ForwarderError.RequestCanceled, StatusCodes.Status502BadGateway);
#if NET6_0_OR_GREATER
Debug.Assert(requestCancellationSource.IsCancellationRequested || requestException.ToString().Contains("ConnectTimeout"), requestException.ToString());
#else
Debug.Assert(requestCancellationSource.IsCancellationRequested || requestException.ToString().Contains("ConnectHelper"), requestException.ToString());
#endif
return await ReportErrorAsync(ForwarderError.RequestTimedOut, StatusCodes.Status504GatewayTimeout);
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/ReverseProxy.Tests/Forwarder/HttpForwarderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,40 @@ public async Task RequestTimedOut_Returns504()
events.AssertContainProxyStages(new[] { ForwarderStage.SendAsyncStart });
}

[Fact]
public async Task RequestConnectTimedOut_Returns504()
{
var events = TestEventListener.Collect();

var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
httpContext.Request.Host = new HostString("example.com:3456");

var proxyResponseStream = new MemoryStream();
httpContext.Response.Body = proxyResponseStream;

var destinationPrefix = "https://microsoft.com:123/"; // Port that doesn't accept connections
var sut = CreateProxy();

using var client = new HttpMessageInvoker(new SocketsHttpHandler
{
// Time out immediately
ConnectTimeout = TimeSpan.FromTicks(1)
});

var proxyError = await sut.SendAsync(httpContext, destinationPrefix, client);

Assert.Equal(ForwarderError.RequestTimedOut, proxyError);
Assert.Equal(StatusCodes.Status504GatewayTimeout, httpContext.Response.StatusCode);
Assert.Equal(0, proxyResponseStream.Length);
var errorFeature = httpContext.Features.Get<IForwarderErrorFeature>();
Assert.Equal(ForwarderError.RequestTimedOut, errorFeature.Error);
Assert.IsAssignableFrom<OperationCanceledException>(errorFeature.Exception);

AssertProxyStartFailedStop(events, destinationPrefix, httpContext.Response.StatusCode, errorFeature.Error);
events.AssertContainProxyStages(new[] { ForwarderStage.SendAsyncStart });
}

[Fact]
public async Task RequestCanceled_Returns502()
{
Expand Down