Tasks Created in an Actor-Isolated Context are Enqueued Deterministically

This is not new behavior, it comes from the fact that Task{} inherits isolation from the enclosing actor context -- so in this case, tasks become MainActor isolated and are enqueued on it and get to execute in order. Because there's no other task escalation happening here either, they do end up executing in the expected order.

The difference between @MainActor and actor here is unfortunately a bit confusing but well defined: you must explicitly close over the isolated self of an actor for the closure and therefore Task to be isolated to that actor. You don't have to do this explicit closing over with global actors.

The sample would work with actor if you did:

Task {
  _ = self
}

and exhibit the same semantics as the global actor code.

2 Likes