Skip to content

Task created from a method with an isolated parameter breaks actor isolation #62568

@dfed

Description

@dfed

Description
A Task created from a method that executes within the isolated context of a passed-in actor should inherit the execution context of the passed-in actor, but does not.

Steps to reproduce
The following code fails to compile:

func execute<ActorType: Actor>(
    on isolatedActor: isolated ActorType,
    task: @Sendable (isolated ActorType) -> Void)
{
    // Compiler correctly allows this task to execute synchronously.
    task(isolatedActor)
    // Start a task that inherits the current execution context (i.e. that of the isolatedActor)
    Task {
        // BUG: compiler insists that we must `await` the `task`, meaning that this Task is not executing in the isolatedActor's execution context
        task(isolatedActor) // Compiler error: Expression is 'async' but is not marked with 'await'
    }
}

Here's a screenshot of the same code with the inlined compiler error for folk who like Xcode's syntax hilighting:

A screenshot of the code above in Xcode

Expected behavior
I would expect the above code to compile without modification.

Per the Task documentation, a not-detached Task "inherits the priority and actor context of the caller", meaning that a Task created in an isolated ActorType method should execute within the isolated context of the passed-in actor.

Environment

  • Swift compiler version info

I was able to reproduce this issue on the following swift compiler versions (I did not try other versions):

swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)
Target: arm64-apple-macosx13.0
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: arm64-apple-macosx13.0
  • Xcode version info

I was able to reproduce this issue on the following Xcode versions (I did not try other versions):

Xcode 14.1
Build version 14B47b
Xcode 14.2
Build version 14C18
  • Deployment target:
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),

Additional context

This bug report was first posted as a question in the Swift forums.

Unlike #62503 and #62505, I have not found a way to force the compiler to execute the Task above in the expected execution context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language featurestriage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions