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

Added operation cancel operation #46330

Merged
merged 29 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
const Microsoft.AspNetCore.Http.StatusCodes.Status499ClientClosedRequest = 499 -> int
Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult
Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult.ExecuteAsync(Microsoft.AspNetCore.Http.HttpContext! httpContext) -> System.Threading.Tasks.Task!
static Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult.Instance.get -> Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult!
5 changes: 5 additions & 0 deletions src/Http/Http.Abstractions/src/StatusCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ public static class StatusCodes
/// </summary>
public const int Status451UnavailableForLegalReasons = 451;

/// <summary>
/// HTTP status code 499.
singh733 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public const int Status499ClientClosedRequest = 499;
singh733 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// HTTP status code 500.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Http/WebUtilities/src/ReasonPhrases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static class ReasonPhrases
{ 429, "Too Many Requests" },
{ 431, "Request Header Fields Too Large" },
{ 451, "Unavailable For Legal Reasons" },
{ 499, "Client Closed Request" },

{ 500, "Internal Server Error" },
{ 501, "Not Implemented" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public async Task Invoke(HttpContext context)
}
catch (Exception ex)
{
if (ex is OperationCanceledException && context.RequestAborted.IsCancellationRequested)
singh733 marked this conversation as resolved.
Show resolved Hide resolved
{
_logger.RequestAbortedException();

if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status499ClientClosedRequest;
}

return;
}

_logger.UnhandledException(ex);

if (context.Response.HasStarted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ internal static partial class DiagnosticsLoggerExtensions
[LoggerMessage(1, LogLevel.Error, "An unhandled exception has occurred while executing the request.", EventName = "UnhandledException")]
public static partial void UnhandledException(this ILogger logger, Exception exception);

[LoggerMessage(4, LogLevel.Debug, "The request has aborted.", EventName = "RequestAborted")]
public static partial void RequestAbortedException(this ILogger logger);

// ExceptionHandlerMiddleware
[LoggerMessage(2, LogLevel.Warning, "The response has already started, the error handler will not be executed.", EventName = "ResponseStarted")]
public static partial void ResponseStartedErrorHandler(this ILogger logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ static async Task Awaited(ExceptionHandlerMiddlewareImpl middleware, HttpContext

private async Task HandleException(HttpContext context, ExceptionDispatchInfo edi)
{
if (edi.SourceException is OperationCanceledException && context.RequestAborted.IsCancellationRequested)
{
_logger.RequestAbortedException();
singh733 marked this conversation as resolved.
Show resolved Hide resolved

if (!context.Response.HasStarted)
{
context.Response.StatusCode = StatusCodes.Status499ClientClosedRequest;
}

return;
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
}

_logger.UnhandledException(edi.SourceException);
// We can't do anything if the response has already started, just abort.
if (context.Response.HasStarted)
Expand Down
3 changes: 3 additions & 0 deletions src/Servers/IIS/IIS/src/Core/IISHttpContext.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ public static void ConnectionBadRequest(ILogger logger, string connectionId, Mic

[LoggerMessage(4, LogLevel.Debug, @"Connection id ""{ConnectionId}"" bad request data: ""{message}""", EventName = nameof(ConnectionBadRequest))]
private static partial void ConnectionBadRequest(ILogger logger, string connectionId, string message, Microsoft.AspNetCore.Http.BadHttpRequestException ex);

[LoggerMessage(5, LogLevel.Debug, @"Connection ID ""{ConnectionId}"", Request ID ""{TraceIdentifier}"": The request has aborted.", EventName = "RequestAborted")]
public static partial void RequestAborted(ILogger logger, string connectionId, string traceIdentifier);
}
}
4 changes: 4 additions & 0 deletions src/Servers/IIS/IIS/src/Core/IISHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ protected void ReportApplicationError(Exception ex)

Log.ApplicationError(_logger, ((IHttpConnectionFeature)this).ConnectionId, ((IHttpRequestIdentifierFeature)this).TraceIdentifier, ex);
}
protected void ReportRequestAborted()
singh733 marked this conversation as resolved.
Show resolved Hide resolved
{
Log.RequestAborted(_logger, ((IHttpConnectionFeature)this).ConnectionId, ((IHttpRequestIdentifierFeature)this).TraceIdentifier);
}

public void PostCompletion(NativeMethods.REQUEST_NOTIFICATION_STATUS requestNotificationStatus)
{
Expand Down
18 changes: 13 additions & 5 deletions src/Servers/IIS/IIS/src/Core/IISHttpContextOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Server.IIS.Core;
Expand All @@ -19,7 +20,6 @@ public IISHttpContextOfT(MemoryPool<byte> memoryPool, IHttpApplication<TContext>
{
_application = application;
}

singh733 marked this conversation as resolved.
Show resolved Hide resolved
public override async Task<bool> ProcessRequestAsync()
{
var context = default(TContext);
Expand All @@ -43,7 +43,15 @@ public override async Task<bool> ProcessRequestAsync()
}
catch (Exception ex)
{
ReportApplicationError(ex);
if (ex is OperationCanceledException && _requestAborted)
singh733 marked this conversation as resolved.
Show resolved Hide resolved
{
ReportRequestAborted();
}
else
{
ReportApplicationError(ex);
}

success = false;
}

Expand Down Expand Up @@ -82,9 +90,9 @@ public override async Task<bool> ProcessRequestAsync()
}
else if (!HasResponseStarted && _requestRejectedException == null)
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
{
// If the request was aborted and no response was sent, there's no
// meaningful status code to log.
StatusCode = 0;
// If the request was aborted and no response was sent, we use status code 499 for logging
// https://learn.microsoft.com/en-us/azure/application-gateway/http-response-codes#499--client-closed-the-connection
StatusCode = StatusCodes.Status499ClientClosedRequest;
singh733 marked this conversation as resolved.
Show resolved Hide resolved
success = false;
}

Expand Down
15 changes: 11 additions & 4 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,14 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
}
catch (Exception ex)
{
ReportApplicationError(ex);
if (ex is OperationCanceledException && _connectionAborted)
singh733 marked this conversation as resolved.
Show resolved Hide resolved
{
Log.RequestAborted(ConnectionId, TraceIdentifier);
}
else
{
ReportApplicationError(ex);
}
}

KestrelEventSource.Log.RequestStop(this);
Expand Down Expand Up @@ -739,9 +746,9 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
}
else if (!HasResponseStarted)
{
// If the request was aborted and no response was sent, there's no
// meaningful status code to log.
StatusCode = 0;
// If the request was aborted and no response was sent, we use status code 499 for logging
// https://learn.microsoft.com/en-us/azure/application-gateway/http-response-codes#499--client-closed-the-connection
singh733 marked this conversation as resolved.
Show resolved Hide resolved
StatusCode = StatusCodes.Status499ClientClosedRequest;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public void Http3DisabledWithHttp1AndNoTls(EndPoint endPoint)
GeneralLog.Http3DisabledWithHttp1AndNoTls(_generalLogger, endPoint);
}

public void RequestAborted(string connectionId, string traceIdentifier)
{
GeneralLog.RequestAbortedException(_generalLogger, connectionId, traceIdentifier);
}

private static partial class GeneralLog
{
[LoggerMessage(13, LogLevel.Error, @"Connection id ""{ConnectionId}"", Request id ""{TraceIdentifier}"": An unhandled exception was thrown by the application.", EventName = "ApplicationError")]
Expand Down Expand Up @@ -99,6 +104,9 @@ private static partial class GeneralLog
[LoggerMessage(65, LogLevel.Warning, "HTTP/3 is not enabled for {Endpoint}. HTTP/3 requires TLS. Connections to this endpoint will use HTTP/1.1.", EventName = "Http3DisabledWithHttp1AndNoTls")]
public static partial void Http3DisabledWithHttp1AndNoTls(ILogger logger, EndPoint endPoint);

[LoggerMessage(66, LogLevel.Debug, @"Connection id ""{ConnectionId}"", Request id ""{TraceIdentifier}"": The request has aborted.", EventName = "RequestAborted")]
public static partial void RequestAbortedException(ILogger logger, string connectionId, string traceIdentifier);

// Highest shared ID is 65. New consecutive IDs start at 66
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}
}