Skip to content

update: instructions for coroutine cancelation #3651

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
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions docs/topics/coroutines-and-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,33 @@ completion or cancel it explicitly, but that won't happen automatically as it wo

### Canceling the loading of contributors

Consider two versions of the `loadContributorsConcurrent()` function. The first uses `coroutineScope` to start all of the
child coroutines, whereas the second uses `GlobalScope`. Compare how both versions behave when you try to cancel
the parent coroutine.
Create two versions of the function that loads the list of contributors. Compare how both versions behave when you try to
cancel the parent coroutine. The first version will use `coroutineScope` to start all of the child coroutines,
whereas the second will use `GlobalScope`.

1. In `Request5Concurrent.kt`, add a 3-second delay to the `loadContributorsConcurrent()` function:

```kotlin
suspend fun loadContributorsConcurrent(
service: GitHubService,
req: RequestData
): List<User> = coroutineScope {
// ...
async {
log("starting loading for ${repo.name}")
delay(3000)
// load repo contributors
}
// ...
}
```

The delay affects all of the coroutines that send requests, so that there's enough time to cancel the loading
after the coroutines are started but before the requests are sent.

1. Copy the implementation of `loadContributorsConcurrent()` from `Request5Concurrent.kt` to
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt`, and then remove the creation of a new `coroutineScope`.
2. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:
2. Create the second version of the loading function: copy the implementation of `loadContributorsConcurrent()` to
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt` and then remove the creation of a new `coroutineScope`.
3. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:

```kotlin
suspend fun loadContributorsNotCancellable(
Expand All @@ -894,25 +914,8 @@ the parent coroutine.
```

* The function now returns the result directly, not as the last expression inside the lambda (lines `#1` and `#3`).
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope (
line `#2`).
3. Add a 3-second delay to all of the coroutines that send requests, so that there's enough time to cancel the loading
after the coroutines are started but before the requests are sent:

```kotlin
suspend fun loadContributorsConcurrent(
service: GitHubService,
req: RequestData
): List<User> = coroutineScope {
// ...
async {
log("starting loading for ${repo.name}")
delay(3000)
// load repo contributors
}
// ...
}
```
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope
(line `#2`).

4. Run the program and choose the _CONCURRENT_ option to load the contributors.
5. Wait until all of the "contributors" coroutines are started, and then click _Cancel_. The log shows no new results,
Expand Down