Skip to content

[Concurrency] Don't allow erasing global actor isolation when the function value crosses an isolation boundary. #72607

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

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

hborla
Copy link
Member

@hborla hborla commented Mar 27, 2024

The actor isolation checker allows erasing global actor isolation in function conversions when the conversion happens in the same isolation domain and the destination type is not Sendable under the assumption that the function value will never cross an isolation boundary. This is an important rule, because it allows calling synchronous higher order function APIs with global actor isolated function values as long as you're calling from a global actor isolated context. However, the actor isolation checker was allowing the conversion in cases where the function value crosses an isolation boundary:

func crossIsolationBoundary(_ closure: () -> Void) async { closure() }

@MainActor func test() async {
  let closure = { @MainActor @Sendable in
    MainActor.assertIsolated()
  }

  await crossIsolationBoundary(closure)
}

This PR diagnoses the function conversion that loses actor isolation if the function conversion is an argument to a call that crosses an isolation boundary.

We still need to fix this case under region isolation:

func crossIsolationBoundary(_ closure: () -> Void) async { closure() }

@MainActor func test() async {
  let closure = { @MainActor @Sendable in
    MainActor.assertIsolated()
  }

  let erased: () -> Void = closure

  await crossIsolationBoundary(erased)
}

Currently, the above code is "fine" because the function conversion itself isn't crossing an isolation boundary, and from the perspective of region analysis, erased is in a disconnected region. I think the way to solve this is to merge erased to the main actor region due to the function conversion that erases main actor isolation.

Resolves: rdar://118206647

@hborla
Copy link
Member Author

hborla commented Mar 27, 2024

@swift-ci please smoke test

@hborla hborla force-pushed the unsafe-isolation-erasure branch from f841fa0 to 6154823 Compare March 27, 2024 01:53
@hborla
Copy link
Member Author

hborla commented Mar 27, 2024

@swift-ci please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants