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 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public async Task Invoke(HttpContext context)
}
catch (Exception ex)
{

singh733 marked this conversation as resolved.
Show resolved Hide resolved
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 = 499;
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}

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 = 499;
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}

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, "The request has aborted.", EventName = "RequestAborted")]
public static partial void RequestAborted(ILogger logger);
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
}

public void PostCompletion(NativeMethods.REQUEST_NOTIFICATION_STATUS requestNotificationStatus)
{
Expand Down
15 changes: 14 additions & 1 deletion 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,7 @@ public IISHttpContextOfT(MemoryPool<byte> memoryPool, IHttpApplication<TContext>
{
_application = application;
}

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

if (!HasResponseStarted)
{
StatusCode = StatusCodes.Status499ClientClosedRequest;
}

return false;
}

ReportApplicationError(ex);
singh733 marked this conversation as resolved.
Show resolved Hide resolved
success = false;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,20 @@ private async Task ProcessRequests<TContext>(IHttpApplication<TContext> applicat
// (DisposeContext logs StatusCode).
SetBadRequestState(ex);
ReportApplicationError(ex);
}
}
singh733 marked this conversation as resolved.
Show resolved Hide resolved
catch (Exception ex)
{
if (ex is OperationCanceledException && RequestAborted.IsCancellationRequested)
{
Log.RequestAborted();

if (!HasResponseStarted)
{
StatusCode = 499;
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}

return;
}
ReportApplicationError(ex);
singh733 marked this conversation as resolved.
Show resolved Hide resolved
}

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()
{
GeneralLog.RequestAbortedException(_generalLogger);
}

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, "The request has aborted.", EventName = "RequestAborted")]
singh733 marked this conversation as resolved.
Show resolved Hide resolved
public static partial void RequestAbortedException(ILogger logger);

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