orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Enqueuing task to a work item group which should have terminated

Open vmendi opened this issue 6 years ago • 9 comments

We are seeing this kind of exception recurring every 5 minutes in our logs:

Enqueuing task "[Id=4, Status=WaitingToRun]" to a work item group which should have terminated. Likely reasons are that the task is not being 'awaited' properly or a TaskScheduler was captured and is being used to schedule tasks after a grain has been deactivated. WorkItemGroup: "WorkItemGroup:Name=[Activation: S10.240.0.66:11111:324240928*grn/7277585/00000000+0e02bc229e1341adb813571a44d93924@5a387aa6 #GrainType=Manticore.GlobalActors.GameMetricsActor Placement=CommonKeyGrainPlacementStrategy],WorkGroupStatus=Waiting. Currently QueuedWorkItems=0; Total EnQueued=13; Total processed=13; Quantum expirations=0; TaskRunner=ActivationTaskScheduler-16:Queued=0; Detailed SchedulingContext=<[Activation: S10.240.0.66:11111:324240928*grn/7277585/000000000000000000000000000000000600000007277585+0e02bc229e1341adb813571a44d93924-0xFD35CA5F@5a387aa6 #GrainType=Manticore.GlobalActors.GameMetricsActor Placement=CommonKeyGrainPlacementStrategy State=Invalid NonReentrancyQueueSize=0 EnqueuedOnDispatcher=0 InFlightCount=0 NumRunning=0 IdlenessTimeSpan=00:04:57.8471887 CollectionAgeLimit=00:02:00]>" Task.AsyncState: "System.Action" " at Orleans.Runtime.Scheduler.WorkItemGroup.EnqueueTask(Task task)\n at System.Threading.Tasks.TaskContinuation.InlineIfPossibleOrElseQueue(Task task, Boolean needsProtection)\n at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)\n at System.Threading.Tasks.Task`1.TrySetResult(TResult result)\n at System.Threading.Tasks.Task.DelayPromise.Complete()\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\n at System.Threading.TimerQueueTimer.CallCallback()\n at System.Threading.TimerQueueTimer.Fire()\n at System.Threading.ThreadPoolWorkQueue.Dispatch()"

I verified that the actor was deactivated 5 minutes before the exceptions began:

Returned from calling "[Activation: S10.240.0.66:11111:324240928*grn/7277585/00000000+0e02bc229e1341adb813571a44d93924@5a387aa6 #GrainType=Manticore.GlobalActors.GameMetricsActor Placement=CommonKeyGrainPlacementStrategy State=Deactivating]" grain's OnDeactivateAsync() method "Manticore.GlobalActors.GameMetricsActor"

I think that I have removed all the timers in our system, so I can' think of anything that is calling any actor on a 5 minutes timer. I also removed all the code in our actors' methods reducing them to "return Task.CompletedTask".

I have seen this issue happen with all actor types, I chose this example randomly.

I don't know what method is being enqueued over and over in the WorkItemGroup, but I bet it's OnDeactivateAsync.

Using Orleans 3.1.4, .NET Core 2.1 LTS.

vmendi avatar Apr 10 '20 20:04 vmendi

It looks like this was caused by a Task.Delay(), are there any of those in your grain code?

ReubenBond avatar Apr 10 '20 20:04 ReubenBond

I removed all Task.delays in our grain code @ReubenBond

vmendi avatar Apr 10 '20 20:04 vmendi

I'm basing this on the line here: System.Threading.Tasks.Task.DelayPromise.Complete()

A full heap dump may help you to find the cause. You can open it in Visual Studio and take a look at the Parallel Stacks (Tasks) view. Similar can be achieved in WinDBG to find the continuation which is being scheduled after the delay.

ReubenBond avatar Apr 10 '20 21:04 ReubenBond

Oh, I forgot to mention. Unfortunately this only happens in our production/sandbox clusters, which are Linux based. It doesn't reproduce on our Windows dev environment under Visual.

vmendi avatar Apr 10 '20 21:04 vmendi

you can use dotnet-dump to grab the dump on linux, and it even lets you run the SOS commands on those dump files without having a complete working lldb.

Even better might be to use visual studio with ssh remote debugging against your sandbox instance.

If you enable sourcelink support, you should even be able set a breakpoint in Orleans.Runtime.Scheduler.WorkItemGroup.EnqueueTask (and then once that is hit, move the breakpoint to the line that logs this warning.) That should make the culprit much easier to find.

Hope this helps!

KevinCathcart avatar Apr 10 '20 22:04 KevinCathcart

I have reason to believe this is due to our usage of AzureTableGrainStorage which pulls in Microsoft.Azure.Cosmos.Table 1.0.5, which in turn pulls in Microsoft.Azure.DocumentDB.Core 2.x. The cosmos v2 sdk still has this issue open: https://github.com/Azure/azure-cosmos-dotnet-v2/issues/77 while cosmos v3 has issue https://github.com/Azure/azure-cosmos-dotnet-v3/issues/758 closed with an assist from Reuben. We patched the AzureTableGrainStorage methods to wrap cosmos API calls within Task.Run to force them to run on the default scheduler and haven't seen the error logged.

rbitanga-manticore avatar Apr 14 '20 02:04 rbitanga-manticore

After conversation in Gitter, it seems the solution is to wait for Microsoft.Azure.Cosmos.Table v2.0.0 to come out of preview. This new version depends on the CosmosSDK v3, which is the one that actually fixes the issue https://github.com/Azure/azure-cosmos-dotnet-v3/issues/758

vmendi avatar Apr 15 '20 23:04 vmendi

This happens because CosmosSDK doesn't use ConfigureAwait(false) and tries to use Task.Run instead through a helper, but this only checks for SynchronizationContext.Current and not TaskScheduler.Current != TaskScheduler.Default so it has no effect when running on Orleans.

pentp avatar Apr 28 '20 16:04 pentp

We are marking this issue as stale due to the lack of activity in the past six months. If there is no further activity within two weeks, this issue will be closed. You can always create a new issue based on the guidelines provided in our pinned announcement.

ghost avatar Dec 07 '21 21:12 ghost