Sendable Closures that are also isolated and synchronous?

I wonder if this is related to this bug: Function parameter isolation not respected , although the latter even compiles fine with the nightly build.

The documentation is misleading here. This is only true under specific circumstances. If you don’t capture the isolation directly or the context is not global-actor isolated it currently executes on the GCE: Compiler Explorer

I discussed this in somewhat more detail here: [Pre-Pitch]: updating `with{Checked|Unsafe}Continuation` to support typed throws (and perhaps nonisolated(nonsending)) - #8 by NotTheNHK and here Why is a Task closure defined in an Actor's method nonisolated? . I also want to point out that we have only been making this claim since Swift 5.8. In earlier Swift versions, this was actually true up until SE-0338. The good news is that we can address this with nonisolated(nonsending), and there is ongoing work to do so.

As for the question, you could use a lock or spawn a new task. However, you need to be careful with locks, as they can easily lead to deadlocks, as the documentation warns.

2 Likes