Skip to content

ORLEANS0226: Continue durable task bodies on their scheduler

PropertyValue
SeverityError
CategoryUsage
Code fixNone

A durable task body, including a step, awaits with ConfigureAwait(false), or with ConfigureAwaitOptions which omit ContinueOnCapturedContext:

public async DurableTask<decimal> ConvertAsync(string currency, decimal amount)
{
await Approve(amount).WithId("approve");
// The continuation runs on the thread pool: the state change races with the grain's other turns, and commits
// apart from the result.
var rate = await _rates.GetRateAsync(currency, DurableTask.Current.CancellationToken).ConfigureAwait(false);
_lastRate.Value = rate;
return amount * rate;
}

A durable task body runs on its grain activation’s scheduler, which runs one piece of the grain’s code at a time. The scheduler orders the body’s durable operations and its state changes, and the runtime commits the body’s result with the state changes which the body’s tail makes.

An await which continues without its captured context resumes the body on the thread pool:

  • Durable operations, such as DurableTask.RunInline, task starts, and DurableTask.GetVersion, throw InvalidOperationException there, because the execution orders and records its operations on its scheduler.
  • State changes race with the grain’s other turns, including its methods and other executions.
  • The tail’s state changes and the body’s result commit separately. In the example, a crash after _lastRate commits and before the result commits runs the tail again, which fetches a rate and assigns _lastRate again.

ORLEANS0014 reports ConfigureAwait(false) in grain code in general.

For the body’s tail, see Replayed code, recorded operations, and the tail.

Await without ConfigureAwait. The continuation returns to the activation’s scheduler. ConfigureAwait(true) and ConfigureAwaitOptions.ContinueOnCapturedContext continue there too:

public async DurableTask<decimal> ConvertOnSchedulerAsync(string currency, decimal amount)
{
await Approve(amount).WithId("approve");
// The continuation returns to the activation's scheduler, and the state change commits with the result.
var rate = await _rates.GetRateAsync(currency, DurableTask.Current.CancellationToken);
_lastRate.Value = rate;
return amount * rate;
}

Library methods which the body awaits keep their own ConfigureAwait(false): the rule inspects the body’s awaits, and the body’s await of the library method resumes on the scheduler.

Keep the diagnostic enabled: durable operations throw off the scheduler, and state changes there race with the grain’s other work.