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

David W

09/17/2020, 4:27 PM
Am I correct in thinking that
Copy code
launch {
  singletonReceiveChannel.consumeEach { printLine(it) }
}
will produce a memory leak due to
consumeEach
never getting canceled, even if `launch`'s CoroutineScope ends?
z

Zach Klippenstein (he/him) [MOD]

09/17/2020, 5:29 PM
If the launch's scope or job is cancelled, consumeEach should be cancelled as well.
d

David W

09/17/2020, 5:35 PM
Oh really? That's perfect, then
I was thinking that since
consumeEach
stays suspended, it wouldn't be canceled
b

bdawg.io

09/17/2020, 6:14 PM
suspend
generally means that function becomes part of the caller's scope/structured concurrency (in this case, the
launch
's scope)
🙏 1
z

Zach Klippenstein (he/him) [MOD]

09/17/2020, 6:23 PM
Cancellation primarily means suspended coroutines are cancelled. Running code can’t actually be cancelled unless it cooperatively checks for cancellation.
1
d

David W

09/17/2020, 6:27 PM
ahh, that's what i was hung up on then
4 Views