Concurrency is broken in Xcode 14 for macOS

The following code works on iOS, but crashes on macOS. I believe this is because of SE-0338 combined with the lack of @_unsafeInheritExecutor in the macOS stdlib included with Xcode 14. In my view, Xcode 14 is unsafe to use for macOS until this is fixed (presumably when Xcode 14.1 includes the macOS 13 SDK with the 5.7 stdlib)

@MainActor
func threadingTest() async {
    dispatchPrecondition(condition: .onQueue(.main))
    
    return await withUnsafeContinuation {
        dispatchPrecondition(condition: .onQueue(.main)) // Crashes here on macOS
        $0.resume()
    }
}
2 Likes

Does anyone else have thoughts on this? The continuation functions not running synchronously on the same actor/executor means that many use cases are broken and unsafe. It means that other Tasks can interleave on an actor after you check a state machine, but before you create and store your continuation.

Other important functions, like withTaskGroup(), are also annotated @_unsafeInheritExecutor in the 5.7 stdlib, and therefore are also broken in Xcode 14 for macOS.

On which versions of iOS does this work? Given that the runtime is included, I'd imagine that only 16 betas would be affected.

This test succeeds on every version of iOS; it is actually not influenced by the dylib in the OS.

The test fails when building for macOS using Xcode 14, because Xcode 14 pairs the 5.7 compiler with the 5.6 stdlib for macOS. The compiler makes some task scheduling decisions (where to insert swift_task_switch), and it’s making the wrong decisions here because it’s reading the 5.6 stdlib, which lacks the required @_unsafeInheritExecutor attribute on withUnsafeContinuation, withTaskGroup, etc

Seems like switching SDK at the last minute after months of beta testing Xcode, basically shipping an untested combination of the Swift compiler and SDK, isn't the best of idea. I guess you already know the solution to your problem: either stick to Xcode 13 or use the Xcode 14.1 beta.

Yeah, the answer is basically just "never use .0 versions of Xcode to target macOS" . They've been consistently broken ever since the macOS releases were moved to October, and barring a significant change in how the period between the Xcode release and the macOS release is handled you should expect them to continue to be broken in the future.

3 Likes