Skip to content

Commit 203ab5b

Browse files
authored
Explain potential memory leak that nulling BufferedChannelIterator.continuation out prevents (#3837)
1 parent 7ef545e commit 203ab5b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

kotlinx-coroutines-core/common/src/channels/BufferedChannel.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,11 @@ internal open class BufferedChannel<E>(
16901690
}
16911691

16921692
fun tryResumeHasNextOnClosedChannel() {
1693-
// Read the current continuation and clean
1694-
// the corresponding field to avoid memory leaks.
1693+
/*
1694+
* Read the current continuation of the suspended `hasNext()` call and clean the corresponding field to avoid memory leaks.
1695+
* While this nulling out is unnecessary, it eliminates memory leaks (through the continuation)
1696+
* if the channel iterator accidentally remains GC-reachable after the channel is closed.
1697+
*/
16951698
val cont = this.continuation!!
16961699
this.continuation = null
16971700
// Update the `hasNext()` internal result and inform

0 commit comments

Comments
 (0)