kotlinx.coroutines
kotlinx.coroutines copied to clipboard
Unstarted lazy job prevents parent from completion
trafficstars
The following example hangs forever:
class LazyChildTest : CoroutineScope {
@get:Rule
val timeout: CoroutinesTimeout = CoroutinesTimeout.seconds(1)
private val parent = Job()
override val coroutineContext: CoroutineContext = parent
@BeforeTest
fun start() {
launch(CoroutineName("lazy-job"), start = CoroutineStart.LAZY) {
}
}
@AfterTest
fun cleanup() {
parent.cancel()
}
@Test
fun test(): Unit = runBlocking(CoroutineName("blocking")) {
parent.complete()
parent.join()
}
}
The reason is that the lazy job didn't start so the parent will never complete. The first idea is to force a recursive children startup at parent completion.
Also, remember about #1544 as well since they are connected.
See: Customizable coroutine behaviour on scope termination #1065