Ensuring serial access to a dependency

Actor is reentrant. This means that func endpoint() async -> Result can be called several times. Yes, it is guaranteed that at the same time only one task can be executed on an actor. But after function is suspended, another call can occur.

Image the following:

  1. func endpoint() async -> Result is called. Under the hood it calls the withCheckedThrowingContinuation function, where old callback based function is called. Immediately after that withCheckedThrowingContinuation become suspended until continuation will be resumed. So, as it becomes suspended, actor "become free" and can execute another func.
  2. func endpoint() async -> Result is called one more time, and then one more....
  3. lots of call to func endpoint() async -> Result were made
  4. the first call of func endpoint() async -> Result resumed

There are some separate discussions like Actor Reentrancy - #2 by DevAndArtist

1 Like