kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Unstarted lazy job prevents parent from completion

Open cy6erGn0m opened this issue 6 years ago • 2 comments
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.

cy6erGn0m avatar Sep 17 '19 13:09 cy6erGn0m

Also, remember about #1544 as well since they are connected.

cy6erGn0m avatar Sep 17 '19 13:09 cy6erGn0m

See: Customizable coroutine behaviour on scope termination #1065

fvasco avatar Sep 17 '19 14:09 fvasco