What happens to a `Channel` when it becomes unreac...
# coroutines
c
What happens to a
Channel
when it becomes unreachable? Does it get cleanly GC'd?
j
Yeah. It's just a fancy list.
➕ 3
🙏 1
y
Can confirm, I've written a rudimentary channel implementation before, they're just fancy lists (of "waiters" i.e. coroutines that are waiting to send/receiver a value, and optionally a buffer of values). Kotlinx coroutines channels are way more involved (they wrote a paper about it IIRC), but that's just performance things that don't affect it's GC-ability
j
Roman has some good talks from the 2018ish era about building the lock free algorithms behind it
y

2017 video pt 1▾

2017 video pt 2▾

2022 paper
🙏 2
c
Then, if there is a reader that exists and no more writers, the channel can't be GC'd and the reader is stuck forever?
y
Assuming that no other actively-running coroutine has access to the channel, then no, the channel will be GCed The channel keeps a reference to its waiters in the form of the continuation it stores. The continuation is effectively a lambda. Coroutines don't really "exist" materially beyond their continuation. In other words, the reader's continuation (which holds all of its current state) will only be referred to by the channel, so they'll have a cycle and thus can both be GCed
c
So said otherwise, if the only readers are suspended on the channel, they don't block the GC of the channel?
y
Yep! Continuations usually have a singular reference to them (because they're single use), so the suspended readers are being held by the channel, and nowhere else. All the coroutine's state is stored in its continuation
🙏 1
c
I have a CI job that hangs forever, and I'm trying to figure out why. At least now I know it can't be a reader stuck in a never-closing channel