swift icon indicating copy to clipboard operation
swift copied to clipboard

Global actor isolated methods safety hole.

Open CrystDragon opened this issue 9 months ago • 2 comments

Description

I've posted a related feedback on the forum, please check this: https://forums.swift.org/t/a-concurrency-safety-hole-related-to-global-actor-isolated-methods/78803.

Reproduction

class A { 
  func a() async { 
    await b()  // <- ❗️❗️❗️ no compiler errors, not as expected
  }

  @MainActor 
  func b() async { }
}

Expected behavior

There should be diagnostics on the line await b(). We had it with compiler 5.10, but not any more nowadays (both with Swift 6.0 and nightly).

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2) Target: arm64-apple-macosx14.0

Additional information

No response

CrystDragon avatar Mar 26 '25 03:03 CrystDragon

AFAIK, it works as expected. Swift will automatically switch to the appropriate actor. You can test with the following program:

@main
struct Main {
    static func main() async throws {
        await A().a()
    }
}

class A {
    func a() async {
        await b()
    }

    @MainActor
    func b() async {
        _ = {print(Thread.current.isMainThread)}()
    }
}

It always prints true

nikitabobko avatar Apr 06 '25 13:04 nikitabobko

AFAIK, it works as expected. Swift will automatically switch to the appropriate actor. You can test with the following program:

The issue is not about where b is executed, the issue it that it is not safe to send self (an A, which is not Sendable) across domains.

As illustrated in the swift forums post in OP, missing such diagnostics could introduce real world concurrency safety problems.

CrystDragon avatar Apr 07 '25 03:04 CrystDragon

This issue still exists at the moment, hope the fix can make it to Swift 6.2.

CrystDragon avatar Jun 19 '25 09:06 CrystDragon