TaskCanceledException exceptions randomly occurring with ResponseBodyCanceled message

See original GitHub issue

Describe the bug

After deploying a service with YARP, we are getting exceptions as seen below. Most requests are successful, but a small portion of them are having this issue. It seems to be affecting the destination controllers that do more processing.

To Reproduce

We’ve been unable to consistently reproduce the issue in our testing environment. It only occurred under a production load. We suspected that it was either timing out or the response was too large, but we were still unable to replicate the issue when testing either of those.

Error message: “ResponseBodyCanceled: Copying the response body was canceled.” Stack trace:

System.Threading.Tasks.TaskCanceledException:
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.ThrowForFailedGetResult (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Net.Http.Http2Connection+Http2Stream.System.Threading.Tasks.Sources.IValueTaskSource.GetResult (System.Net.Http, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Net.Http.Http2Connection+Http2Stream+<ReadDataAsync>d__69.MoveNext (System.Net.Http, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Yarp.ReverseProxy.Forwarder.StreamCopier+<CopyAsync>d__3.MoveNext (Yarp.ReverseProxy, Version=1.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)

Further technical details

The service is running on a linux container in an Azure App Service using Yarp.ReverseProxy 1.1.1

The config is below:

"ReverseProxy": {
    "Routes": {
      "clientcontrollers": {
        "ClusterId": "webapi",
        "Match": { "Path": "/Client/{*id}" },
        "Order": -1
      }
    },
    "Clusters": {
      "webapi": {
        "Destinations": {
          "primary": {
            "Address": "https://example.com"
          }
        },
        "HttpRequest": {
          "ActivityTimeout": "00:04:00"
        }
      }
    }
  }

The ActivityTimeout was added after the problem started occurring but hasn’t seemed to change anything

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MihaZupancommented, Nov 27, 2022

The exception is thrown less than a second after proxying to the destination server and does not appear to be timing out.

The fact you see the TaskCanceledException while reading from the destination means that something signaled the internal cancellation token. Currently the only two conditions for that are:

  • The activity timeout fired because nothing happened for 4 minutes
  • The client disconnected

Can you check the httpContext.RequestAborted.IsCancellationRequested in the cases where you see this error? If that cancellation token is signaled, the client disconnected and YARP cancelled the read from the destination as a response. From the proxy’s perspective, that’s an error condition we can’t do anything about.

0reactions
karelzcommented, Dec 6, 2022

Triage: Seems resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is this exception happening? (TaskCanceledException )
This is due to either one of the two reasons: A server disconnection. a timeout by the Http client. The default for HttpClient...
Read more >
Task canceled exception · Issue #67 · aws/aws-sdk-net
The service was an ASP.NET application running in IIS. Exception: Message = A task was canceled. Type = System.Threading.Tasks.
Read more >
TaskCanceledException Class (System.Threading.Tasks)
Initializes a new instance of the TaskCanceledException class with a specified message that describes the error. TaskCanceledException(String, Exception).
Read more >
Solved: TaskCanceledException when debugging
Message : An unhandled exception of type 'System.Threading.Tasks.TaskCanceledException' occurred in WindowsBase.dll
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found