ASP.NET Web API OperationCanceledException when browser cancels the request
Solution 1
This is a bug in ASP.NET Web API 2 and unfortunately, I don't think there's a workaround that will always succeed. We filed a bug to fix it on our side.
Ultimately, the problem is that we return a cancelled task to ASP.NET in this case, and ASP.NET treats a cancelled task like an unhandled exception (it logs the problem in the Application event log).
In the meantime, you could try something like the code below. It adds a top-level message handler that removes the content when the cancellation token fires. If the response has no content, the bug shouldn't be triggered. There's still a small possibility it could happen, because the client could disconnect right after the message handler checks the cancellation token but before the higher-level Web API code does the same check. But I think it will help in most cases.
David
config.MessageHandlers.Add(new CancelledTaskBugWorkaroundMessageHandler());
class CancelledTaskBugWorkaroundMessageHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// Try to suppress response content when the cancellation token has fired; ASP.NET will log to the Application event log if there's content in this case.
if (cancellationToken.IsCancellationRequested)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
return response;
}
}
Solution 2
When implementing an exception logger for WebApi, it is recommend to extend the System.Web.Http.ExceptionHandling.ExceptionLogger
class rather than creating an ExceptionFilter. The WebApi internals will not call the Log method of ExceptionLoggers for canceled requests (however, exception filters will get them). This is by design.
HttpConfiguration.Services.Add(typeof(IExceptionLogger), myWebApiExceptionLogger);
Solution 3
Here's an other workaround for this issue. Just add a custom OWIN middleware at the beginning of the OWIN pipeline that catches the OperationCanceledException
:
#if !DEBUG
app.Use(async (ctx, next) =>
{
try
{
await next();
}
catch (OperationCanceledException)
{
}
});
#endif
Solution 4
I have found a bit more details on this error. There are 2 possible exceptions that can happen:
OperationCanceledException
TaskCanceledException
The first one happens if connection is dropped while your code in controller executes (or possibly some system code around that as well). While the second one happens if the connection is dropped while the execution is inside an attribute (e.g. AuthorizeAttribute
).
So the provided workaround helps to mitigate partially the first exception, it does nothing to help with the second. In the latter case the TaskCanceledException
occurs during base.SendAsync
call itself rather that cancellation token being set to true.
I can see two ways of solving these:
- Just ignoring both exceptions in global.asax. Then comes the question if it's possible to suddenly ignore something important instead?
- Doing an additional try/catch in the handler (though it's not bulletproof + there is still possibility that
TaskCanceledException
that we ignore will be a one we want to log.
config.MessageHandlers.Add(new CancelledTaskBugWorkaroundMessageHandler());
class CancelledTaskBugWorkaroundMessageHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
// Try to suppress response content when the cancellation token has fired; ASP.NET will log to the Application event log if there's content in this case.
if (cancellationToken.IsCancellationRequested)
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
catch (TaskCancellationException)
{
// Ignore
}
return response;
}
}
The only way I figured out we can try to pinpoint the wrong exceptions is by checking if stacktrace contains some Asp.Net stuff. Does not seem very robust though.
P.S. This is how I filter these errors out:
private static bool IsAspNetBugException(Exception exception)
{
return
(exception is TaskCanceledException || exception is OperationCanceledException)
&&
exception.StackTrace.Contains("System.Web.HttpApplication.ExecuteStep");
}
Solution 5
You could try changing the default TPL task exception handling behavior through web.config
:
<configuration>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
Then have a static
class (with a static
constructor) in your web app, which would handle AppDomain.UnhandledException
.
However, it appears that this exception is actually getting handled somewhere inside ASP.NET Web API runtime, before you even have a chance to handle it with your code.
In this case, you should be able to catch it as a 1st chance exception, with AppDomain.CurrentDomain.FirstChanceException
, here is how. I understand this may not be what you are looking for.
Bates Westmoreland
.NET & JavaScript developer ASP.NET MVC ASP.NET Web API Ext JS
Updated on October 20, 2020Comments
-
Bates Westmoreland over 3 years
When a user loads a page, it makes one or more ajax requests, which hit ASP.NET Web API 2 controllers. If the user navigates to another page, before these ajax requests complete, the requests are canceled by the browser. Our ELMAH HttpModule then logs two errors for each canceled request:
Error 1:
System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()
Error 2:
System.OperationCanceledException: The operation was canceled. at System.Threading.CancellationToken.ThrowIfCancellationRequested() at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<CopyResponseAsync>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<ProcessRequestAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Looking at the stacktrace, I see that the exception is being thrown from here: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http.WebHost/HttpControllerHandler.cs#L413
My question is: How can I handle and ignore these exceptions?
It appears to be outside of user code...
Notes:
- I am using ASP.NET Web API 2
- The Web API endpoints are a mix of async and non-async methods.
- No matter where I add error logging, I am unable to catch the exception in user code
- Global.asax
Applicaiton_Error
TaskScheduler.UnobservedTaskException
- ELMAH Error Filtering
void ErrorLog_Filtering
(https://code.google.com/p/elmah/wiki/ErrorFiltering)
- Global.asax
-
Bates Westmoreland about 10 yearsNeither of those allowed me to handle the exception either.
-
noseratio about 10 years@BatesWestmoreland, not even
FirstChanceException
? Have you tried handling it with a static class which persists across HTTP requests? -
Bates Westmoreland about 10 yearsThe problem I am trying to solve it to catch, and ignore these exceptions. Using
AppDomain.UnhandledException
orAppDomain.CurrentDomain.FirstChanceException
may allow me to inspect the exception, but not catch and ignore. I didn't see a way to mark these exceptions as handled using either of these approaches. Correct me if I am wrong. -
Bates Westmoreland about 10 yearsIn my case, these exceptions occur because the browser cancels the request as the user navigates to a new url.
-
Gabriel S. about 10 yearsI see. In my case, the requests are issued through the
WinHTTP
API, not from a browser. -
Bates Westmoreland about 10 yearsAs an update, this does capture some of the requests. We still see quite a few in our logs. Thanks for the work-around. Looking forward to a fix.
-
Ken Smith almost 10 yearsI used this workaround for quite a while, and it seemed to work great. Recently - I think associated with an upgrade to ASP.NET MVC 5.2 - this error started showing up in my logs again.
-
Kiran over 9 years@KenSmith: Do you still see this error after upgrading to 5.2.2?
-
KnightFox about 9 years@KiranChalla - I can confirm that the upgrade to 5.2.2 still has these errors.
-
Nate Barbettini about 9 years@KnightFox I am also seeing these errors in my application sporadically, even though I have the latest version of Web API 2.
-
Ravi about 9 yearsAs a temporary fix and in order to stop business from panicking can I add below code into my ElmahExceptionFilter??
public override void OnException(HttpActionExecutedContext context) { if (null != context.Exception && !(context.Exception is TaskCanceledException || context.Exception is OperationCanceledException)) { Elmah.ErrorSignal.FromCurrentContext().Raise(context.Exception); } base.OnException(context); }
-
M2012 over 8 yearsStill I'm getting the error. I've used above suggestion, any other clues.
-
Mohan Gopi over 8 yearsI updated ASP.NET MVC 5.2 and i added workaround also still i am getting "Task Cancelled Exception".
-
Bill.Zhuang about 8 yearsafter patch this workaround, still meet those exceptions under webapi 5.2.2.
-
Shaddy Zeineddine almost 8 years@MohanGopi please see my solution below. I ran into issues using the accepted answer and I found out it stops working in later versions of WebAPI
-
sepehr over 7 yearsIn my case ,client is retrofit (okhttp) , an android network library , not a browser . Can you guide me to find the root of this problem please?
-
seangwright over 6 yearsWhen I tried the recommendation above, I still received Exceptions when the request had been canceled even before it was passed to
SendAsync
(you can simulate this by holding downF5
in the browser on a url that makes requests to your Api. I solved this issue by also adding theif (cancellationToken.IsCancellationRequested)
check above the call toSendAsync
. Now the exceptions no longer show up when the browser quickly cancels requests. -
NitinSingh about 6 yearsI was getting this error primarily from the OWIN context and this one targets it better
-
JobaDiniz almost 6 yearsIs there a link to the github bug? Anyone knows if this is already fixed in
5.2.6
? -
vsevolod almost 6 years@JobaDiniz Bug on gihub I believe they are not going to fix it.
-
Ilya Chernomordik almost 6 yearsIt seems that the problem with this approach is that the error still pops up in the Global.asax error handling... Event though it's not sent to the exception handler
-
Ilya Chernomordik almost 6 yearsI have found some more details on when both exception happen and figured out that this workaround only works against one of them. Here are some details: stackoverflow.com/a/51514604/1671558
-
Schoof over 5 yearsIn your suggested code, you create the
response
variable inside the try and return it outside of the try. That can't possible work can it? Also where do you use the IsAspNetBugException? -
Schoof over 5 yearsShouldn't the if be before you call the base.SendAsync? This doesn't solve the issue for us at all.
-
Ilya Chernomordik over 5 yearsNo, that can't work, it's just has to be declared outside of the try/catch block of course and initialized with something like completed task. It's just an example of the solution that is not bulletproof anyway. As for the other question you use it in the Global.Asax OnError handler. If you don't use that one to log your messages, you don't need to worry anyway. If you do, this is an example of how you filter out "non errors" from system.
-
Ciaran Gallagher almost 5 yearsWith this in place, will my requests still be processed correctly in case of where a cancellation token is sent?
-
Ciaran Gallagher almost 5 yearsI tried the suggestion by @seangwright, but now my code doesn't throw exceptions but also doesn't process the request. Is there any way to ignore the cancellation request?
-
jsgoupil almost 5 yearsFor those who want to trigger this error to see how it behaves, the best way to do so is the following. Click Pause while debugging. Make a request to your API then abort it. Resume your Web API app. You will get the OperationCanceledException.
-
Triynko about 2 yearsThrowing unobserved task exceptions is a bad idea; there's a reason it's off by default. Every method with an
async void
signature would throw one, as well as any Tasks at all that get garbage collected without having observed their result. Furthermore, that's not even related to unhandled TaskCancelledExceptions. If a TaskCancelledException is being thrown, the result of the Task (cancelled) has already been observed.