azabost
04/17/2023, 8:08 PMcancel()
like here:
@Test
fun myTest() = runTest {
launch {
cancel()
}
}
then what is being cancelled - the launched job or the `runTest`'s scope?
I did some testing and I used a custom scope: CoroutineScope
val job = scope.launch {
// Uncomment one of these to compare behaviour:
cancel() // from: import kotlinx.coroutines.cancel
//coroutineContext.job.cancel()
//scope.cancel()
}
and it turned out only scope.cancel()
cancelled the whole scope
, while all the other statements only cancelled the job
which is weird because cancel()
imported by import kotlinx.coroutines.cancel
is:
/**
* Cancels this scope, including its job and all its children with an optional cancellation [cause].
* A cause can be used to specify an error message or to provide other details on
* a cancellation reason for debugging purposes.
* Throws [IllegalStateException] if the scope does not have a job in it.
*/
public fun CoroutineScope.cancel(cause: CancellationException? = null) {
val job = coroutineContext[Job] ?: error("Scope cannot be cancelled because it does not have a job: $this")
job.cancel(cause)
}
so, according to the documentation, it is supposed to cancel the scope, not the job.
Does anyone know if this is some kind of bug?Joffrey
04/17/2023, 8:27 PMlaunch
provides a CoroutineScope
as receiver to the lambda. That's the one you're cancellingazabost
04/17/2023, 8:27 PMlaunch
was invoked (i.e. CoroutineScope.launch()
)?azabost
04/17/2023, 8:28 PMscope.launch {
cancel()
}
Should I cancel scope
?azabost
04/17/2023, 8:28 PMJoffrey
04/17/2023, 8:29 PMlaunch
creates with a job that has the parent scope's job as parentazabost
04/17/2023, 8:29 PMlaunch
creates a new (child) scope?mkrussel
04/17/2023, 8:29 PMazabost
04/17/2023, 8:30 PMlaunch
doesmkrussel
04/17/2023, 8:31 PMazabost
04/17/2023, 8:31 PMJoffrey
04/17/2023, 8:31 PMCoroutineScope
, and you can definitely launch inside launch. That's the whole basis of structured concurrencyazabost
04/17/2023, 8:32 PMcoroutineScope { ... }
to create a child scope, no?Joffrey
04/17/2023, 8:32 PMwithContext
also creates a child scopemkrussel
04/17/2023, 8:33 PMmkrussel
04/17/2023, 8:34 PMazabost
04/17/2023, 8:34 PMazabost
04/17/2023, 8:35 PMit's a sibling of the outer launch since they were both launched from the same scope
azabost
04/17/2023, 8:36 PMJoffrey
04/17/2023, 8:37 PMscope
variable. The second example uses the child scope of the outer launch
as implicit receiver, which is the child scope I'm talking about here.azabost
04/17/2023, 8:37 PMlaunch
creating a new scope, I thought something must be wrong in the test code I wrote.Joffrey
04/17/2023, 8:38 PMazabost
04/17/2023, 8:38 PMlaunch
KDoc. Otherwise, it's hard to guess what is the lambda's receiver exactly.azabost
04/17/2023, 8:39 PMJoffrey
04/17/2023, 8:56 PM