Help understanding a Swift 5.10 compiler warning when racing two async tasks to arrange a timeout mechanism using structured concurrency

I have run into that, and here is the answer:

The code in which I got this warning:

@MainActor
struct Wrapper {
    func test() async {
        await withTaskGroup(of: Void.self) { group in
            for _ in 0..<100 {
                group.addTask {
                    print("Hello, world!")
                }
            }
            await group.waitForAll() // warning: passing argument of non-sendable type 'inout TaskGroup<Void>' outside of main actor-isolated context may introduce data races
        }
    }
}

And the mentioned workaround for now is to make test nonisolated.

SE-0420 was partly to address this issue in some of the APIs.

5 Likes