Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The operation was canceled at TaskScheduler.UnobservedTaskException in WebAPI

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?

like image 766
Alexey Strakh Avatar asked Apr 29 '14 09:04

Alexey Strakh


2 Answers

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

like image 117
Yishai Galatzer Avatar answered Nov 15 '22 00:11

Yishai Galatzer


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;
    }
like image 32
Toolkit Avatar answered Nov 15 '22 00:11

Toolkit