Skip to content

ORLEANS0211: Wait with durable delays

PropertyValue
SeverityWarning
CategoryUsage
Code fixNone

A durable task body awaits Task.Delay or calls Thread.Sleep at a point from which it still reaches a durable operation:

public async DurableTask ExpireTrialAsync(string customer)
{
// The wait lives in memory: a deactivation loses it, and the replay waits the full 14 days again.
await Task.Delay(TimeSpan.FromDays(14));
await Notify(customer, "Your trial has ended").WithId("expired");
}

Task.Delay waits in the memory of the current run. When the activation deactivates, its silo restarts, or the run fails before the delay ends, the wait is lost, and the replay starts a new 14-day wait from the start of the run, so a trial whose run restarts every few days never expires. Thread.Sleep also blocks the activation’s scheduler, so the grain processes nothing else while it sleeps.

A durable delay records its due time in the execution’s logical time. The grain can deactivate while it waits, the runtime reactivates it when the delay is due, and a replay resumes the recorded delay instead of starting another.

For delays and timeouts, see Combinators and handles.

Wait for a duration with DurableTask.Delay:

public async DurableTask ExpireTrialDurablyAsync(string customer)
{
// The runtime records the due time, and Durable Jobs reactivates the grain when it is due.
await DurableTask.Delay(TimeSpan.FromDays(14)).WithId("trial");
await Notify(customer, "Your trial has ended").WithId("expired");
}

Wait until a time with DurableTask.DelayUntil. Compute the time from arguments, recorded results, or DurableTask.Current.UtcNow, so every replay computes the same due time:

public async DurableTask RemindBeforeExpiryAsync(string customer, DateTimeOffset expiresAt)
{
await DurableTask.DelayUntil(expiresAt.AddDays(-1)).WithId("day-before");
await Notify(customer, "Your trial ends tomorrow").WithId("reminder");
}

Bound work with WaitAsync or WaitUntilAsync instead of racing it with Task.Delay. The timeout is a durable delay which counts from the first run. When it elapses first, the work receives a cancellation request, and a task which ends canceled throws TimeoutException:

public async DurableTask<bool> NotifyWithTimeoutAsync(string customer)
{
try
{
// Instead of racing the work with Task.Delay: a durable timeout which counts from the first run.
await Notify(customer, "Your trial has ended").WaitAsync(TimeSpan.FromMinutes(5)).WithId("notify");
return true;
}
catch (TimeoutException)
{
return false;
}
}

Suppress the diagnostic for a short pause which a replay may repeat, such as a pause before a durable operation which polls a resource that is still starting. A backoff between attempts of an external call inside a DurableTask.Run delegate reaches no durable operation, so the rule doesn’t report it.