https://kotlinlang.org logo
Title
c

CLOVIS

11/01/2021, 5:36 PM
What happens to a scope when the system doesn't have a reference to it? As in: • create a class that has a private CoroutineScope attribute • launch something in that scope • do not keep any references to the class What is expected to happen? Will the scope be garbage-collected? If so, what happens to the launched coroutine?
o

Oliver.O

11/01/2021, 5:39 PM
Given that
CoroutineScope
is just an encapsulation of `coroutineContext`: The coroutine will retain a reference to that coroutine context (see
AbstractCoroutine
). So it won't be garbage collected and the coroutine will live on until it completes (normally or cancelled).
Ah, and I forgot: The coroutines machinery also needs to retain a reference to the coroutine as it might need to be scheduled at any time.
c

CLOVIS

11/01/2021, 6:06 PM
I see. So it becomes impossible to cancel the coroutine, and it finishes working normally. When it's done, everything is garbage-collected?
o

Oliver.O

11/01/2021, 6:17 PM
That's what I would expect. Usually one would prefer a coroutine context with some lifecycle support around it: When the respective lifecycle ends, its coroutines would be cancelled.
e

ephemient

11/01/2021, 8:48 PM
I believe it's possible that if an unreferenced scope only contains suspended coroutines, the whole thing may get garbage collected without going through the usual cancellation. but I'm not having much luck figuring out how to induce such a situation
o

Oliver.O

11/01/2021, 9:07 PM
For that to be the case, all remaining references would have to be "inside" ones (coroutines referencing the scope and vice versa), but none of the suspended coroutines could be referenced from the outside, right? It would be strange if this could be possible without coroutines entering their completed state. If they are not completed, there must be something referencing them, which could trigger their continuation. But without diving deeply into the countless mechanisms in the coroutines machinery, I can only hypothesize, not prove it.
e

ephemient

11/01/2021, 9:25 PM
suspendCoroutine
can suspend a coroutine in a way that doesn't have outside references to its continuation to resume
or perhaps all outside references are garbage collected
at this point… I don't know how to observe if that happens lol
o

Oliver.O

11/01/2021, 10:38 PM
Hopefully it won't be observable for those who stay away from tinkering with the mechanisms at the level of
suspendCoroutine
. The library's user-facing API should ensure that there is always something watching a suspended continuation, be it a channel, a timer, a flow, whatever. Anything else is hopefully considered a bug and would get fixed. 😇
n

Nick Allen

11/01/2021, 10:40 PM
I've actually done this before, launching simple coroutines that die with the object instead of using an explicit scope. I eventually changed it since it seemed easy to trip up on later (GC'd coroutines never run finally blocks).
Just a loop that reads from a channel. If the channel is GC'd, then the coroutine will never be resumed and it'll be GC'd too.