Apache Lucene open-source search software
LUCENE-8212: Ensure all closeables are closed even if an VMError is thrown
Simon Willnauerf664896d1fff
diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.javaindex 766d6fb32ea..350ae432306 100644--- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java+++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java@@ -113,19 +113,31 @@ public final class IOUtils {}/**- * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.+ * Closes all given <tt>Closeable</tt>s, suppressing all thrown non {@link VirtualMachineError} exceptions.+ * Even if a {@link VirtualMachineError} is thrown all given closeable are closed.* @see #closeWhileHandlingException(Closeable...)*/public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) {+ VirtualMachineError firstError = null;+ Throwable firstThrowable = null;for (Closeable object : objects) {try {if (object != null) {object.close();}} catch (VirtualMachineError e) {- throw e;+ firstError = useOrSuppress(firstError, e);} catch (Throwable t) {+ firstThrowable = useOrSuppress(firstThrowable, t);+ }+ }+ if (firstError != null) {+ // we ensure that we bubble up any errors. We can't recover from these but need to make sure they are+ // bubbled up. if a non-VMError is thrown we also add the suppressed exceptions to it.+ if (firstThrowable != null) {+ firstError.addSuppressed(firstThrowable);}+ throw firstError;}}