Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dfed opened this issue Dec 14, 2022 · 1 comment · Fixed by #68303
Closed

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

dfed opened this issue Dec 14, 2022 · 1 comment · Fixed by #68303
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features triage needed This issue needs more specific labels

Comments

@dfed
Copy link

dfed commented Dec 14, 2022

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.

@dfed dfed added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Dec 14, 2022
@ktoso ktoso added concurrency Feature: umbrella label for concurrency language features triage needed This issue needs more specific labels and removed triage needed This issue needs more specific labels labels Dec 14, 2022
@ktoso
Copy link
Member

ktoso commented Dec 14, 2022

Thanks for the report!

Linked to rdar://103337121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants