Skip to content

ORLEANS0223: Assign new values to grain state instead of changing them in place

PropertyValue
SeverityWarning
CategoryUsage
Code fixNone

Durable task code changes an object which grain state holds, in place: it assigns, increments, or decrements a member or an element of the object, or calls a method which changes a collection, such as Add or Clear. The object is reached through IDurableValue<T>.Value, IPersistentState<T>.State, or an entry of a durable dictionary or list, directly or through a local which holds it:

[GenerateSerializer]
public sealed class Wishlist
{
[Id(0)]
public List<string> Items { get; set; } = [];
}
public sealed class WishlistGrain : DurableGrain
{
private readonly IDurableValue<Wishlist> _wishlist;
public WishlistGrain() => _wishlist = StateManager.GetOrAddValue<Wishlist>("wishlist");
public async DurableTask AddAsync(string item)
{
await CheckStock(item).WithId("check-stock");
// Changes the list which the value holds: no journal record holds the new item.
_wishlist.Value!.Items.Add(item);
}
private static async DurableTask<bool> CheckStock(string item) => item.Length > 0;
}

Durable values and collections record each change which passes through their APIs, such as an assignment to IDurableValue<T>.Value or to an entry of a durable dictionary. The change commits with the grain’s next journal write.

A change made in place to an object which these operation-tracked values or collection entries hold bypasses the API. The object in memory holds the change, and no record holds it until another change writes the whole value. In the example, the task completes and its result commits while the journal holds the wishlist without the new item. An activation which recovers the grain from the journal loses the item.

Journal-backed IPersistentState<T> records its whole state on each journal write, including in-place changes to its serialized members and collections. The rule also recommends immutable values and assignment for this state in durable task code.

Value types, records whose members are init-only, and immutable collections change through assignment, which the API records, so the rule reports none of them.

For replay-safe state, see Replay-safety rules.

Store immutable values, and assign a new value. A record with an immutable collection returns a changed copy, and the assignment records it:

[GenerateSerializer, Immutable]
public sealed record SavedItems(ImmutableList<string> Items)
{
public static SavedItems Empty { get; } = new([]);
}
public sealed class SavedItemsGrain : DurableGrain
{
private readonly IDurableValue<SavedItems> _saved;
public SavedItemsGrain() => _saved = StateManager.GetOrAddValue<SavedItems>("saved-items");
public async DurableTask AddAsync(string item)
{
await CheckStock(item).WithId("check-stock");
// The assignment passes through the value's API, which records the new list.
var saved = _saved.Value ?? SavedItems.Empty;
_saved.Value = saved with { Items = saved.Items.Add(item) };
}
private static async DurableTask<bool> CheckStock(string item) => item.Length > 0;
}

Suppress the diagnostic for a member which the state holds in memory only, such as a cache which the serializer skips.