Skip to content

ORLEANS0200: Access grain state in recorded effects or after the last durable operation

PropertyValue
SeverityWarning
CategoryUsage
Code fixNone

A durable task body reads or changes grain state at a point from which the body still reaches a durable operation. Grain state is a field or property whose type is, or implements, a journaled collection, IDurableValue<T>, IDurableDictionary<TKey, TValue>, IDurableList<T>, IDurableQueue<T>, IDurableSet<T>, IDurableTaskCompletionSource<T>, or IDurableLog<T>, or persistent state, IPersistentState<TState>. The first method counts a reminder before sending it, and the second branches on a status which its own step changes:

public async DurableTask<int> RemindAsync(string recipient)
{
// A replay runs this line again after the reminder was sent, and counts it twice.
_remindersSent.Value++;
await SendReminderAsync(recipient).WithId("send");
return _remindersSent.Value;
}
public async DurableTask ShipWhenPaidAsync()
{
// A replay reads the status which the "ship" step wrote, takes the other branch, and skips the recorded step.
if (_status.Value == OrderStatus.Paid)
{
await MarkShipped().WithId("ship");
}
}

A replay runs the body from the start, and each recorded operation returns its recorded outcome. Grain state isn’t rebuilt: it holds every committed change, including the changes which later operations made. The code before the body’s last durable operation therefore runs again against newer state:

  • Writes apply again. After a deactivation, the replay of RemindAsync increments _remindersSent a second time, although the reminder was sent once.
  • Reads observe later writes. The replay of ShipWhenPaidAsync reads Shipped, which the recorded ship step wrote, and skips the branch which the first run took. A replay which schedules different operations, or the same operations with different arguments, returns a different result or diverges and stalls the execution.

Three places access grain state once:

  • A DurableTask.RunInline effect runs once, and its state changes commit with its record.
  • A local step, an async DurableTask method of the same grain, commits the state changes of its final code with its recorded result.
  • The body’s synchronous tail, the code after its last durable operation, commits its state changes with the task’s result.

For the rules and the replay model, see Replay-safety rules.

Record the access with DurableTask.RunInline. The effect runs once and returns its outputs, and a replay returns the recorded values. Read state which decides later operations the same way, and append progress to an IDurableLog<T> with DurableTask.RunInline(() => log.Append(entry)), so each entry is appended once:

public async DurableTask<int> RemindOnceAsync(string recipient)
{
// The effect runs once, and its change commits with its record. A replay returns the recorded count.
var sent = DurableTask.RunInline("count", () => ++_remindersSent.Value);
await SendReminderAsync(recipient).WithId("send");
return sent;
}
public async DurableTask ShipWhenPaidOnceAsync()
{
// A replay returns the status which the first run read, so it takes the same branch.
var status = DurableTask.RunInline("read-status", () => _status.Value);
if (status == OrderStatus.Paid)
{
await MarkShipped().WithId("ship");
}
}

Move the change into the synchronous tail. A change with no durable operation after it commits with the task’s result:

public async DurableTask<int> RemindThenCountAsync(string recipient)
{
await SendReminderAsync(recipient).WithId("send");
// No durable operation follows, so this change commits with the task's result and no replay runs it again.
return ++_remindersSent.Value;
}

Make the change in a local step. A step whose body changes state and returns, such as MarkShipped above, commits the change with its recorded result, and a replay returns the result without running the step:

// A local step: its whole body is its final code, so its change commits with its recorded result.
private async DurableTask<OrderStatus> MarkShipped() => _status.Value = OrderStatus.Shipped;

For recorded effects, see Recorded effects, local steps, and calls to other grains.

Suppress the diagnostic for a read whose value never decides a durable operation, its arguments, or a state change, such as a value which only reaches a log message, and for state which no operation changes while the task runs, such as a value which the grain writes once when it’s created.