https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

julian

07/13/2021, 9:23 PM
I've observed that when an async-built-coroutine is nested within a launch-built-coroutine, the async-coroutine's job isn't added to the launch-coroutine's job's children. Is this always true?
Copy code
launch { 
    async { ... }
}
s

streetsofboston

07/13/2021, 10:46 PM
According to the structured concurrency, the coroutine of
launch
should not finish until the coroutine of
async
has finished. The job of the async should be a child of launch.
j

julian

07/13/2021, 11:09 PM
Thanks, @streetsofboston.  That's surprising. So, I can't explain why this (Kotest) test passes:
I used to believe that the parent-child relationships on which structured concurrency depends is detectable by examining the
children
of jobs. But, in recent experiments, it seems to be that structured concurrency can be respected without such relationships being evident in the children of the parent jobs involved. The test above is an example of this.
s

streetsofboston

07/13/2021, 11:38 PM
Maybe it's empty because
launch
has not yet started at all when you assert your test. If your test is asserted a bit later, the coroutine of the
launch
may have been started and its job may have a child-job
Then again,
advanceUntilIdle
should have made sure that the coroutine of
launch
should at least have started.... But I'm not sure about the exact behavior of
advanceUntilIdle
...
j

julian

07/13/2021, 11:46 PM
I can try the test again without Kotest.
Or... I can assert that both jobs are complete before checking if the outer is empty. Yeah, I'll try that instead.
Yup. Same result. Both jobs have completed.
s

streetsofboston

07/14/2021, 2:52 AM
Hmmmmm...... Maybe a parent job removes a child-job from its list when that child-job has finished. I'm not sure whether the behavior of a job's list of child-jobs is defined/documented anywhere.
j

julian

07/14/2021, 6:58 AM
I can investigate that by checking at various points what the state of job children is.
6 Views