addamsson
04/02/2019, 12:46 PMclass MyClass : CoroutineScope {
override val coroutineContext = Dispatchers.Default + SupervisorJob()
fun doSomething(): Job {
val handler = CoroutineExceptionHandler { _, exception ->
// ...
}
return launch(handler) {
// ...
}
}
}
what I want to achieve is that child jobs (launched with launch
) can fail independently. This works right now but it also works if I remove + SupervisorJob()
The docs say that if I add a CoroutineExceptionHandler
to a coroutine it won't bubble exceptions up which is fine but in SupervisorJob
I can see this:
A failure or cancellation of a child does not cause the supervisor job to fail and does not affect its other children,
so a supervisor can implement a custom policy for handling failures of its children:
A failure of a child job that was created using [launch][CoroutineScope.launch] can be handled via [CoroutineExceptionHandler] in the context.
so it implies that I can only use CoroutineExceptionHandler
in a SupervisorJob
yet it works without one. Did I miss something?ahulyk
04/02/2019, 3:02 PMSupervisorJob
addamsson
04/02/2019, 4:44 PMSupervisorJob
addamsson
04/02/2019, 4:44 PMaddamsson
04/02/2019, 4:44 PMhandler
to launch
and there is a failureaddamsson
04/02/2019, 4:45 PMhandler
is called and the parent (MyClass
here) is not cancelledaddamsson
04/02/2019, 4:45 PMaddamsson
04/02/2019, 4:47 PMSupervisorJob
it says that I need to use it to have a Job
which is not cancelled if a child fails but apparently this is not the caseaddamsson
04/02/2019, 4:48 PMSupervisorJob
Dico
04/03/2019, 6:39 AMSupervisorJob
, it does not work in the presence of a regular Job()
. The documentation isn't explicit about this, but I don't see it stating that CoroutineExceptionHandler
works exclusively with SupervisorJob
.addamsson
04/03/2019, 7:07 AMahulyk
04/03/2019, 7:53 AMCoroutineExceptionHandler
will work with regular Job
, but the key difference is that Job
will fail after error occurs. Please check - https://github.com/Kotlin/kotlinx.coroutines/issues/996#issuecomment-472789627addamsson
04/03/2019, 7:56 AMCoroutineContext
and I launch
child jobs with `CoroutineExceptionHandler`s and the parent job is not failing when a child fails. What you're saying is that an error happens in the parent itself it will get cancelled, but if I add SupervisorJob
to it then it will not, right?ahulyk
04/03/2019, 8:14 AMThe most basic instances of [Job] are created with [launch][CoroutineScope.launch] coroutine builder or with a `Job()` factory function. By default, a failure of any of the job's children leads to an immediately failure
of its parent and cancellation of the rest of its children. This behavior can be customized using [SupervisorJob].
addamsson
04/03/2019, 8:17 AMaddamsson
04/03/2019, 8:17 AM