Skip to content

Commit df12d45

Browse files
anidotnetclaude
andcommitted
fix: hasNext() on a RocksDB iterator crashed the JVM after exhaustion
EntrySet, KeySet and ValueSet close the native RocksIterator the first time hasNext() answers false. hasNext() is idempotent by contract, so a second call reached isValid() on the freed handle. BoundedStream's skip loop exhausts the iterator and then the caller asks once more, so any find(..., skipBy(n)) whose skip exceeds the remaining records hit it — ordinary paging, not an edge case. The existing catch (AssertionError) only covered assertions being enabled, which is how the tests ran but not how an application runs. Without -ea the same call is a SIGSEGV in Java_org_rocksdb_RocksIterator_isValid0Jni that takes the process down. hasNext() now returns false once the iterator no longer owns its handle, so it never touches a closed iterator. One test per class asserts isValid() and close() are each reached exactly once across two hasNext() calls; all three fail without the fix. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 1f82946 commit df12d45

7 files changed

Lines changed: 119 additions & 24 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Unreleased
2+
3+
### Issue Fixes
4+
5+
- Fix JVM crash (SIGSEGV) in the RocksDB backend when a query pages past the end of a collection
6+
- `EntrySet`, `KeySet` and `ValueSet` close the native `RocksIterator` the first time `hasNext()` answers `false`, but `hasNext()` is idempotent by contract and a second call reached `isValid()` on the freed handle. `BoundedStream`'s skip loop exhausts the iterator and then the caller asks once more, so any `find(..., skipBy(n))` whose skip exceeds the number of remaining records hit it — an ordinary paging request, not an edge case. The existing `catch (AssertionError)` only covered the case where assertions are enabled (`-ea`), which is how the tests ran but not how an application runs: without assertions the same call is a `SIGSEGV` in `Java_org_rocksdb_RocksIterator_isValid0Jni` that takes the whole process down.
7+
- `hasNext()` now returns `false` when the iterator no longer owns its handle, so it never touches a closed iterator and stays idempotent.
8+
19
## Release 4.4.3 - Jul 20, 2026
210

311
### Issue Fixes

‎nitrite-rocksdb-adapter/src/main/java/org/dizitart/no2/rocksdb/EntrySet.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,20 @@ public EntryIterator() {
4848

4949
@Override
5050
public boolean hasNext() {
51-
try {
52-
boolean result = rawEntryIterator.isValid();
53-
if (!result) {
54-
rawEntryIterator.close();
55-
}
56-
return result;
57-
} catch (AssertionError e) {
51+
// hasNext() is idempotent by contract, and this one closes the native iterator the
52+
// first time it answers false. Asking again then reaches isValid() on a freed handle:
53+
// an AssertionError with -ea, and a SIGSEGV without it. BoundedStream's skip loop
54+
// exhausts the iterator and the caller asks once more, so any find(skipBy(n)) past the
55+
// end of a collection reaches it — with assertions off, which is how an application
56+
// actually runs.
57+
if (!rawEntryIterator.isOwningHandle()) {
5858
return false;
5959
}
60+
boolean result = rawEntryIterator.isValid();
61+
if (!result) {
62+
rawEntryIterator.close();
63+
}
64+
return result;
6065
}
6166

6267
@Override

‎nitrite-rocksdb-adapter/src/main/java/org/dizitart/no2/rocksdb/KeySet.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@ public KeyIterator() {
4444

4545
@Override
4646
public boolean hasNext() {
47-
try {
48-
boolean result = rawEntryIterator.isValid();
49-
if (!result) {
50-
rawEntryIterator.close();
51-
}
52-
return result;
53-
} catch (AssertionError e) {
47+
// hasNext() is idempotent by contract, and this one closes the native iterator the
48+
// first time it answers false. Asking again then reaches isValid() on a freed handle:
49+
// an AssertionError with -ea, and a SIGSEGV without it. BoundedStream's skip loop
50+
// exhausts the iterator and the caller asks once more, so any find(skipBy(n)) past the
51+
// end of a collection reaches it — with assertions off, which is how an application
52+
// actually runs.
53+
if (!rawEntryIterator.isOwningHandle()) {
5454
return false;
5555
}
56+
boolean result = rawEntryIterator.isValid();
57+
if (!result) {
58+
rawEntryIterator.close();
59+
}
60+
return result;
5661
}
5762

5863
@Override

‎nitrite-rocksdb-adapter/src/main/java/org/dizitart/no2/rocksdb/ValueSet.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,20 @@ public ValueIterator() {
4545

4646
@Override
4747
public boolean hasNext() {
48-
try {
49-
boolean result = rawEntryIterator.isValid();
50-
if (!result) {
51-
rawEntryIterator.close();
52-
}
53-
return result;
54-
} catch (AssertionError e) {
48+
// hasNext() is idempotent by contract, and this one closes the native iterator the
49+
// first time it answers false. Asking again then reaches isValid() on a freed handle:
50+
// an AssertionError with -ea, and a SIGSEGV without it. BoundedStream's skip loop
51+
// exhausts the iterator and the caller asks once more, so any find(skipBy(n)) past the
52+
// end of a collection reaches it — with assertions off, which is how an application
53+
// actually runs.
54+
if (!rawEntryIterator.isOwningHandle()) {
5555
return false;
5656
}
57+
boolean result = rawEntryIterator.isValid();
58+
if (!result) {
59+
rawEntryIterator.close();
60+
}
61+
return result;
5762
}
5863

5964
@Override

‎nitrite-rocksdb-adapter/src/test/java/org/dizitart/no2/rocksdb/EntrySetTest.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.rocksdb.RocksDB;
2424
import org.rocksdb.RocksIterator;
2525

26+
import java.util.Iterator;
27+
28+
import static org.junit.Assert.assertFalse;
2629
import static org.mockito.Mockito.*;
2730

2831
public class EntrySetTest {
@@ -45,5 +48,26 @@ public void testIterator2() {
4548
(new EntrySet<>(rocksDB, null, objectFormatter, keyType, Object.class, false)).iterator();
4649
verify(rocksDB).newIterator((ColumnFamilyHandle) any());
4750
}
48-
}
4951

52+
@Test
53+
public void hasNextStaysFalseWithoutTouchingTheClosedIterator() {
54+
// hasNext() is idempotent by contract and this one closes the native iterator the first
55+
// time it answers false, so a second call must not reach it again. On a real RocksIterator
56+
// that second call is isValid() on a freed handle: an AssertionError under -ea, and a
57+
// SIGSEGV without it. BoundedStream's skip loop provokes it on any find(skipBy(n)) past
58+
// the end of a collection.
59+
RocksIterator rawIterator = mock(RocksIterator.class);
60+
RocksDB rocksDB = mock(RocksDB.class);
61+
when(rocksDB.newIterator((ColumnFamilyHandle) any())).thenReturn(rawIterator);
62+
when(rawIterator.isValid()).thenReturn(false);
63+
when(rawIterator.isOwningHandle()).thenReturn(true, false);
64+
KryoObjectFormatter objectFormatter = new KryoObjectFormatter();
65+
66+
Iterator<?> iterator = (new EntrySet<>(rocksDB, null, objectFormatter, Object.class, Object.class, false)).iterator();
67+
assertFalse(iterator.hasNext());
68+
assertFalse(iterator.hasNext());
69+
70+
verify(rawIterator, times(1)).isValid();
71+
verify(rawIterator, times(1)).close();
72+
}
73+
}

‎nitrite-rocksdb-adapter/src/test/java/org/dizitart/no2/rocksdb/KeySetTest.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.rocksdb.RocksDB;
2424
import org.rocksdb.RocksIterator;
2525

26+
import java.util.Iterator;
27+
28+
import static org.junit.Assert.assertFalse;
2629
import static org.mockito.Mockito.*;
2730

2831
public class KeySetTest {
@@ -34,5 +37,26 @@ public void testIterator() {
3437
(new KeySet<>(rocksDB, null, objectFormatter, Object.class)).iterator();
3538
verify(rocksDB).newIterator((ColumnFamilyHandle) any());
3639
}
37-
}
3840

41+
@Test
42+
public void hasNextStaysFalseWithoutTouchingTheClosedIterator() {
43+
// hasNext() is idempotent by contract and this one closes the native iterator the first
44+
// time it answers false, so a second call must not reach it again. On a real RocksIterator
45+
// that second call is isValid() on a freed handle: an AssertionError under -ea, and a
46+
// SIGSEGV without it. BoundedStream's skip loop provokes it on any find(skipBy(n)) past
47+
// the end of a collection.
48+
RocksIterator rawIterator = mock(RocksIterator.class);
49+
RocksDB rocksDB = mock(RocksDB.class);
50+
when(rocksDB.newIterator((ColumnFamilyHandle) any())).thenReturn(rawIterator);
51+
when(rawIterator.isValid()).thenReturn(false);
52+
when(rawIterator.isOwningHandle()).thenReturn(true, false);
53+
KryoObjectFormatter objectFormatter = new KryoObjectFormatter();
54+
55+
Iterator<?> iterator = (new KeySet<>(rocksDB, null, objectFormatter, Object.class)).iterator();
56+
assertFalse(iterator.hasNext());
57+
assertFalse(iterator.hasNext());
58+
59+
verify(rawIterator, times(1)).isValid();
60+
verify(rawIterator, times(1)).close();
61+
}
62+
}

‎nitrite-rocksdb-adapter/src/test/java/org/dizitart/no2/rocksdb/ValueSetTest.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.rocksdb.RocksDB;
2424
import org.rocksdb.RocksIterator;
2525

26+
import java.util.Iterator;
27+
28+
import static org.junit.Assert.assertFalse;
2629
import static org.mockito.Mockito.*;
2730

2831
public class ValueSetTest {
@@ -34,5 +37,26 @@ public void testIterator() {
3437
(new ValueSet<>(rocksDB, null, objectFormatter, Object.class)).iterator();
3538
verify(rocksDB).newIterator((ColumnFamilyHandle) any());
3639
}
37-
}
3840

41+
@Test
42+
public void hasNextStaysFalseWithoutTouchingTheClosedIterator() {
43+
// hasNext() is idempotent by contract and this one closes the native iterator the first
44+
// time it answers false, so a second call must not reach it again. On a real RocksIterator
45+
// that second call is isValid() on a freed handle: an AssertionError under -ea, and a
46+
// SIGSEGV without it. BoundedStream's skip loop provokes it on any find(skipBy(n)) past
47+
// the end of a collection.
48+
RocksIterator rawIterator = mock(RocksIterator.class);
49+
RocksDB rocksDB = mock(RocksDB.class);
50+
when(rocksDB.newIterator((ColumnFamilyHandle) any())).thenReturn(rawIterator);
51+
when(rawIterator.isValid()).thenReturn(false);
52+
when(rawIterator.isOwningHandle()).thenReturn(true, false);
53+
KryoObjectFormatter objectFormatter = new KryoObjectFormatter();
54+
55+
Iterator<?> iterator = (new ValueSet<>(rocksDB, null, objectFormatter, Object.class)).iterator();
56+
assertFalse(iterator.hasNext());
57+
assertFalse(iterator.hasNext());
58+
59+
verify(rawIterator, times(1)).isValid();
60+
verify(rawIterator, times(1)).close();
61+
}
62+
}

0 commit comments

Comments
 (0)