Skip to content

Deprecate runAsyncAndBlock API #35911

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
Feb 12, 2021

Conversation

etcwilde
Copy link
Member

@etcwilde etcwilde commented Feb 11, 2021

I'm throwing a deprecation warning on runAsyncAndBlock to keep people from starting to rely on it in the wild.
We don't want to use runAsyncAndBlock for a couple reasons.

First, because it blocks the thread it's on, which can cause deadlocks. If you try to run @MainActor code after blocking the main thread, you're going to have a bad day.
Second, because it spawns a new thread, it can contribute to thread-explosions, which are also bad news.

For new code, the correct entry-point is

@main struct Main {
  static func main() async {
    // have fun with async...
  }
}

Remember that for single-file tests, you'll need to pass -parse-as-library or it will complain about @main being used with top-level code.

We don't want people using this as a means of jumping from synchronous
code to async code. It blocks the thread that it's running on and spawns
a new thread to run the async code.

This has a couple of drawbacks. First, if it blocks a thread that is
needed by the async code (i.e, calling something on the main actor while
blocking the main thread), it is an immediate deadlock. This is a bad
day for everyone. Second, it can easily lead to thread-explosions.

The proper entry-point for async code is to use something of the
following form:

```
@main
struct Main {
  static func main() async {
    // ...
  }
}
```

This should take care of most new code. There are still places where
folks will need to jump from synchronous code to async code. That still
needs to be designed and implemented, but we don't want people to come
to rely on `runAsyncAndBlock`.
@etcwilde etcwilde requested review from ktoso and rjmccall February 11, 2021 05:43
@etcwilde
Copy link
Member Author

@swift-ci please smoke test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me

@slavapestov
Copy link
Contributor

Will top level code in a script also be async eventually?

@etcwilde
Copy link
Member Author

@swift-ci please smoke test linux platform

@etcwilde
Copy link
Member Author

Will top level code in a script also be async eventually?

Yes, top-level code will have async capabilities eventually.

@etcwilde etcwilde merged commit e813bbe into swiftlang:main Feb 12, 2021
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.

4 participants