Apache Lucene open-source search software
Fix infinite loop when RefCountedSharedArena's underlying Arena#close fails due to concurrent usage of segments (#15112)
Uwe Schindler278e9674c71d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex caa9eaf3850..43dc2c7518f 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -306,6 +306,9 @@ Bug Fixesresulted in query branches being wrongly ignored if the QueryIndex wasbuilt by a different JVM. (Luke Kot-Zaniewski)+* GITHUB#15106: Fix infinite loop when RefCountedSharedArena's underlying+ Arena#close fails due to concurrent usage of segments. (Uwe Schindler)+Build---------------------* Upgrade forbiddenapis to version 3.9. (Uwe Schindler)diff --git a/lucene/core/src/java/org/apache/lucene/store/RefCountedSharedArena.java b/lucene/core/src/java/org/apache/lucene/store/RefCountedSharedArena.javaindex 5f5fe98d4c0..8fa4882dcf5 100644--- a/lucene/core/src/java/org/apache/lucene/store/RefCountedSharedArena.java+++ b/lucene/core/src/java/org/apache/lucene/store/RefCountedSharedArena.java@@ -50,7 +50,11 @@ final class RefCountedSharedArena implements Arena {private static final int ACQUIRE_DECREMENT = REMAINING_UNIT - 1; // 0xffffprivate final String segmentName;++ // onClose could be called multiple times when the inner Arena's close fails with+ // IllegalStateExceptionprivate final Runnable onClose;+private final Arena arena;// high 16 bits contain the total remaining acquires; monotonically decreasing@@ -99,14 +103,10 @@ final class RefCountedSharedArena implements Arena {/** Decrements the ref count. */void release() {- int value;while (true) {- value = state.get();+ final int value = state.get();final int count = value & 0xFFFF;- if (count == 0) {- throw new IllegalStateException(value == CLOSED ? "closed" : "nothing to release");- }- final int newValue = count == 1 ? CLOSED : value - 1;+ final int newValue = count <= 1 ? CLOSED : value - 1;if (this.state.compareAndSet(value, newValue)) {if (newValue == CLOSED) {onClose.run();