Skip to content

Clarification on CoroutineScope doc #2891

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
KelvinJin opened this issue Aug 23, 2021 · 0 comments
Closed

Clarification on CoroutineScope doc #2891

KelvinJin opened this issue Aug 23, 2021 · 0 comments
Labels
docs KDoc and API reference

Comments

@KelvinJin
Copy link

KelvinJin commented Aug 23, 2021

So I've noticed that the doc says

If the given [context] does not contain a [Job] element, then a default Job() is created.
This way, cancellation or failure of any child coroutine in this scope cancels all the other children,
just like inside [coroutineScope] block.

The key confusion here is that cancellation of any child coroutine in this scope cancels all the other children. From my testing, cancellation of a child of a Job() doesn't really cancels all other children. Maybe I'm missing anything but the testing code is:

val scope = CoroutineScope(Dispatchers.Default)
// Or more explicitly val scope = CoroutineScope(Dispatchers.Default + Job())

val job1 = scope.launch {
    delay(99999)
}
val job2 = scope.launch {
    delay(500)
}

job2.cancel()

println("job1 state: ${job1.isCancelled}") // false
println("job2 state: ${job2.isCancelled}") // true
@qwwdfsad qwwdfsad added the docs KDoc and API reference label Aug 23, 2021
qwwdfsad added a commit that referenced this issue Aug 23, 2021
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this issue Sep 14, 2022
* Update CoroutineScope() factory documentation

Fixes Kotlin#2891
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

2 participants