-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Clarifications on the documented behavior of supervisorScope
W.R.T. its parent.
#3725
Comments
The failure of fun main() = runBlocking<Unit> {
coroutineContext.job.invokeOnCompletion { println("Parent job cancelling with $it") }
try {
supervisorScope {
error("Some error")
}
} catch (e: Exception) {
}
} |
@dkhalanskyjb Got it! Thanks for your response. Still, I feel this piece of doc is not super clear and could be improved, and I wonder why it is in In fact, it seems to be a property of all scoped coroutines (created by scope functions). From private fun cancelParent(cause: Throwable): Boolean {
// Is scoped coroutine -- don't propagate, will be rethrown
if (isScopedCoroutine) return true
// ....
} While on normal hierarchies we can expect parent cancellation: fun main() = runBlocking<Unit> {
val job0 = coroutineContext.job
val job1 = Job(job0)
val job2 = SupervisorJob(job1)
job0.invokeOnCompletion { println("Parent job cancelling with $it") }
job2.completeExceptionally(IllegalStateException("Some error"))
} (Output):
|
Seems like a lot of the information was outdated. Fixes #3725
Seems like a lot of the information was outdated. Fixes #3725
Seems like a lot of the information was outdated. Fixes #3725
Seems like a lot of the information was outdated. Fixes #3725
The KDoc for
supervisorScope
states that:I might be missing something obvious, but how can an exception thrown at
supervisorScope
block not cancel the parentJob
?supervisorScope
block, this exception is rethrown, so it obviously cancels the parent:Output:
cancel
thesupervisorScope
job with an exception, it also fails the parent:Output:
coroutineScope
builder.So I'm wondering what the docs really mean. If documentation is indeed correct, is it possible to provide a small code example in the docs to exemplify the stated behavior (and how it differs from
coroutineScope
)?Please note that this is not regarding
SupervisorJob
behavior regarding children cancellation and failure, which I think is clear. But theSupervisorJob
behavior WRT its parent, and also specifically the behavior of thesupervisorScope
builder WRT exceptions thrown in the scope block (not on children coroutines).The text was updated successfully, but these errors were encountered: