Skip to content

Missing diagnostic when capturing self in deinit Task #72893

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

Closed
rnapier opened this issue Apr 6, 2024 · 0 comments · Fixed by #75224
Closed

Missing diagnostic when capturing self in deinit Task #72893

rnapier opened this issue Apr 6, 2024 · 0 comments · Fixed by #75224
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@rnapier
Copy link

rnapier commented Apr 6, 2024

Description

While there are many valid reasons to pass self as a parameter in deinit, it is never valid to capture it in a Task. This is discussed by @mikeash in #62191 (comment):

But capturing self with a Task or async dispatch is pretty much guaranteed to be wrong, so a warning should be doable for those specific cases.

Doing so should generate a warning.

Reproduction

@MainActor class Service {
    func doThings() {}
}

@MainActor class Controller {
    let service: Service

    init(service: Service) { self.service = service }

    func printMe() { print("Controller lives") }

    deinit {
        print("Controller deinit")
        Task { @MainActor in
            service.doThings()
        }
    }
}

let service = Service()

do {
    let controller = Controller(service: service)
    controller.printMe()
}

Expected behavior

As written, this code will crash, but no diagnostic will be printed in the deinit. If a [service] capture is added to the Task, then this code is fine. It is tricky for developers to understand this subtle difference (self does not even appear in deinit).

An ideal diagnostic would guide them to the proper capture. If no such capture can solve this, then the diagnostic should still indicate the problem.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

@rnapier rnapier added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 6, 2024
@hborla hborla added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels Apr 27, 2024
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants