question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'TaskCanceledException' shouldn't be thrown unless caller canceled the task!

See original GitHub issue

In version 1.15.1

Oh the dreaded TaskCanceledException! What I have seen so many times now is that I am trying to run a bunch of functional tests in parallel. They all try to use the SDK in parallel, and then they all blow up like this:

var caches = await manager.Redis.ListByResourceGroupAsync(resourceGroup.Name); return caches.Where(c => c.ProvisioningState.Equals(“succeeded”, StringComparison.OrdinalIgnoreCase) && options.Matches(c, defaultRegion));

Message: System.Threading.Tasks.TaskCanceledException : A task was canceled.

Result StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Management.Redis.Fluent.RedisOperations.<ListByResourceGroupWithHttpMessagesAsync>d__11.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 Microsoft.Azure.Management.Redis.Fluent.RedisOperationsExtensions.<ListByResourceGroupAsync>d__6.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.TaskAwaiter1.GetResult() at ProvisioningTests.IRedisCachesExtensions.<GetAsync>d__0.MoveNext() in C:\repo\cp_psprod\test\FunctionalTests\ProvisioningTests\IRedisCachesExtensions.cs:line 17 --- 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.TaskAwaiter1.GetResult() at ProvisioningTests.Tests.TestBase.<GetOrCreateRedisCache>d__4.MoveNext() in C:\repo\cp_psprod\test\FunctionalTests\ProvisioningTests\Tests\TestBase.cs:line 40 — 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 ProvisioningTests.Tests.ScaleUpDownTests.<ScaleUpDown>d__1.MoveNext() in C:\repo\cp_psprod\test\FunctionalTests\ProvisioningTests\Tests\ScaleUpDownTests.cs:line 21

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
TimLovellSmithcommented, Nov 15, 2019

I think you are right that this is caused by the behavior of HttpClient. I would suggest you do one of two things:

  1. Close as won’t fix, please get them to fix HttpClient instead, or:
  2. Bake the workaround of checking for actual token cancellation into Fluent SDK, so that people can get a more natural experience like TimeoutException from Fluent SDK, and not have to face this confusing behavior of HttpClient. Despite this not always matching the behavior of HttpClient, at least it would allow you to design your own API’s behavior nicely, and follow the spirit of guidelines here

“Only the requesting object can issue the cancellation request, and each listener is responsible for noticing the request and responding to it in an appropriate and timely manner.”

“Requesting is distinct from listening. An object that invokes a cancelable operation can control when (if ever) cancellation is requested.”

https://docs.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads?redirectedfrom=MSDN

=> (bottom line, if I didn’t cancel the task, it shouldn’t be canceled!)

(Community, if you agree TimeoutException is better please upvote issue, if you think TaskCanceledExceptions are great and should be expected all the time even when you didn’t request them, please downvote…)

0reactions
xseeseeseecommented, Feb 6, 2020

@TimLovellSmith @qub1n Here are two options for you to set in your code and test if you still face the same issue.

  • Add retry policy
var azure = Microsoft.Azure.Management.Fluent.Azure.Configure()
                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders)
                        .WithRetryPolicy(new RetryPolicy(new HttpStatusCodeErrorDetectionStrategy(), 3))
                        .Authenticate(azureCredentials)
                        .WithDefaultSubscription();

Please check details here for using RetryPolicy

  • Increase http client timeout
var rm = azure.ManagementClients
            .Where(c => c is IResourceManagementClient)
            .DefaultIfEmpty(null)
            .FirstOrDefault() as IResourceManagementClient;
(rm as IAzureClient).HttpClient.Timeout = TimeSpan.FromMinutes(5);

Please feel free to let me know if these help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TaskCanceledException when calling Task.Delay with a ...
Curiously, the cancellation exception seems to only be thrown when the cancellation token is on Task.Delay. Put the token on the ContinueWith ...
Read more >
TaskCanceledException Constructor (System.Threading. ...
TaskCanceledException (). Initializes a new instance of the TaskCanceledException class with a system-supplied message that describes the error.
Read more >
Just because you stopped waiting for it, doesn't mean the ...
Namely, that even if the WaitAsync() call is cancelled or times-out, the original Task continues running in the background.
Read more >
Tell me why I shouldn't swallow all ...
I guess it depends on what's causing the exception to be thrown. If something's caused a cancellation within the code such as an...
Read more >
Why Do You Need a Cancellation Token in C# for Tasks?
If the task was cancelled via ThrowIfCancellationRequested() method call the exception will be the type of TaskCanceledException .
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