title: Task cancelled exception causing internal server error in Web API project
body: <p>I have an ASP.NET web API project. I have 3 cloud services hosted in Azure behind a traffic manager end point. Recently, there were a few failures and I need some help understanding the root-cause.</p>
  
  <p>There was higher traffic than usual but the calls were working fine. During a certain window of time (about 10 mins), one of the instances started throwing internal server errors. Even health ping requests were throwing internal server errors. API calls that were successful about an hour or two back were failing too. A stack trace is provided below.</p>
  
  <pre><code>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.&lt;InvokeActionAsyncCore&gt;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.&lt;ExecuteAsync&gt;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.&lt;ExecuteAsync&gt;d__0.MoveNext()
  </code></pre>
  
  <p>It seems to me that issue was not related to the code. One possibility is thread contention but when I created a new test cloud service and made many parallel calls to it I got time outs but not internal server errors.</p>
  
  <p><strong>Update:</strong>
  We have a request handler which is similar to the following and it seems ASP.NET framework is triggering the cancellation via the cancellation token in the handler. We have verified that the control does not flow to our controller after the handler is executed.</p>
  
  <pre><code>public class RequestMessageHandler : DelegatingHandler
      {
          protected override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
          {
              if (request == null)
              {
                  throw new ArgumentException("RequestMessage is null");
              }
  
              MyEventSource.Log.HttpIncomingStart(request);
  
              return base.SendAsync(request, cancellationToken);
          }
      }
  </code></pre>