Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Functions throw TaskCanceledException #5216

Closed
vijayrkn opened this issue May 8, 2019 · 21 comments
Closed

Azure Functions throw TaskCanceledException #5216

vijayrkn opened this issue May 8, 2019 · 21 comments
Assignees

Comments

@vijayrkn
Copy link

vijayrkn commented May 8, 2019

Create new Azure Function 2.0 project with an HttpTrigger function. Run it locally. Open the URL to trigger the funtion

Systems.Threading.Tasks.TaskCanceledException

in System.Private.CoreLib.dll

"A task was cancelled"

This happens for each request.

More details in this bug report : https://developercommunity.visualstudio.com/content/problem/549316/azure-functions-throw-taskcanceledexception.html

@ColbyTresness
Copy link

@Francisco-Gamino can you investigate?

@GuilhermeMorais
Copy link

GuilhermeMorais commented May 25, 2019

I got the same error, in my case the project was created using the cmd: func init
This error happens while debugging or when you publish to Azure.

Error detail:

System.Threading.Tasks.TaskCanceledException
  HResult=0x8013153B
  Message=A task was canceled.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.MetricsEventManager.FunctionActivityTracker.<<-ctor>b__8_0>d.MoveNext() in C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\Diagnostics\MetricsEventManager.cs:line 341

@zorkind
Copy link

zorkind commented Sep 13, 2019

Any news on this issue? for me this is happening in 1.0 az function. after it run for some time, exactly 229936ms (around 1 hour) i don't think this is function timeout, my function is on an app service so the timeout should be zero as default.

@arby50
Copy link

arby50 commented Oct 2, 2019

I can duplicate as well, but calling multiple HTTP clients simultaneously to the same HTTPTrigger. For example, when starting 20 simultaneous calls, 5 will error with the TaskCanceledException within the first 30 seconds.

@Francisco-Gamino
Copy link
Contributor

Hi @ankitkumarr, could you please take a look? If this is not Core-Tools related, we'll move it to the correct repo. Thanks.

@karinpm
Copy link

karinpm commented Nov 7, 2019

I am also experiencing this issue - is there any update?

@ankitkumarr
Copy link
Contributor

This looks like a host issue. Moving to that repo.

@ankitkumarr ankitkumarr transferred this issue from Azure/Azure-Functions Nov 7, 2019
@ankitkumarr
Copy link
Contributor

@yojagad would you have an insight on this? I remember you were doing some stuff with MetricsEventManager.

@spettifer
Copy link

I would be good to have at least a vague explanation for this as we are also experiencing this issue. Whilst not a show stopper for us it is at the very least inconvenient.

@mikelor
Copy link

mikelor commented Jan 2, 2020

I'm getting the same error. When I activate historical debugging, it appears to be in the MetricsEventManager.cs in Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics

The exception is thrown at on this line, in the FunctionActivityTracker
await Task.Delay(TimeSpan.FromSeconds(1), _etwTaskCancellationSource.Token);

    private class FunctionActivityTracker : IDisposable
    {
        private readonly string _executionId = Guid.NewGuid().ToString();
        private readonly object _functionMetricEventLockObject = new object();
        private ulong _totalExecutionCount = 0;
        private int _functionActivityFlushInterval;
        private CancellationTokenSource _etwTaskCancellationSource = new CancellationTokenSource();
        private ConcurrentQueue<FunctionMetrics> _functionMetricsQueue = new ConcurrentQueue<FunctionMetrics>();
        private Dictionary<string, RunningFunctionInfo> _runningFunctions = new Dictionary<string, RunningFunctionInfo>();
        private bool _disposed = false;
        private IOptionsMonitor<AppServiceOptions> _appServiceOptions;
        private IMetricsPublisher _metricsPublisher;

        internal FunctionActivityTracker(IOptionsMonitor<AppServiceOptions> appServiceOptions, IEventGenerator generator, IMetricsPublisher metricsPublisher, int functionActivityFlushInterval)
        {
            MetricsEventGenerator = generator;
            _appServiceOptions = appServiceOptions;
            _functionActivityFlushInterval = functionActivityFlushInterval;
            _metricsPublisher = metricsPublisher;
            Task.Run(
                async () =>
                {
                    try
                    {
                        int currentSecond = _functionActivityFlushInterval;
                        while (!_etwTaskCancellationSource.Token.IsCancellationRequested)
                        {
                            RaiseMetricsPerFunctionEvent();

                            if (currentSecond >= _functionActivityFlushInterval)
                            {
                                RaiseFunctionMetricEvents();
                                currentSecond = 0;
                            }
                            else
                            {
                                currentSecond = currentSecond + 1;
                            }

                            await Task.Delay(TimeSpan.FromSeconds(1), _etwTaskCancellationSource.Token);
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        // This exception gets throws when cancellation request is raised via cancellation token.
                        // Let's eat this exception and continue
                    }
                },
                _etwTaskCancellationSource.Token);
        }

`

@ankitkumarr
Copy link
Contributor

ankitkumarr commented Jan 2, 2020

Thanks @mikelor for sharing that. That is weird that the exception from there is being bubbled up. As you can see, there is a catch (TaskCanceledException) in place to take care of that exception.

I am having a hard time reproing this. I even added code to manually cancel the cancellation token right before that line, and it gets accurately handled in the catch block.

I will look more into this.

Adding @fabiocav as he may have some insight on why this is bubbling up, or a workaround.

What version of core-tools / Azure Functions Runtime are you on? You can check by running func

@mikelor
Copy link

mikelor commented Jan 3, 2020

Hi @ankitkumarr, I created a simple app the repros the problem when reading from a Service Bus queue. See the TaskCanceledFunctionApp repo.

As you mentioned the Exception gets handled, so it's probably okay. I'm showing the exceptions occurring in the debugger, but since their being swallowed up, everything continues to run.

Is this the "expected behavior"? Looks like it, but would like some confirmation.

Exception has occurred: CLR/System.Threading.Tasks.TaskCanceledException
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in System.Private.CoreLib.dll: 'A task was canceled.'
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.MetricsEventManager.FunctionActivityTracker.<<-ctor>b__10_0>d.MoveNext() in C:\azure-functions-host\src\WebJobs.Script.WebHost\Diagnostics\MetricsEventManager.cs:line 363

This sample writes to a Service Bus Queue via a Timer Trigger and reads from the queue via a Queue Trigger.

Also included is a createResourceGroup.sh shell script that will create the Azure Service Bus to write/read from.

This successfully reproduces the TaskCanceledException for me. See ReadMe for more info. You will need to add your own localsettings.json file to add a reference to the ServiceBusConnectionString generated by the script.

func -v returns 3.0.2009

I'm using Visual Studio 16.5.0 Preview 1.0
And have implemented this workaround Debug startup fails "no Function runtime available" #5145

cc: @fabiocav

@yangyadi1993
Copy link

Any updates or workaround? I am having the same issue.

@SergeyOpalev
Copy link

Do not use async void - change it to async Task instead.

@LarrySmith-1437
Copy link

LarrySmith-1437 commented May 27, 2020

same problem, with function defined using "async Task". Same exception every execution.

Funny... Converted back to non-async, but every execution is STILL throwing a task canceled exception from System.Private.CoreLib.dll

@fabiocav
Copy link
Member

@yangyadi1993 @LarrySmith-1437 is this with the same stack shared above? If so, this is not an exception that will impact runtime behavior.

@LarrySmith-1437
Copy link

@fabiocav I updated my comment above, it's happening elsewhere once I made my method non-async. So now it's not throwing from my code, but somewhere else.

@fabiocav
Copy link
Member

Understood, but is this causing a runtime exception that is impacting your application, or are you seeing that in VS while debugging and setting exceptions to throw? If your stack is different, can you please open a separate issue with the details? Thanks!

@LarrySmith-1437
Copy link

Does not seem to be impacting execution, except of course it does draw some attention and one would love to know where/why the exception is happening prior to putting code into production. I found it interesting that the exception is being thrown whether the Run method is async or not.

@masaab
Copy link

masaab commented Jul 23, 2020

I was missing an await keyword for my method. Make sure all your async methods contain await keyword.

@fabiocav
Copy link
Member

Closing as resolved.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests