While experimenting with parallel flow extensions,...
# coroutines
a
While experimenting with parallel flow extensions, I found a not so obvious behavior. This code:
Copy 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
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
⏸️ 1
a
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
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
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
Or use
GlobalScope
.
l
@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
apologies, I'll keep them separate in the future
👍 1