I add a CoroutineExceptionHandler to my CoroutineScope and on failure get the CoroutineName from the Context to see which coroutine failed. So far so easy.
However when nesting coroutines to use structured concurrency, the name will be the one of the parent coroutine and not the child that failed. But i want to know what child failed. Is it possible to get the Context of the failed coroutine instead of the parent?
I have simplified my problem to this code that runs and demonstrates the issue: https://pl.kotl.in/3ZOpR_eyx
should the above link not work, here is the code:
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import java.util.concurrent.CompletableFuture
import kotlin.coroutines.CoroutineContext
val awaiter = CompletableFuture<Void>()
val exceptionHandler: CoroutineExceptionHandler
get() = CoroutineExceptionHandler { context, t ->
val name = context[CoroutineName.Key]?.name
println("Actual Failure in coroutine $name")
awaiter.complete(null)
}
val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO + exceptionHandler)
val clientName = "client1"
fun main() {
coroutineScope.launch(CoroutineName("HandleClient-$clientName")) {
val task1 = launch(CoroutineName("Task1-$clientName") + exceptionHandler) {
// task that can be cancelled and can throw exceptions
println("Expect Failure in coroutine Task1-$clientName")
throw RuntimeException("I failed")
}
val task2 = launch(CoroutineName("Task2-$clientName") + exceptionHandler) {
// task that can be cancelled and can throw exceptions
delay(1000)
}
// If any child coroutine fails, this will cancel and close the sockets
joinAll(task1, task2)
awaiter.get()
}
}
1