diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index b4757401478..072cdd2e352 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -101,6 +101,12 @@ Bug fixes * LUCENE-5285: Improved highlighting of multi-valued fields with FastVectorHighlighter. (Nik Everett via Adrien Grand) +Changes in Runtime Behavior + +* LUCENE-5362: IndexReader and SegmentCoreReaders now throw + AlreadyClosedException if the refCount in incremented but + is less that 1. (Simon Willnauer) + ======================= Lucene 4.6.0 ======================= New Features diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexReader.java b/lucene/core/src/java/org/apache/lucene/index/IndexReader.java index 70174d5f23f..b416812fa55 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexReader.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexReader.java @@ -168,8 +168,9 @@ public abstract class IndexReader implements Closeable { * @see #tryIncRef */ public final void incRef() { - ensureOpen(); - refCount.incrementAndGet(); + if (!tryIncRef()) { + ensureOpen(); + } } /** diff --git a/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java b/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java index 99d96570163..057c98dff51 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java +++ b/lucene/core/src/java/org/apache/lucene/index/SegmentCoreReaders.java @@ -32,6 +32,7 @@ import org.apache.lucene.codecs.PostingsFormat; import org.apache.lucene.codecs.StoredFieldsReader; import org.apache.lucene.codecs.TermVectorsReader; import org.apache.lucene.index.SegmentReader.CoreClosedListener; +import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.CompoundFileDirectory; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; @@ -139,7 +140,13 @@ final class SegmentCoreReaders { } void incRef() { - ref.incrementAndGet(); + int count; + while ((count = ref.get()) > 0) { + if (ref.compareAndSet(count, count+1)) { + return; + } + } + throw new AlreadyClosedException("SegmentCoreReaders is already closed"); } NumericDocValues getNormValues(FieldInfo fi) throws IOException {