ORLEANS0212: Filter cancellation handlers by IsTaskCancellationRequested
| Property | Value |
|---|---|
| Severity | Warning |
| Category | Usage |
| Code fix | None |
A try block in a durable task body contains durable operations, and one of its catch clauses catches OperationCanceledException, a type it derives from, or a type which derives from it, has no filter which reads DurableTaskContext.IsTaskCancellationRequested, and doesn’t rethrow:
public async DurableTask<OrderResult> BookAsync(string room){ try { await Hold(room).WithId("hold"); await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period"); } catch (OperationCanceledException) { // Also runs when the host cancels the attempt, for example when the activation stops: the handler compensates // for a booking which nobody canceled, and its status change commits while the next attempt confirms the // booking. _status.Value = OrderStatus.Cancelled; return new OrderResult(OrderStatus.Cancelled, DurableTask.Current.TaskCancellationReason); }
return await Confirm(room).WithId("confirm");}Rule description
Section titled “Rule description”A durable task observes two kinds of cancellation through OperationCanceledException:
- Task cancellation is durable: a caller requests it, such as with
CancelAsyncon the task’s handle, and the host records it with the task, together with the point at which each execution first observes it. A replay observes it at that same point, and the body decides the task’s outcome, for example by compensating. - Attempt cancellation stops the current run of the body, for example when the activation stops, or the task is aborted or deleted. The attempt’s later durable operations throw
DurableTaskAttemptCanceledException, and its changes to journaled state commit with the grain’s next journal write. A stopping activation’s task continues in a new attempt.
An unfiltered handler treats both alike. When the host cancels the attempt during the hold period, the handler in the example compensates for a booking which nobody canceled: its state change commits, and an external call which it makes, such as releasing the room in a booking system, takes effect, while the next attempt replays the task and confirms the booking.
DurableTask.Current.IsTaskCancellationRequested is true only for task cancellation, in the canceled attempt and in every later attempt.
For compensation, see Compensate inline when the workflow is canceled.
How to fix violations
Section titled “How to fix violations”Filter the handler. The handler then runs for task cancellation, and the exception of attempt cancellation ends the run. Shield compensating children with Shielded(), so they run although the task’s cancellation was requested:
public async DurableTask<OrderResult> BookOrReleaseAsync(string room){ try { await Hold(room).WithId("hold"); await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period"); } catch (OperationCanceledException) when (DurableTask.Current.IsTaskCancellationRequested) { // Runs for task cancellation only. When the host cancels the attempt, the exception ends it, and the next // attempt replays the task. await Release(room).WithId("release").Shielded(); _status.Value = OrderStatus.Cancelled; return new OrderResult(OrderStatus.Cancelled, DurableTask.Current.TaskCancellationReason); }
return await Confirm(room).WithId("confirm");}Rethrow after compensating. A handler which ends with throw; lets every cancellation end the run, and the task ends canceled after task cancellation:
public async DurableTask<OrderResult> BookOrCancelAsync(string room){ try { await Hold(room).WithId("hold"); await DurableTask.Delay(TimeSpan.FromMinutes(15)).WithId("hold-period"); } catch (OperationCanceledException) { // Rethrows after compensating, so the task ends canceled, and a canceled attempt ends too. await Release(room).WithId("release").Shielded(); throw; }
return await Confirm(room).WithId("confirm");}When to suppress
Section titled “When to suppress”Suppress the diagnostic when the handler’s code is correct for both task cancellation and attempt cancellation, for example a handler which only records a log entry through a replay-safe logger.
