Skip to content

Commit

Permalink
Catch non-IOException in FileUtils.close so all resources are closed
Browse files Browse the repository at this point in the history
patch by Liudmila Kornilova; reviewed by Benedict Elliott Smith for CASSANDRA-15225
  • Loading branch information
kornilova203 authored and belliottsmith committed Aug 7, 2019
1 parent b2f6953 commit f21106f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
@@ -1,4 +1,5 @@
2.2.15
* Catch non-IOException in FileUtils.close to make sure that all resources are closed (CASSANDRA-15225)
* Handle exceptions during authentication/authorization (CASSANDRA-15041)
* Fix JDK7 compatibility broken in cassandra-2.2 (CASSANDRA-15050)
* Support cross version messaging in in-jvm upgrade dtests (CASSANDRA-15078)
Expand Down
10 changes: 5 additions & 5 deletions src/java/org/apache/cassandra/io/util/FileUtils.java
Expand Up @@ -235,22 +235,22 @@ public static void close(Closeable... cs) throws IOException

public static void close(Iterable<? extends Closeable> cs) throws IOException
{
IOException e = null;
Throwable e = null;
for (Closeable c : cs)
{
try
{
if (c != null)
c.close();
}
catch (IOException ex)
catch (Throwable ex)
{
e = ex;
if (e == null) e = ex;
else e.addSuppressed(ex);
logger.warn("Failed closing stream {}", c, ex);
}
}
if (e != null)
throw e;
maybeFail(e, IOException.class);
}

public static String getCanonicalPath(String filename)
Expand Down
27 changes: 25 additions & 2 deletions src/java/org/apache/cassandra/utils/Throwables.java
Expand Up @@ -35,8 +35,31 @@ public static Throwable merge(Throwable existingFail, Throwable newFail)

public static void maybeFail(Throwable fail)
{
if (fail != null)
com.google.common.base.Throwables.propagate(fail);
if (failIfCanCast(fail, null))
throw new RuntimeException(fail);
}

public static <T extends Throwable> void maybeFail(Throwable fail, Class<T> checked) throws T
{
if (failIfCanCast(fail, checked))
throw new RuntimeException(fail);
}

public static <T extends Throwable> boolean failIfCanCast(Throwable fail, Class<T> checked) throws T
{
if (fail == null)
return false;

if (fail instanceof Error)
throw (Error) fail;

if (fail instanceof RuntimeException)
throw (RuntimeException) fail;

if (checked != null && checked.isInstance(fail))
throw checked.cast(fail);

return true;
}

public static Throwable close(Throwable accumulate, Iterable<? extends AutoCloseable> closeables)
Expand Down

0 comments on commit f21106f

Please sign in to comment.