Skip to content

[concurrency]: nonisolated(nonsending) can unexpectedly lose actor isolation #86083

@jamieQ

Description

@jamieQ

Description

in the following example, for some reason the isolation assertions fail at runtime when it does not seem like they should.

Reproduction

actor MyActor {
    func test(_ closure: nonisolated(nonsending) () async -> Void) async {
        // uncommenting this somehow fixes the assertion failure
        // self.assertIsolated()
        await closure()
    }
}

nonisolated func test() async {
    let a = MyActor()

    await a.test {
        let iso = #isolation
        print(iso as Any)       // Optional(output.MyActor)
        assert(iso === a)       // ✅
        iso!.assertIsolated()   // 💥
        a.assertIsolated()      // 💥
    }
}

await test()

Expected behavior

the isolation assertions should pass

Environment

Swift version 6.3-dev (LLVM cff341857efbfc7, Swift 956dc0c)
Target: x86_64-unknown-linux-gnu

Additional information

godbolt: https://swift.godbolt.org/z/qPE9dxMq9
similar issue discussed here: #84206

Metadata

Metadata

Assignees

No one assigned

    Labels

    async & awaitFeature → concurrency: asynchronous function aka the async/await patternbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions