while working on
SPARK-22713 , I fund (and partially fixed) a scenario in which an iterator is already exhausted but still holds a reference to some resources that can be GCed at this point.
However, these resources can not be GCed because of this reference.
the specific fix applied in
SPARK-22713 was to wrap the iterator with a CompletionIterator that cleans it when exhausted, thing is that it's quite easy to get this wrong by closing over local variables or this reference in the cleanup function itself.
I propose solving this by modifying CompletionIterator to discard references to the wrapped iterator and cleanup function once exhausted.
- a dive into the code showed that most CompletionIterators are eventually used by
implementations, it seems all of them first exhaust the iterator and then perform some kind of post-processing: i.e. merging spills, sorting, writing partitions files and then concatenating them into a single file... bottom line the Iterator may actually be 'sitting' for some time after being exhausted.