Skip to content

ORLEANS0201: Produce non-deterministic values in recorded effects

PropertyValue
SeverityWarning
CategoryUsage
Code fixNone

A durable task body produces a value which differs between runs at a point from which it still reaches a durable operation. The rule reports wall-clock time (DateTime.Now, DateTime.UtcNow, DateTime.Today, DateTimeOffset.Now, DateTimeOffset.UtcNow, TimeProvider.GetUtcNow, TimeProvider.GetLocalNow, TimeProvider.GetTimestamp, and Stopwatch), random values and identifiers (Guid.NewGuid, Guid.CreateVersion7, Random, and RandomNumberGenerator), and process values (Environment.TickCount, Environment.MachineName, Environment.ProcessId, Environment.CurrentManagedThreadId, Environment.ProcessorCount, and environment variables). Values which only reach a log call are exempt.

The following payment draws an idempotency key and computes a deadline from the system clock:

public async DurableTask<Charge?> PayAsync(decimal amount)
{
// A replay draws another key and computes another deadline.
var idempotencyKey = Guid.NewGuid().ToString("N");
var deadline = DateTimeOffset.UtcNow.AddDays(3);
var (received, decision) = await Approved.TryWaitAsync(deadline).WithId("approval");
if (!received || !decision.Approved)
{
return null;
}
return await DurableTask.Run(ct => payments.ChargeAsync(idempotencyKey, amount, ct)).WithId("charge");
}

A replay runs the body from the start, so it produces another value:

  • Another deadline. The replay computes a deadline from the current clock. The recorded wait keeps its recorded due time, so the value which the body computed disagrees with the wait which it scheduled.
  • Another key. A replay which reaches the charge step before it recorded a result runs the step with a new key, so the payment provider can’t recognize the retry and charges again.
  • Divergence. A value which reaches a durable call’s arguments, a WithId name, or a branch makes the replay schedule a different operation than the one which the first run recorded, and the execution diverges and stalls until code which matches the record replays it.

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

Derive identifiers from the task’s identity, and read logical time. The grain, DurableTask.Current.Id, and DurableTask.Current.Incarnation name the run in every replay, so a key built from them is the same in every replay and attempt, including one which repeats a charge whose result didn’t commit. The incarnation distinguishes a later run of the same task identifier, such as one which starts after the earlier run completed and its history was deleted, so that run charges under another key. DurableTask.Current.UtcNow is the execution’s logical time, which the runtime records, so a replay computes the same deadline. See Derive idempotency keys from the task:

public async DurableTask<Charge?> PayOnceAsync(decimal amount)
{
// The grain, the task's identifier, and its incarnation name this run in every replay, so together they identify
// the charge, and a later run of the same identifier charges under another key.
var idempotencyKey = $"{this.GetGrainId()}/{DurableTask.Current.Id}/{DurableTask.Current.Incarnation:N}/charge";
// Logical time: the runtime records it, so a replay computes the same deadline.
var deadline = DurableTask.Current.UtcNow.AddDays(3);
var (received, decision) = await Approved.TryWaitAsync(deadline).WithId("approval");
if (!received || !decision.Approved)
{
return null;
}
return await DurableTask.Run(ct => payments.ChargeAsync(idempotencyKey, amount, ct)).WithId("charge");
}

Record other values with DurableTask.RunInline. The effect runs once, such as DurableTask.RunInline("reference", () => Guid.NewGuid().ToString("N")), and every replay returns the recorded value. The record commits with the task’s next journal write, together with the grain state which the body changes, so state and branches observe the same value in every run. A value which leaves the grain before that write, such as a key which a step passes to an external service, comes from the task’s identity.

Produce the value inside a step. A DurableTask.Run delegate or a local step records its result, so values which it returns are recorded with it.

Suppress the diagnostic for a value which never reaches a durable operation, its arguments, its name, a branch, or grain state, such as a timestamp which only measures the duration of the current run for a metric.