Skip to content

ORLEANS0225: Await durable tasks instead of polling them in durable task bodies

PropertyValue
SeverityError
CategoryUsage
Code fixNone

A durable task body, including a step, polls a scheduled task with ScheduledTask.IsCompletedAsync, ScheduledTask.GetStatusAsync, or ScheduledTask.GetResponseAsync with PollingOptions:

public async DurableTask<string> PublishAsync(string dataset)
{
var export = await Export(dataset).WithId("export").ScheduleAsync();
// The poll throws NotSupportedException in a durable task body.
while (!await export.IsCompletedAsync())
{
await DurableTask.Delay(TimeSpan.FromMinutes(1)).WithId("poll-interval");
}
return await export;
}

A poll reports the task’s status when the poll runs, and the execution records nothing about it. A replay re-runs the body at another time, when the poll observes another status. A body which branches on the status, such as the loop in the example, then schedules other operations than the run which the execution recorded, and the replay diverges.

The runtime throws NotSupportedException for a poll inside a durable task, so the body fails at its first poll.

An await, or a wait with a durable timeout, records its outcome, and every replay observes the same one. ScheduledTask.GetResponseAsync without PollingOptions waits for the terminal response like an await. Code outside durable task bodies, such as a client, polls.

For handles and timeouts, see Combinators and handles.

Await the task. The body resumes when the task completes, and a replay returns the recorded result. A body which does other work first schedules the task and awaits its handle later:

public async DurableTask<string> PublishWhenExportedAsync(string dataset)
{
// The body resumes when the export completes, and a replay returns its recorded result.
return await Export(dataset).WithId("export");
}

Wait with a durable timeout. WaitAsync with a timeout races the task with a durable delay. When the delay completes first, the task receives a cancellation request, and a task which ends canceled throws TimeoutException. A handle’s WaitAsync with a timeout waits the same way, and ScheduledTask.WhenAny with a scheduled DurableTask.Delay returns whichever completes first:

public async DurableTask<string?> PublishWithinAsync(string dataset)
{
try
{
// A durable delay races the export, and the execution records which completed first.
return await Export(dataset).WaitAsync(TimeSpan.FromHours(1)).WithId("export");
}
catch (TimeoutException)
{
return null;
}
}

Keep the diagnostic enabled: a poll in a durable task body throws NotSupportedException. Polls belong in code outside durable task bodies, such as a client or an ordinary grain method.