Skip to content

ORLEANS0217: Pass AttemptCancellationToken to calls in task-cancellation cleanup

PropertyValue
SeverityWarning
CategoryUsage
Code fixAvailable

Code which runs only after task cancellation passes DurableTaskContext.CancellationToken to a call. Such code is the handler of a catch clause, or the true branch of an if statement or a conditional expression, whose filter or condition is DurableTaskContext.IsTaskCancellationRequested, alone or as an operand of &&:

public async DurableTask<OrderResult> HoldAsync(string room)
{
try
{
await Hold(room).WithId("hold");
await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period");
return await Confirm(room).WithId("confirm");
}
catch (OperationCanceledException) when (DurableTask.Current.IsTaskCancellationRequested)
{
// Task cancellation has already canceled CancellationToken, so the release ends at once.
await rooms.ReleaseAsync(room, DurableTask.Current.CancellationToken);
return new OrderResult(OrderStatus.Cancelled, DurableTask.Current.TaskCancellationReason);
}
}

A durable task’s context carries two cancellation tokens:

  • CancellationToken is canceled by task cancellation and by attempt cancellation, so the calls which ordinary task code makes end promptly in both cases.
  • AttemptCancellationToken is canceled only by attempt cancellation, for example when the task’s activation stops.

Task cancellation is recorded with the task, so IsTaskCancellationRequested stays true, and CancellationToken stays canceled, in the canceled attempt and in every later attempt. Cleanup which runs after task cancellation therefore passes a canceled token, and a call which observes it ends at once. In the example, ReleaseAsync throws OperationCanceledException before it releases the room, the exception escapes the body, and the task ends canceled with the room still held.

The rule inspects the code of the region itself. Lambdas and local functions in the region can run in another task’s context, such as a step’s, so the rule skips them.

For the two tokens, see Task cancellation and attempt cancellation, and for cleanup, see Compensate inline when the workflow is canceled.

Pass AttemptCancellationToken. The code fix replaces CancellationToken with AttemptCancellationToken and keeps the receiver. The call runs to completion, and it still ends promptly when the host cancels the attempt:

public async DurableTask<OrderResult> HoldOrReleaseAsync(string room)
{
try
{
await Hold(room).WithId("hold");
await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period");
return await Confirm(room).WithId("confirm");
}
catch (OperationCanceledException) when (DurableTask.Current.IsTaskCancellationRequested)
{
// AttemptCancellationToken is canceled only when the host cancels the attempt, so the release runs to completion.
await rooms.ReleaseAsync(room, DurableTask.Current.AttemptCancellationToken);
return new OrderResult(OrderStatus.Cancelled, DurableTask.Current.TaskCancellationReason);
}
}

Release in a shielded step. A step receives its own cancellation token, and the task’s cancellation doesn’t reach a shielded step. The step records its outcome, so a replay of the handler returns it and releases the room once:

public async DurableTask<OrderResult> HoldOrReleaseOnceAsync(string room)
{
try
{
await Hold(room).WithId("hold");
await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period");
return await Confirm(room).WithId("confirm");
}
catch (OperationCanceledException) when (DurableTask.Current.IsTaskCancellationRequested)
{
// The task's cancellation doesn't reach a shielded step, and the step records the release.
await DurableTask.Run(ct => rooms.ReleaseAsync(room, ct)).WithId("release").Shielded();
return new OrderResult(OrderStatus.Cancelled, DurableTask.Current.TaskCancellationReason);
}
}

In-process cleanup, such as releasing a semaphore or disposing a resource, belongs in finally blocks and using statements, which run for both kinds of cancellation.

Suppress the diagnostic for a call which is meant to end at once after task cancellation, such as optional work which the cleanup skips once the task is canceled.