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

Add note regarding synchronous communication between too many isolates #6182

Merged
merged 12 commits into from
Nov 8, 2024
Prev Previous commit
Next Next commit
Move text around
  • Loading branch information
aam committed Nov 4, 2024
commit 89162f268b8ceaa089c73981d4de0f8e16571f86
8 changes: 8 additions & 0 deletions src/content/language/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ isolate, it will remain untouched in the main isolate. This is how isolates are
meant to function, and it's important to keep in mind when you’re considering
using isolates.

#### Synchronous blocking communication between isolates

There is a limit to the number of isolates running in parallel:

- The limit is not hardcoded to a particular number, it is calculated based on the Dart VM heap size available to the Dart application, can be considered to be between 8 and 32 depending on the platform.
- This limit doesn't affect asynchronous communction between isolates via messages - you can have hundreds of isolates running and making progress. The isolates are scheduled on the CPU in round-robin fashion and yield to each other often.
- Attempts to do *synchronous* communication between isolates over the limit though may result in a deadlock unless special care is taken (the C code that does synchronous communication would need to leave the current isolate before it blocks and re-enter it before returning to dart, see [`Dart_EnterIsolate`]({{site.repo.dart.sdk}}/blob/c9a8bbd8d6024e419b5e5f26b5131285eb19cc93/runtime/include/dart_api.h#L1254) and [`Dart_ExitIsolate`]({{site.repo.dart.sdk}}/blob/c9a8bbd8d6024e419b5e5f26b5131285eb19cc93/runtime/include/dart_api.h#L1455).
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I would expand on this last bullet a bit. It's full of advanced concepts that aren't discussed in these docs at all, and they're concepts that many Dart developers probably have never thought about in their life.

Given the recent user survey that found folks want more advanced isolate samples, we might even want to add example code of attempting synchronous communication between isolates. That would probably constitute a new issue and separate PR. I'll leave that up to you, @MaryaBelanger, and let me know if you want to discuss.


#### Message types

Messages sent via [`SendPort`][]
Expand Down
6 changes: 0 additions & 6 deletions src/content/language/isolates.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ but here are some more situations where they can be useful:
- Performing I/O, such as communicating with a database.
- Handling a large volume of network requests.

Also note a caveat due to a limit to the number of isolates running in parallel:

- The limit is not hardcoded to a particular number, it is calculated based on the Dart VM heap size available to the Dart application, can be considered to be between 8 and 32 depending on the platform.
- This limit doesn't affect asynchronous communction between isolates via messages - you can have hundreds of isolates running and making progress. The isolates are scheduled on the CPU in round-robin fashion and yield to each other often.
- Attempts to do *synchronous* communication between isolates over the limit though may result in a deadlock unless special care is taken (the C code that does synchronous communication would need to leave the current isolate before it blocks and re-enter it before returning to dart, see [`Dart_EnterIsolate`]({{site.repo.dart.sdk}}/blob/c9a8bbd8d6024e419b5e5f26b5131285eb19cc93/runtime/include/dart_api.h#L1254) and [`Dart_ExitIsolate`]({{site.repo.dart.sdk}}/blob/c9a8bbd8d6024e419b5e5f26b5131285eb19cc93/runtime/include/dart_api.h#L1455).

[Flutter]: {{site.flutter-docs}}/perf/isolates

## Implementing a simple worker isolate
Expand Down