Skip to content
Closed
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 @@ -20,7 +20,6 @@
import java.io.IOException;
import java.util.function.Supplier;

import scala.collection.AbstractIterator;
import scala.collection.Iterator;
import scala.math.Ordering;

Expand Down Expand Up @@ -168,39 +167,40 @@ public void cleanupResources() {
sorter.cleanupResources();
}

public Iterator<UnsafeRow> sort() throws IOException {
public Iterator<InternalRow> sort() throws IOException {
try {
final UnsafeSorterIterator sortedIterator = sorter.getSortedIterator();
if (!sortedIterator.hasNext()) {
// Since we won't ever call next() on an empty iterator, we need to clean up resources
// here in order to prevent memory leaks.
cleanupResources();
}
return new AbstractIterator<UnsafeRow>() {
return new RowIterator() {

private final int numFields = schema.length();
private UnsafeRow row = new UnsafeRow(numFields);

@Override
public boolean hasNext() {
return !isReleased && sortedIterator.hasNext();
}

@Override
public UnsafeRow next() {
public boolean advanceNext() {
try {
sortedIterator.loadNext();
row.pointTo(
sortedIterator.getBaseObject(),
sortedIterator.getBaseOffset(),
sortedIterator.getRecordLength());
if (!hasNext()) {
UnsafeRow copy = row.copy(); // so that we don't have dangling pointers to freed page
row = null; // so that we don't keep references to the base object
cleanupResources();
return copy;
if (!isReleased && sortedIterator.hasNext()) {
sortedIterator.loadNext();
row.pointTo(
sortedIterator.getBaseObject(),
sortedIterator.getBaseOffset(),
sortedIterator.getRecordLength());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indents

// Here is the initial bug fix in SPARK-9364: the bug fix of use-after-free bug
// when returning the last row from an iterator. For example, in
// [[GroupedIterator]], we still use the last row after traversing the iterator
// in `fetchNextGroupIterator`
if (!sortedIterator.hasNext()) {
row = row.copy(); // so that we don't have dangling pointers to freed page
cleanupResources();
}
return true;
} else {
return row;
row = null; // so that we don't keep references to the base object
return false;
}
} catch (IOException e) {
cleanupResources();
Expand All @@ -210,14 +210,18 @@ public UnsafeRow next() {
}
throw new RuntimeException("Exception should have been re-thrown in next()");
}
};

@Override
public UnsafeRow getRow() { return row; }

}.toScala();
} catch (IOException e) {
cleanupResources();
throw e;
}
}

public Iterator<UnsafeRow> sort(Iterator<UnsafeRow> inputIterator) throws IOException {
public Iterator<InternalRow> sort(Iterator<UnsafeRow> inputIterator) throws IOException {
while (inputIterator.hasNext()) {
insertRow(inputIterator.next());
}
Expand Down