7

Recently I have added error logging for every UnobservedTaskException at my backend API app. The reason is that some API call executes additional tasks which are finished AFTER I return the result, that is why I couldn't track errors there on action (web api calls) level.

Now I'm getting lots of "The operation was canceled" exceptions and not sure what to do with it. I even not sure that this exception is caused by my tasks executed by API call.

Here is stack:

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.OnAsyncHandlerCompletion(IAsyncResult ar)

Any ideas how to debug it?

Mando
  • 11,414
  • 17
  • 86
  • 167
  • Please see this answer: http://stackoverflow.com/questions/22157596/asp-net-web-api-operationcanceledexception-when-browser-cancels-the-request/22621596#22621596 – Alex Sorokoletov Sep 03 '14 at 22:00

3 Answers3

8

Task cancel exception comes typically from tasks from an aborted request. We are tracking a bug that will be fixed in 5.2 where these are not going to show up in the exception logger. So typically you can safely ignore these.

You can pick up the nightly builds on https://www.myget.org/gallery/aspnetwebstacknightly to verify this fixes your issue.

Here is a link to the bug: http://aspnetwebstack.codeplex.com/workitem/1797

Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41
  • How can you verify which version you're using? – BCarlson Sep 18 '14 at 13:16
  • Look into packages.config – Yishai Galatzer Sep 18 '14 at 14:19
  • Thanks. I've upgraded from 5.1.0 to 5.2.2, and deployed to production. Hopefully I'll have confirmation tomorrow it fixed my issue. – BCarlson Sep 18 '14 at 19:01
  • 3
    We're on 5.2.2 right now. This issue is still happening. – Peter Majeed Dec 28 '15 at 19:07
  • @petermajeed can you be more specific? And why aren't you running 5.2.3? – Yishai Galatzer Dec 28 '15 at 20:43
  • @YishaiGalatzer we're still seeing the exact issue mentioned in the question. We're not running 5.2.3 because 5.2.2 was available when we created the application and we haven't had any reason to upgrade to 5.2.3. It does seem like this issue is still persisting in 5.2.3 according to the issue @ https://aspnetwebstack.codeplex.com/workitem/2283. – Peter Majeed Dec 28 '15 at 20:56
  • Yes I see that now, makes sense. I'll ping the owners (I no longer work on Web API). You are safe for now to ignore these exceptions, that's really what the code was supposed to do anyways. Also it would be good to provide a repro project, because I believe Ryan did add a test for this when he fixed it – Yishai Galatzer Dec 29 '15 at 02:21
  • Peter are you using IExceptionLogger or ExceptionFilterAttribute? – Yishai Galatzer Dec 29 '15 at 02:35
  • @YishaiGalatzer I am using ExceptionFilterAttribute which i override and send my exceptions to Elmah. I just upgraded to 5.2.3 and this started happening. I still haven't figured out how to stop it but over 3 months I have over 8k elmahs. Also, mine is not happening on a client cancel. It happens sometimes when a controller is called. – idlehands23 Jan 21 '16 at 16:16
  • 1
    ExceptionFilterAttribute will catch these, that's expected. They mostly happen when a client has disconnected and the cancellation token was triggered. You can just safely ignore them in the attribute (there is no code change in the attribute it always caught them, but the new code might be cancelling more aggressively so you see more of them). The easiest way to deal with it, is just to ignore this exception in code. – Yishai Galatzer Jan 21 '16 at 17:57
  • @YishaiGalatzer I found a workaround here. http://stackoverflow.com/questions/22157596/asp-net-web-api-operationcanceledexception-when-browser-cancels-the-request Should i use this? or Should I just ingnore it here: public override void OnException(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext.Exception != null) Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception); base.OnException(actionExecutedContext); } – idlehands23 Jan 21 '16 at 23:46
  • There are two separate things here: 1. If you just want to block Elmah as an HttpModule, David's suggestion will mostly work 2. But if you report directly to Elmah from your Exception I would go with your approach. However your code is missing checking for the exception type (you want to check for OperationCancelledException). Also note that when a client cancels you might also get other types of exceptions. – Yishai Galatzer Jan 22 '16 at 17:50
1

You can ignore this type of error

 public class ExceptionHandlingAttribute : ExceptionFilterAttribute
  {
    public override void OnException(HttpActionExecutedContext context)
    {
      if (context.Exception.Message == "The operation was canceled.")
        return;
      var title = "friendly  title";
      var msg = "friendly customizable message";
      NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
      logger.Fatal(context.Exception);
      var resp = new HttpResponseMessage(HttpStatusCode.Gone)
        {
          Content = new StringContent(title),
          ReasonPhrase = msg
        };
      throw new HttpResponseException(resp);
    }

And in Web API controller

[ExceptionHandling]
  public class SomeController : ApiController
  {
    [HttpGet]
    public object GetSomething()
    {
      return something;
    }
Toolkit
  • 10,779
  • 8
  • 59
  • 68
  • 3
    Why not make it type safe? `actionExecutedContext.Exception.GetType() != typeof(OperationCanceledException)` – mellis481 Aug 09 '16 at 13:57
0

Getting TaskChanceledException too sometimes, but i am at Amazon Web Services boat. Usually this happens right after I uploaded new version of app. Usually app servers restart (via Beanstalk UI page) helps..

Konstantin Isaev
  • 642
  • 8
  • 14