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

[SR-15789] MainActor isolation is not ensured #58066

Closed
swift-ci opened this issue Jan 29, 2022 · 4 comments
Closed

[SR-15789] MainActor isolation is not ensured #58066

swift-ci opened this issue Jan 29, 2022 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-15789
Radar rdar://problem/88285600
Original Reporter PoltoraIvana (JIRA User)
Type Bug
Status Closed
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @kavon
Priority Medium

md5: 2e59f16869dc5cd2056f09f72289da88

Issue Description:

Having the code:

import UIKit


class ViewController: UIViewController {
  
  private let presenter: Presenter
  
  required init?(coder: NSCoder) {
    let presenter = Presenter()
    self.presenter = presenter
    super.init(coder: coder)
    presenter.view = self
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
   
    print("did load")
    presenter.doSmth()
  }


}


extension ViewController {
  
  @MainActor
  func doSmthOnMain() {
    dispatchPrecondition(condition: .onQueue(.main))
  }
  
}


final class Presenter {
  
  @MainActor weak var view: ViewController?
  
  func doSmth() {
    Task {
      await view?.doSmthOnMain()
    }
  }
  
}

application crashes on the line:

dispatchPrecondition(condition: .onQueue(.main)) 

If you run the project attached, you will see that @mainactor isolation is not ensured and this method is called from cooperative queue.

Interestingly, if I delete @mainactor from the declaration:

  @MainActor weak var view: ViewInternal? 

Method is ensured to be run on main, so no crash.

@swift-ci
Copy link
Collaborator Author

Comment by Ivan Sobolevskiy (JIRA)

I further simplified the case and attached a new one.

Interestingly, if we remove 'final' from 'Presenter' declaration, everything works fine. So it seems because of some optimisation Main actor isolation is not ensured

@DougGregor
Copy link
Member

@swift-ci create

@kavon
Copy link
Contributor

kavon commented Feb 7, 2022

Fixed in Swift 5.6 #41176

@kavon
Copy link
Contributor

kavon commented Feb 7, 2022

Thank you for the report.

This issue was closed.
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. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants