Plain Job doesn't wait for children on join()
See original GitHub issueA Job
that isn’t backed by a coroutine doesn’t seem to wait for its children to complete when join()
is called after cancelling. Given the following tests, I would expect them both to pass but only one does.
private fun runTest(cancelAndJoinBlock: suspend (Job) -> Unit) {
runBlocking {
val parentJob = Job()
val finishedLast = AtomicReference<String>()
launch(parent = parentJob) {
// Something "long-running" that doesn't suspend
Thread.sleep(500)
finishedLast.set("child")
}
// Give the child enough time to have started
delay(200, TimeUnit.MILLISECONDS)
cancelAndJoinBlock(parentJob)
finishedLast.set("join")
// Give the child enough time to have finished
delay(1, TimeUnit.SECONDS)
// The child should have finished before the join on the parent returned
assertEquals("join", finishedLast.get())
}
}
// This one fails
@Test fun parentJobWithCancelAndJoin() {
runTest { parentJob ->
parentJob.cancel()
parentJob.join()
}
}
// This one passes
@Test fun parentJobWithCancelAndJoinChildren() {
runTest { parentJob ->
parentJob.cancelChildren()
parentJob.joinChildren()
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
What is the difference between launch/join and async/await in ...
launch with join is used to wait until the job gets finished. It simply suspends the coroutine calling join() , leaving the current...
Read more >Job and children awaiting in Kotlin Coroutines - Kt. Academy
The first important advantage of a job is that it can be used to wait until the coroutine is completed. For that, we...
Read more >Waiting for Coroutines in Kotlin - DZone
Joining all jobs running inside a coroutine is not a requirement to ensure their completion is waited for. By using the coroutineScope builder...
Read more >Why your multiprocessing Pool is stuck (it's full of sharks!)
Some code that ought to work, but doesn't. Unfortunately, while the Pool class is useful, it's also full of vicious sharks, just waiting...
Read more >Adult Kids Living At Home | Managing Failure To Launch ...
Are you one of the many frustrated parents with their adult kids still living at home? Our experts provide insight on managing failure...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After investigation I’ve found that this is a bug and it will be fixed in the next update. When Job is cancelled it should go into cancelling state while it has children and the
join
should wait until it becomes cancelled (that is, when all its children become cancelled).It does make sense.