Apache Lucene open-source search software
LUCENE-2446: ensure we close file if we hit exception writing codec header
Robert Muire26cc9283bfa
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.javaindex b2a742ce5e6..4cf947d1b88 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/MemoryPostingsFormat.java@@ -284,7 +284,15 @@ public final class MemoryPostingsFormat extends PostingsFormat {private MemoryFieldsConsumer(SegmentWriteState state) throws IOException {final String fileName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, EXTENSION);out = state.directory.createOutput(fileName, state.context);- CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);+ boolean success = false;+ try {+ CodecUtil.writeHeader(out, CODEC_NAME, VERSION_CURRENT);+ success = true;+ } finally {+ if (!success) {+ IOUtils.closeWhileHandlingException(out);+ }+ }this.state = state;}