Skip to content

[SR-15703] MainActor isolation fails when synthesized async function (from Objective-C method) throws an error #57982

@toddreed

Description

@toddreed
Previous ID SR-15703
Radar rdar://89479707
Original Reporter @toddreed
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment
swift -version
swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
Target: x86_64-apple-macosx12.0
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @kavon
Priority Medium

md5: c52cb58ce3c02bb5270368dfb10304f1

Issue Description:

When calling a synthesized async function (i.e. synthesize from an Objective-C method) from a @mainactor function in Swift, execution resumes on a background thread instead of the main thread when the function throws an error.

Example: in the code the below, the assertion in the catch block fails.

@main
class MainActorBug {

    @MainActor
    static func main() async {
        let adder = Adder()
        do {
            let result = try await adder.add(-5)
            assert(Thread.isMainThread)
            print("The answer is \(result)")
        } catch {
            assert(Thread.isMainThread, "Not on main thread")
            print(error)
        }
    }
}

To reproduce:

unzip MainActorBug.zip
cd MainActorBug
swift run MainActorBug

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions