Skip to content

[6.0][Concurrency] Diagnose captures of self in a task created in deinit. #75227

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
Jul 15, 2024

Conversation

hborla
Copy link
Member

@hborla hborla commented Jul 13, 2024

  • Explanation: self outliving deinit causes a fatal error at runtime. This is a common mistake to make when creating isolated tasks inside nonisolated deinits to workaround the lack of synchronous isolated deinits in Swift 6. This change adds a new compiler error, staged in as a warning in language modes prior to Swift 6 mode, when you capture self in an @escaping closure that's also sending or @Sendable, which covers both the task creation APIs as well as DispatchQueue.async when used directly inside a deinit.
  • Scope: Only impacts (implicit or explicit) captures of self in an @escaping closure that is also sending or @Sendable inside a deinit.
  • Issues: Missing diagnostic when capturing self in deinit Task #72893
  • Original PRs: [Concurrency] Diagnose captures of self in a task created in deinit. #75224
  • Risk: Low; the new error message is staged in as a warning via .warnUntilSwiftVersion(6)
  • Testing: Added new tests.
  • Reviewers: @ktoso

This is done by diagnosing captures of `self` in escaping `sending` or
`@Sendable` closures inside a deinit, which almost certainly means `self`
will outlive deinit at runtime, which is a fatal error. This is a common
mistake to make when creating isolated tasks inside nonisolated deinits
to workaround the lack of synchrnous isolated deinits in Swift 6.

(cherry picked from commit 18b747c)
@hborla hborla requested a review from a team as a code owner July 13, 2024 20:48
@hborla
Copy link
Member Author

hborla commented Jul 13, 2024

@swift-ci please test

@tbkka
Copy link
Contributor

tbkka commented Jul 15, 2024

Nice!

@hborla hborla merged commit 220f4ce into swiftlang:release/6.0 Jul 15, 2024
5 checks passed
@hborla hborla deleted the 6.0-self-capture-deinit-task branch July 15, 2024 16:38
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.

3 participants