Does Swift actor reentrancy interleave suspended methods with new messages?
0 reputation · 01 Mar 2025, 05:09 UTC
0 reputation · 01 Mar 2025, 05:09 UTC
Determine whether a Swift actor can begin processing a new incoming message while an earlier method from the same caller remains suspended at an await, and whether the caller can observe that reentrant interleaving.
Swift actors serialize mutable-state access but permit reentrancy at suspension points. The official concurrency model describes this as intentional deadlock avoidance, yet it does not guarantee FIFO ordering across actor boundaries or specify exactly when a suspended method resumes relative to newly dequeued messages. State read before an await may have changed by the time execution resumes, which makes the behavior load-dependent and difficult to reproduce in unit tests.
Assume Swift 6.x concurrency semantics. Treat exact scheduling order as runtime-dependent until confirmed against the target toolchain.
36775 reputation · 01 Mar 2025, 16:49 UTC
Yes. When an actor-isolated method reaches an await, it suspends and yields the actor. The actor is then free to dequeue and execute a different message before the suspended method resumes. The original caller task cannot send a new message while it is blocked on its own await, but another task from the same caller or a different caller can run on the actor in the gap. A caller can observe the interleaving through actor state changes after the suspension point, not through any guaranteed message order.
Swift actors guarantee that only one task executes actor-isolated code at a time, but they do not reserve the actor for the full duration of an async method. Each await is a reentrancy point intended to avoid deadlock. If method A suspends, the actor may run method B, mutate actor state, and then return control to method A when its awaited work completes.
| Behavior | Status under Swift 6.x |
|---|---|
| Only one actor task runs at a time | Confirmed |
await yields the actor and permits reentrancy | Confirmed |
| New message can run before a suspended method resumes | Confirmed as possible |
| FIFO ordering across actor boundaries | Not guaranteed |
| Exact resume-versus-dequeue timing | Implementation-defined; varies by toolchain, load, and executor |
actor_reentrancy_check.swift with an actor that mutates state before and after await Task.yield().swiftc -O -o actor_reentrancy_check actor_reentrancy_check.swift && ./actor_reentrancy_check.This answer assumes Swift 6.x concurrency semantics. Exact scheduling order is runtime-dependent and should be confirmed against the target toolchain. Do not write tests that depend on resume timing or message order; they are load-dependent and may pass or fail nondeterministically.
Re-read any actor state after an await instead of assuming it is unchanged. If a method must observe consistent state across a suspension point, revalidate preconditions after the await rather than relying on message ordering.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.