MainActor isolation is broken by conforming to an async protocol #68487
Labels
actor isolation
Feature → concurrency: Actor isolation
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler itself
concurrency
Feature: umbrella label for concurrency language features
conformances
Feature → protocol: protocol conformances
duplicate
Resolution: Duplicates another issue
SILGen
Area → compiler: The SIL generation stage
swift 5.8
unexpected behavior
Bug: Unexpected behavior or incorrect output
Description
When a protocol with async functions is marked with the MainActor annotation, any implementation of this protocol that doesn't mark the functions as async as well when conforming to the protocol, will not execute on the MainActor.
Steps to reproduce
Given the following protocol and implementation:
When the following code is executed:
Both print statements run in a background thread.
But with the following adjustments of either
or
We get the correct result, where the body of the function inside the struct runs in the main thread.
Expected behavior
The expected behavior would be that any code that runs on the
doStufffunction inMyMainActorStructwould run on the main thread, as per the global actor inference rulesEnvironment
Target: arm64-apple-macosx13.0
Additional context
I have posted this question on both stackoverflow and the swift foruns and was asked to create an issue here (there's more information there).
https://forums.swift.org/t/weird-behavior-in-mainactor/67243
https://stackoverflow.com/questions/77089023/why-is-the-following-code-not-running-on-the-main-thread
The text was updated successfully, but these errors were encountered: