https://kotlinlang.org logo
Title
a

altavir

04/28/2019, 6:47 AM
While experimenting with parallel flow extensions, I found a not so obvious behavior. This code:
val deferred = coroutineScope {
                async(start = CoroutineStart.LAZY) { println("async") }
            }
            deferred.await()
causes a infinite block. I understand that it is caused by interference of lazy deferred with structured concurrency, but it significantly reduces application for lazy deferred. Is it intended to be this way?
👆 1
o

octylFractal

04/28/2019, 6:49 AM
what behavior were you expecting? this certainly looks intended to me
- coroutineScope blocks until all child coroutines are complete - LAZY does not start until
await
is called - therefore, it blocks
😒uspend: 1
a

altavir

04/28/2019, 6:53 AM
Yes, I understand that. But in this case I can't create a lazy coroutine and leak it outside the scope. Also this block is actually quite hard to find.
1
o

octylFractal

04/28/2019, 6:55 AM
leak it outside the scope
isn't this antithetical to structured concurrency? if you want to "leak" something, you can't tie it as a child -- you must start it using something like
GlobalScope
, to make it owned by something else, so it may persist outside of the scope.
a

altavir

04/28/2019, 7:01 AM
I see, but it still kills the whole idea for lazy coroutines. From logical point of view, it seems that lazy coroutine should run in the scope that started it, not the one that created it. Probably it would be good to create a separate class for lazy coroutines with suspend
CoroutineScope.start
and deprecate
CoroutineStart.LAZY
.
d

Dominaezzz

04/28/2019, 8:48 AM
Or use
GlobalScope
.
l

louiscad

04/28/2019, 10:31 AM
@octylFractal Please, stop using the verb "block" when the "suspend" verb should be used instead. This is bringing ambiguity, and not everyone here is knowledgeable about difference between blocking and suspending as beginners join regularly too.
o

octylFractal

04/28/2019, 6:07 PM
apologies, I'll keep them separate in the future
👍 1