diff --git a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java b/lucene/src/java/org/apache/lucene/index/SegmentMerger.java index 6b6bc74f153..fac20c47552 100644 --- a/lucene/src/java/org/apache/lucene/index/SegmentMerger.java +++ b/lucene/src/java/org/apache/lucene/index/SegmentMerger.java @@ -574,37 +574,37 @@ final class SegmentMerger { final List perDocBits = new ArrayList(); final List perDocBitsStarts = new ArrayList(); int docBase = 0; - try { - for(IndexReader r : readers) { - final int maxDoc = r.maxDoc(); - final PerDocValues producer = r.perDocValues(); - if (producer != null) { - perDocSlices.add(new ReaderUtil.Slice(docBase, maxDoc, perDocProducers.size())); - perDocProducers.add(producer); - perDocBits.add(r.getLiveDocs()); - perDocBitsStarts.add(docBase); - } - docBase += maxDoc; + for (IndexReader r : readers) { + final int maxDoc = r.maxDoc(); + final PerDocValues producer = r.perDocValues(); + if (producer != null) { + perDocSlices.add(new ReaderUtil.Slice(docBase, maxDoc, perDocProducers + .size())); + perDocProducers.add(producer); + perDocBits.add(r.getLiveDocs()); + perDocBitsStarts.add(docBase); } - perDocBitsStarts.add(docBase); - if (!perDocSlices.isEmpty()) { - mergeState.multiLiveDocs = new MultiBits(perDocBits, perDocBitsStarts, true); - final PerDocConsumer docsConsumer = codec - .docsConsumer(new PerDocWriteState(segmentWriteState)); - boolean success = false; - try { - final MultiPerDocValues multiPerDocValues = new MultiPerDocValues(perDocProducers - .toArray(PerDocValues.EMPTY_ARRAY), perDocSlices - .toArray(ReaderUtil.Slice.EMPTY_ARRAY)); - docsConsumer.merge(mergeState, multiPerDocValues); - success = true; - } finally { - IOUtils.closeSafely(!success, docsConsumer); - } - } - } finally { - IOUtils.closeSafely(false, perDocProducers); + docBase += maxDoc; } + perDocBitsStarts.add(docBase); + if (!perDocSlices.isEmpty()) { + mergeState.multiLiveDocs = new MultiBits(perDocBits, perDocBitsStarts, + true); + final PerDocConsumer docsConsumer = codec + .docsConsumer(new PerDocWriteState(segmentWriteState)); + boolean success = false; + try { + final MultiPerDocValues multiPerDocValues = new MultiPerDocValues( + perDocProducers.toArray(PerDocValues.EMPTY_ARRAY), + perDocSlices.toArray(ReaderUtil.Slice.EMPTY_ARRAY)); + docsConsumer.merge(mergeState, multiPerDocValues); + success = true; + } finally { + IOUtils.closeSafely(!success, docsConsumer); + } + } + /* don't close the perDocProducers here since they are private segment producers + * and will be closed once the SegmentReader goes out of scope */ } private MergeState mergeState;