Skip to content

Commit

Permalink
Set native handles to null on close in Java wrapper classes (#13818)
Browse files Browse the repository at this point in the history
This updates a few Java wrapper classes that track native resources to zero the native handles when they are closed to make it easier to track down use-after-close errors.

Authors:
  - Jason Lowe (https://github.com/jlowe)

Approvers:
  - Gera Shegalov (https://github.com/gerashegalov)
  - Nghia Truong (https://github.com/ttnghia)

URL: #13818
  • Loading branch information
jlowe authored Aug 4, 2023
1 parent b855872 commit 8370cbe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion java/src/main/java/ai/rapids/cudf/ChunkedPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public PackedColumnMetadata buildMetadata() {

@Override
public void close() {
chunkedPackDelete(nativePtr);
try {
chunkedPackDelete(nativePtr);
} finally {
nativePtr = 0;
}
}

private static native long chunkedPackGetTotalContiguousSize(long nativePtr);
Expand Down
1 change: 1 addition & 0 deletions java/src/main/java/ai/rapids/cudf/ColumnVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public synchronized void close() {
eventHandler.onClosed(this, refCount);
}
if (refCount == 0) {
super.close();
offHeap.clean(false);
} else if (refCount < 0) {
offHeap.logRefCountDebug("double free " + this);
Expand Down
7 changes: 7 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6824,4 +6824,11 @@ public void testColumnViewNullCount() {
assertEquals(vector.getNullCount(), view.getNullCount());
}
}

@Test
public void testUseAfterFree() {
ColumnVector vector = ColumnVector.fromBoxedInts(1, 2, 3);
vector.close();
assertThrows(NullPointerException.class, vector::getDeviceMemorySize);
}
}

0 comments on commit 8370cbe

Please sign in to comment.