Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix ParallelIterable memory leak where queue continues to be populated even after iterator close #9402

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ private ParallelIterator(
try (Closeable ignored =
(iterable instanceof Closeable) ? (Closeable) iterable : () -> {}) {
for (T item : iterable) {
// exit manually because `ConcurrentLinkedQueue` can't be
// interrupted
if (closed) {
return;
}
Heltman marked this conversation as resolved.
Show resolved Hide resolved

queue.add(item);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -72,6 +73,66 @@ public CloseableIterator<Integer> iterator() {
.untilAsserted(() -> assertThat(queue).isEmpty());
}

@Test
public void closeMoreDataParallelIteratorWithoutCompleteIteration()
throws IOException, IllegalAccessException, NoSuchFieldException {
ExecutorService executor = Executors.newFixedThreadPool(1);
Iterator<Integer> integerIterator =
Heltman marked this conversation as resolved.
Show resolved Hide resolved
new Iterator<Integer>() {
private int number = 1;

@Override
public boolean hasNext() {
if (number > 1000) {
return false;
}
Heltman marked this conversation as resolved.
Show resolved Hide resolved

number++;
return true;
}

@Override
public Integer next() {
try {
// sleep to control number generate rate
Thread.sleep(10);
} catch (InterruptedException e) {
// Sleep interrupted, we ignore it!
}
return number;
}
};
Iterable<CloseableIterable<Integer>> transform =
Iterables.transform(
Lists.newArrayList(1),
item ->
new CloseableIterable<Integer>() {
@Override
public void close() {}

@Override
public CloseableIterator<Integer> iterator() {
return CloseableIterator.withClose(integerIterator);
}
});

ParallelIterable<Integer> parallelIterable = new ParallelIterable<>(transform, executor);
CloseableIterator<Integer> iterator = parallelIterable.iterator();
Field queueField = iterator.getClass().getDeclaredField("queue");
queueField.setAccessible(true);
ConcurrentLinkedQueue<?> queue = (ConcurrentLinkedQueue<?>) queueField.get(iterator);

assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next()).isNotNull();
Awaitility.await("Queue is populated")
.atMost(5, TimeUnit.SECONDS)
.untilAsserted(() -> queueHasElements(iterator, queue));
iterator.close();
Awaitility.await("Queue is cleared")
.atMost(5, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(queue).as("Queue is not empty after cleaning").isEmpty());
}

private void queueHasElements(CloseableIterator<Integer> iterator, Queue queue) {
assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next()).isNotNull();
Expand Down