Can actor reentrancy cause stale cached reads across await points in Swift
0 reputation · 27 Apr 2020, 22:36 UTC
Understanding consistency of actor-isolated state across suspension points is relevant for designs that read a value before an await and reuse it after resume within the same actor method.
Swift actors serialize access to isolated mutable state, but actor reentrancy permits other tasks to be scheduled on the same actor while the current task is suspended at an await point. The nonisolated keyword allows specific members to be accessed outside the actor's isolation domain. MainActor provides a global isolation target for main-thread execution.
The uncertainty is whether a value observed before an await can be treated as stable for later logic in the same method, or whether reentrancy can interleave mutations that make a locally cached read stale by the time execution resumes.
Does Swift provide a guarantee that isolated state observed before an await remains unchanged after resume unless mutated by the resuming task itself? Which patterns safely preserve a snapshot of actor state across an await without introducing data races? When does reentrancy behavior differ for MainActor versus a custom actor with serial executor semantics?