Skip to content

Apache Lucene open-source search software

LUCENE-5654: Fix close methods that would suppress OOME or similar

Robert Muir56ab1d110260
diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/BinaryDictionary.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/BinaryDictionary.java
index 251875cc83d..1956c996637 100644
--- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/BinaryDictionary.java
+++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/BinaryDictionary.java
@@ -54,12 +54,12 @@ public abstract class BinaryDictionary implements Dictionary {
protected BinaryDictionary() throws IOException {
InputStream mapIS = null, dictIS = null, posIS = null;
- IOException priorE = null;
int[] targetMapOffsets = null, targetMap = null;
String[] posDict = null;
String[] inflFormDict = null;
String[] inflTypeDict = null;
ByteBuffer buffer = null;
+ boolean success = false;
try {
mapIS = getResource(TARGETMAP_FILENAME_SUFFIX);
mapIS = new BufferedInputStream(mapIS);
@@ -117,10 +117,13 @@ public abstract class BinaryDictionary implements Dictionary {
}
dictIS.close(); dictIS = null;
buffer = tmpBuffer.asReadOnlyBuffer();
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, mapIS, posIS, dictIS);
+ if (success) {
+ IOUtils.close(mapIS, posIS, dictIS);
+ } else {
+ IOUtils.closeWhileHandlingException(mapIS, posIS, dictIS);
+ }
}
this.targetMap = targetMap;
diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/CharacterDefinition.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/CharacterDefinition.java
index fbd86118d0e..e153ef27a4e 100644
--- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/CharacterDefinition.java
+++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/CharacterDefinition.java
@@ -62,8 +62,8 @@ public final class CharacterDefinition {
public static final byte KANJINUMERIC = (byte) CharacterClass.KANJINUMERIC.ordinal();
private CharacterDefinition() throws IOException {
- IOException priorE = null;
InputStream is = null;
+ boolean success = false;
try {
is = BinaryDictionary.getClassResource(getClass(), FILENAME_SUFFIX);
is = new BufferedInputStream(is);
@@ -75,10 +75,13 @@ public final class CharacterDefinition {
invokeMap[i] = (b & 0x01) != 0;
groupMap[i] = (b & 0x02) != 0;
}
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, is);
+ if (success) {
+ IOUtils.close(is);
+ } else {
+ IOUtils.closeWhileHandlingException(is);
+ }
}
}
diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/ConnectionCosts.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/ConnectionCosts.java
index 48e2884cb83..47315c8f267 100644
--- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/ConnectionCosts.java
+++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/ConnectionCosts.java
@@ -39,9 +39,9 @@ public final class ConnectionCosts {
private final short[][] costs; // array is backward IDs first since get is called using the same backward ID consecutively. maybe doesn't matter.
private ConnectionCosts() throws IOException {
- IOException priorE = null;
InputStream is = null;
short[][] costs = null;
+ boolean success = false;
try {
is = BinaryDictionary.getClassResource(getClass(), FILENAME_SUFFIX);
is = new BufferedInputStream(is);
@@ -58,10 +58,13 @@ public final class ConnectionCosts {
a[i] = (short)accum;
}
}
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, is);
+ if (success) {
+ IOUtils.close(is);
+ } else {
+ IOUtils.closeWhileHandlingException(is);
+ }
}
this.costs = costs;
diff --git a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionary.java b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionary.java
index b6283438d22..bc0ccd510f8 100644
--- a/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionary.java
+++ b/lucene/analysis/kuromoji/src/java/org/apache/lucene/analysis/ja/dict/TokenInfoDictionary.java
@@ -38,17 +38,20 @@ public final class TokenInfoDictionary extends BinaryDictionary {
private TokenInfoDictionary() throws IOException {
super();
- IOException priorE = null;
InputStream is = null;
FST<Long> fst = null;
+ boolean success = false;
try {
is = getResource(FST_FILENAME_SUFFIX);
is = new BufferedInputStream(is);
fst = new FST<>(new InputStreamDataInput(is), PositiveIntOutputs.getSingleton());
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, is);
+ if (success) {
+ IOUtils.close(is);
+ } else {
+ IOUtils.closeWhileHandlingException(is);
+ }
}
// TODO: some way to configure?
this.fst = new TokenInfoFST(fst, true);
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTOrdTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTOrdTermsWriter.java
index 8c3bf6bc702..5337b9bc51c 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTOrdTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTOrdTermsWriter.java
@@ -219,7 +219,7 @@ public class FSTOrdTermsWriter extends FieldsConsumer {
@Override
public void close() throws IOException {
if (blockOut != null) {
- IOException ioe = null;
+ boolean success = false;
try {
final long blockDirStart = blockOut.getFilePointer();
@@ -247,10 +247,13 @@ public class FSTOrdTermsWriter extends FieldsConsumer {
writeTrailer(blockOut, blockDirStart);
CodecUtil.writeFooter(indexOut);
CodecUtil.writeFooter(blockOut);
- } catch (IOException ioe2) {
- ioe = ioe2;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(ioe, blockOut, indexOut, postingsWriter);
+ if (success) {
+ IOUtils.close(blockOut, indexOut, postingsWriter);
+ } else {
+ IOUtils.closeWhileHandlingException(blockOut, indexOut, postingsWriter);
+ }
blockOut = null;
}
}
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
index 3f5181e683d..88abf78ff11 100644
--- a/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/memory/FSTTermsWriter.java
@@ -198,7 +198,7 @@ public class FSTTermsWriter extends FieldsConsumer {
@Override
public void close() throws IOException {
if (out != null) {
- IOException ioe = null;
+ boolean success = false;
try {
// write field summary
final long dirStart = out.getFilePointer();
@@ -217,10 +217,13 @@ public class FSTTermsWriter extends FieldsConsumer {
}
writeTrailer(out, dirStart);
CodecUtil.writeFooter(out);
- } catch (IOException ioe2) {
- ioe = ioe2;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(ioe, out, postingsWriter);
+ if (success) {
+ IOUtils.close(out, postingsWriter);
+ } else {
+ IOUtils.closeWhileHandlingException(out, postingsWriter);
+ }
out = null;
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsWriter.java
index 8ec6fe1b857..ad08498d0d8 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/BlockTreeTermsWriter.java
@@ -1119,7 +1119,7 @@ public class BlockTreeTermsWriter extends FieldsConsumer {
@Override
public void close() throws IOException {
- IOException ioe = null;
+ boolean success = false;
try {
final long dirStart = out.getFilePointer();
@@ -1148,10 +1148,13 @@ public class BlockTreeTermsWriter extends FieldsConsumer {
CodecUtil.writeFooter(out);
writeIndexTrailer(indexOut, indexDirStart);
CodecUtil.writeFooter(indexOut);
- } catch (IOException ioe2) {
- ioe = ioe2;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(ioe, out, indexOut, postingsWriter);
+ if (success) {
+ IOUtils.close(out, indexOut, postingsWriter);
+ } else {
+ IOUtils.closeWhileHandlingException(out, indexOut, postingsWriter);
+ }
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
index c64059c70e8..a6275e1164b 100644
--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
@@ -4498,29 +4498,25 @@ public class IndexWriter implements Closeable, TwoPhaseCommit{
// Now merge all added files
Collection<String> files = info.files();
CompoundFileDirectory cfsDir = new CompoundFileDirectory(directory, fileName, context, true);
- IOException prior = null;
+ boolean success = false;
try {
for (String file : files) {
directory.copy(cfsDir, file, file, context);
checkAbort.work(directory.fileLength(file));
}
- } catch(IOException ex) {
- prior = ex;
+ success = true;
} finally {
- boolean success = false;
- try {
- IOUtils.closeWhileHandlingException(prior, cfsDir);
- success = true;
- } finally {
- if (!success) {
- try {
- directory.deleteFile(fileName);
- } catch (Throwable t) {
- }
- try {
- directory.deleteFile(IndexFileNames.segmentFileName(info.name, "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
- } catch (Throwable t) {
- }
+ if (success) {
+ IOUtils.close(cfsDir);
+ } else {
+ IOUtils.closeWhileHandlingException(cfsDir);
+ try {
+ directory.deleteFile(fileName);
+ } catch (Throwable t) {
+ }
+ try {
+ directory.deleteFile(IndexFileNames.segmentFileName(info.name, "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION));
+ } catch (Throwable t) {
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java
index 4e4d8cdcf0c..4722e8f29f5 100644
--- a/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java
+++ b/lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java
@@ -59,11 +59,9 @@ final class StandardDirectoryReader extends DirectoryReader {
try {
readers[i] = new SegmentReader(sis.info(i), IOContext.READ);
success = true;
- } catch(IOException ex) {
- prior = ex;
} finally {
if (!success) {
- IOUtils.closeWhileHandlingException(prior, readers);
+ IOUtils.closeWhileHandlingException(readers);
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java
index f1017e028f4..001a99cc2c3 100644
--- a/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/CompoundFileDirectory.java
@@ -123,8 +123,9 @@ public final class CompoundFileDirectory extends BaseDirectory {
/** Helper method that reads CFS entries from an input stream */
private final Map<String, FileEntry> readEntries(Directory dir, String name) throws IOException {
- IOException priorE = null;
ChecksumIndexInput entriesStream = null;
+ Map<String,FileEntry> mapping = null;
+ boolean success = false;
try {
final String entriesFileName = IndexFileNames.segmentFileName(
IndexFileNames.stripExtension(name), "",
@@ -132,7 +133,7 @@ public final class CompoundFileDirectory extends BaseDirectory {
entriesStream = dir.openChecksumInput(entriesFileName, IOContext.READONCE);
version = CodecUtil.checkHeader(entriesStream, CompoundFileWriter.ENTRY_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT);
final int numEntries = entriesStream.readVInt();
- final Map<String, FileEntry> mapping = new HashMap<>(numEntries);
+ mapping = new HashMap<>(numEntries);
for (int i = 0; i < numEntries; i++) {
final FileEntry fileEntry = new FileEntry();
final String id = entriesStream.readString();
@@ -148,14 +149,15 @@ public final class CompoundFileDirectory extends BaseDirectory {
} else {
CodecUtil.checkEOF(entriesStream);
}
- return mapping;
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, entriesStream);
+ if (success) {
+ IOUtils.close(entriesStream);
+ } else {
+ IOUtils.closeWhileHandlingException(entriesStream);
+ }
}
- // this is needed until Java 7's real try-with-resources:
- throw new AssertionError("impossible to get here");
+ return mapping;
}
public Directory getDirectory() {
diff --git a/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java b/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java
index 076402cf352..b6459ea8e73 100644
--- a/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java
+++ b/lucene/core/src/java/org/apache/lucene/store/CompoundFileWriter.java
@@ -129,10 +129,10 @@ final class CompoundFileWriter implements Closeable{
if (closed) {
return;
}
- IOException priorException = null;
IndexOutput entryTableOut = null;
// TODO this code should clean up after itself
// (remove partial .cfs/.cfe)
+ boolean success = false;
try {
if (!pendingEntries.isEmpty() || outputTaken.get()) {
throw new IllegalStateException("CFS has pending open files");
@@ -142,18 +142,25 @@ final class CompoundFileWriter implements Closeable{
getOutput();
assert dataOut != null;
CodecUtil.writeFooter(dataOut);
- } catch (IOException e) {
- priorException = e;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorException, dataOut);
+ if (success) {
+ IOUtils.close(dataOut);
+ } else {
+ IOUtils.closeWhileHandlingException(dataOut);
+ }
}
+ success = false;
try {
entryTableOut = directory.createOutput(entryTableName, IOContext.DEFAULT);
writeEntryTable(entries.values(), entryTableOut);
- } catch (IOException e) {
- priorException = e;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorException, entryTableOut);
+ if (success) {
+ IOUtils.close(entryTableOut);
+ } else {
+ IOUtils.closeWhileHandlingException(entryTableOut);
+ }
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.java
index f45abb1ee3d..d960d01a901 100644
--- a/lucene/core/src/java/org/apache/lucene/store/Directory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java
@@ -181,24 +181,20 @@ public abstract class Directory implements Closeable {
public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
IndexOutput os = null;
IndexInput is = null;
- IOException priorException = null;
+ boolean success = false;
try {
os = to.createOutput(dest, context);
is = openInput(src, context);
os.copyBytes(is, is.length());
- } catch (IOException ioe) {
- priorException = ioe;
+ success = true;
} finally {
- boolean success = false;
- try {
- IOUtils.closeWhileHandlingException(priorException, os, is);
- success = true;
- } finally {
- if (!success) {
- try {
- to.deleteFile(dest);
- } catch (Throwable t) {
- }
+ if (success) {
+ IOUtils.close(os, is);
+ } else {
+ IOUtils.closeWhileHandlingException(os, is);
+ try {
+ to.deleteFile(dest);
+ } catch (Throwable t) {
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
index b5721dfc4cb..1e32c76424c 100644
--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
@@ -378,14 +378,17 @@ public abstract class FSDirectory extends BaseDirectory {
parent.onIndexOutputClosed(name);
// only close the file if it has not been closed yet
if (isOpen) {
- IOException priorE = null;
+ boolean success = false;
try {
super.close();
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
isOpen = false;
- IOUtils.closeWhileHandlingException(priorE, file);
+ if (success) {
+ IOUtils.close(file);
+ } else {
+ IOUtils.closeWhileHandlingException(file);
+ }
}
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
index a4f8b2a4ace..88a2d7b03d3 100644
--- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java
@@ -58,76 +58,6 @@ public final class IOUtils {
private IOUtils() {} // no instance
- /**
- * <p>Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions. Some of the <tt>Closeable</tt>s
- * may be null, they are ignored. After everything is closed, method either throws <tt>priorException</tt>,
- * if one is supplied, or the first of suppressed exceptions, or completes normally.</p>
- * <p>Sample usage:<br/>
- * <pre class="prettyprint">
- * Closeable resource1 = null, resource2 = null, resource3 = null;
- * ExpectedException priorE = null;
- * try {
- * resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException
- * ..do..stuff.. // May throw ExpectedException
- * } catch (ExpectedException e) {
- * priorE = e;
- * } finally {
- * closeWhileHandlingException(priorE, resource1, resource2, resource3);
- * }
- * </pre>
- * </p>
- * @param priorException <tt>null</tt> or an exception that will be rethrown after method completion
- * @param objects objects to call <tt>close()</tt> on
- */
- public static <E extends Exception> void closeWhileHandlingException(E priorException, Closeable... objects) throws E, IOException {
- Throwable th = null;
-
- for (Closeable object : objects) {
- try {
- if (object != null) {
- object.close();
- }
- } catch (Throwable t) {
- addSuppressed((priorException == null) ? th : priorException, t);
- if (th == null) {
- th = t;
- }
- }
- }
-
- if (priorException != null) {
- throw priorException;
- } else {
- reThrow(th);
- }
- }
-
- /**
- * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
- * @see #closeWhileHandlingException(Exception, Closeable...) */
- public static <E extends Exception> void closeWhileHandlingException(E priorException, Iterable<? extends Closeable> objects) throws E, IOException {
- Throwable th = null;
-
- for (Closeable object : objects) {
- try {
- if (object != null) {
- object.close();
- }
- } catch (Throwable t) {
- addSuppressed((priorException == null) ? th : priorException, t);
- if (th == null) {
- th = t;
- }
- }
- }
-
- if (priorException != null) {
- throw priorException;
- } else {
- reThrow(th);
- }
- }
-
/**
* Closes all given <tt>Closeable</tt>s. Some of the
* <tt>Closeable</tt>s may be null; they are
diff --git a/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java b/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
index 5ea20c4aaeb..de057c3fd4f 100644
--- a/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
+++ b/lucene/core/src/java/org/apache/lucene/util/SPIClassIterator.java
@@ -90,7 +90,7 @@ public final class SPIClassIterator<S> implements Iterator<Class<? extends S>> {
final URL url = profilesEnum.nextElement();
try {
final InputStream in = url.openStream();
- IOException priorE = null;
+ boolean success = false;
try {
final BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
String line;
@@ -104,10 +104,13 @@ public final class SPIClassIterator<S> implements Iterator<Class<? extends S>> {
lines.add(line);
}
}
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, in);
+ if (success) {
+ IOUtils.close(in);
+ } else {
+ IOUtils.closeWhileHandlingException(in);
+ }
}
} catch (IOException ioe) {
throw new ServiceConfigurationError("Error loading SPI class list from URL: " + url, ioe);
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java b/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
deleted file mode 100644
index b816026a219..00000000000
--- a/lucene/core/src/test/org/apache/lucene/util/TestIOUtils.java
+++ /dev/null
@@ -1,91 +0,0 @@
-package org.apache.lucene.util;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-public class TestIOUtils extends LuceneTestCase {
-
- static final class BrokenCloseable implements Closeable {
- final int i;
-
- public BrokenCloseable(int i) {
- this.i = i;
- }
-
- @Override
- public void close() throws IOException {
- throw new IOException("TEST-IO-EXCEPTION-" + i);
- }
- }
-
- static final class TestException extends Exception {
- public TestException() {
- super("BASE-EXCEPTION");
- }
- }
-
- public void testSuppressedExceptions() {
- // test with prior exception
- try {
- final TestException t = new TestException();
- IOUtils.closeWhileHandlingException(t, new BrokenCloseable(1), new BrokenCloseable(2));
- } catch (TestException e1) {
- assertEquals("BASE-EXCEPTION", e1.getMessage());
- final StringWriter sw = new StringWriter();
- final PrintWriter pw = new PrintWriter(sw);
- e1.printStackTrace(pw);
- pw.flush();
- final String trace = sw.toString();
- if (VERBOSE) {
- System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
- System.out.println(trace);
- }
- assertTrue("Stack trace does not contain first suppressed Exception: " + trace,
- trace.contains("java.io.IOException: TEST-IO-EXCEPTION-1"));
- assertTrue("Stack trace does not contain second suppressed Exception: " + trace,
- trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));
- } catch (IOException e2) {
- fail("IOException should not be thrown here");
- }
-
- // test without prior exception
- try {
- IOUtils.closeWhileHandlingException((TestException) null, new BrokenCloseable(1), new BrokenCloseable(2));
- } catch (TestException e1) {
- fail("TestException should not be thrown here");
- } catch (IOException e2) {
- assertEquals("TEST-IO-EXCEPTION-1", e2.getMessage());
- final StringWriter sw = new StringWriter();
- final PrintWriter pw = new PrintWriter(sw);
- e2.printStackTrace(pw);
- pw.flush();
- final String trace = sw.toString();
- if (VERBOSE) {
- System.out.println("TestIOUtils.testSuppressedExceptions: Thrown Exception stack trace:");
- System.out.println(trace);
- }
- assertTrue("Stack trace does not contain suppressed Exception: " + trace,
- trace.contains("java.io.IOException: TEST-IO-EXCEPTION-2"));
- }
- }
-
-}
diff --git a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java
index ee49f62c2dd..48e572bfba4 100644
--- a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java
+++ b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsDirectory.java
@@ -243,13 +243,16 @@ public class HdfsDirectory extends BaseDirectory {
@Override
public void close() throws IOException {
- IOException priorE = null;
+ boolean success = false;
try {
super.close();
- } catch (IOException ioe) {
- priorE = ioe;
+ success = true;
} finally {
- IOUtils.closeWhileHandlingException(priorE, writer);
+ if (success) {
+ IOUtils.close(writer);
+ } else {
+ IOUtils.closeWhileHandlingException(writer);
+ }
}
}