Apache Lucene open-source search software
MMapDirectory with MemorySegment: Confirm that scope/session is no longer alive before throwing AlreadyClosedException (#12707)
Uwe Schindlerbbf197fdc282
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 8e3fa77ecd0..c3b39e0e378 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -183,8 +183,8 @@ Improvements* GITHUB#12586: Remove over-counting of deleted terms. (Guo Feng)-* GITHUB#12705: Improve handling of NullPointerException in MMapDirectory's IndexInputs.- (Uwe Schindler, Michael Sokolov)+* GITHUB#12705, GITHUB#12705: Improve handling of NullPointerException and IllegalStateException+ in MMapDirectory's IndexInputs. (Uwe Schindler, Michael Sokolov)Optimizations---------------------diff --git a/lucene/core/src/java19/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java19/org/apache/lucene/store/MemorySegmentIndexInput.javaindex 2c2c65a7b0c..ee102a12d4e 100644--- a/lucene/core/src/java19/org/apache/lucene/store/MemorySegmentIndexInput.java+++ b/lucene/core/src/java19/org/apache/lucene/store/MemorySegmentIndexInput.java@@ -105,9 +105,11 @@ abstract class MemorySegmentIndexInput extends IndexInput implements RandomAccesAlreadyClosedException alreadyClosed(RuntimeException e) {// we use NPE to signal if this input is closed (to not have checks everywhere). If NPE happens,// we check the "is closed" condition explicitly by checking that our "curSegment" is null.- // if it is an IllegalStateException, it can only come from MemorySegment API (other thread has- // closed input)- if (this.curSegment == null || e instanceof IllegalStateException) {+ if (this.curSegment == null) {+ return new AlreadyClosedException("Already closed: " + this);+ }+ // we also check if the session of all segments is still alive:+ if (Arrays.stream(segments).allMatch(s -> s.session().isAlive()) == false) {return new AlreadyClosedException("Already closed: " + this);}// otherwise rethrow unmodified NPE/ISE (as it possibly a bug with passing a null parameter todiff --git a/lucene/core/src/java20/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java20/org/apache/lucene/store/MemorySegmentIndexInput.javaindex a8685ec2249..627c5edbd5a 100644--- a/lucene/core/src/java20/org/apache/lucene/store/MemorySegmentIndexInput.java+++ b/lucene/core/src/java20/org/apache/lucene/store/MemorySegmentIndexInput.java@@ -103,9 +103,11 @@ abstract class MemorySegmentIndexInput extends IndexInput implements RandomAccesAlreadyClosedException alreadyClosed(RuntimeException e) {// we use NPE to signal if this input is closed (to not have checks everywhere). If NPE happens,// we check the "is closed" condition explicitly by checking that our "curSegment" is null.- // if it is an IllegalStateException, it can only come from MemorySegment API (other thread has- // closed input)- if (this.curSegment == null || e instanceof IllegalStateException) {+ if (this.curSegment == null) {+ return new AlreadyClosedException("Already closed: " + this);+ }+ // we also check if the scope of all segments is still alive:+ if (Arrays.stream(segments).allMatch(s -> s.scope().isAlive()) == false) {return new AlreadyClosedException("Already closed: " + this);}// otherwise rethrow unmodified NPE/ISE (as it possibly a bug with passing a null parameter todiff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.javaindex a8685ec2249..627c5edbd5a 100644--- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java+++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java@@ -103,9 +103,11 @@ abstract class MemorySegmentIndexInput extends IndexInput implements RandomAccesAlreadyClosedException alreadyClosed(RuntimeException e) {// we use NPE to signal if this input is closed (to not have checks everywhere). If NPE happens,// we check the "is closed" condition explicitly by checking that our "curSegment" is null.- // if it is an IllegalStateException, it can only come from MemorySegment API (other thread has- // closed input)- if (this.curSegment == null || e instanceof IllegalStateException) {+ if (this.curSegment == null) {+ return new AlreadyClosedException("Already closed: " + this);+ }+ // we also check if the scope of all segments is still alive:+ if (Arrays.stream(segments).allMatch(s -> s.scope().isAlive()) == false) {return new AlreadyClosedException("Already closed: " + this);}// otherwise rethrow unmodified NPE/ISE (as it possibly a bug with passing a null parameter to