From 7f8093eeb7992b0d1c0c97b0b31ee3b46fcecef4 Mon Sep 17 00:00:00 2001 From: Louis Bergelson Date: Wed, 14 Aug 2019 17:17:07 -0400 Subject: [PATCH] Suppress all exceptions during DeleteOnExitPathHook * Currently we were not catching some commonly thrown exceptions like RuntimeIOException while deleting files at shutdown. This results in confusing error messages at program exit which can mask the actual cause of failures. Now we catch all exceptions and output a log message at debug level in case anyone is interested in seeing the failures. --- .../htsjdk/samtools/util/nio/DeleteOnExitPathHook.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/htsjdk/samtools/util/nio/DeleteOnExitPathHook.java b/src/main/java/htsjdk/samtools/util/nio/DeleteOnExitPathHook.java index ae2a0dd46f..8197c35d69 100644 --- a/src/main/java/htsjdk/samtools/util/nio/DeleteOnExitPathHook.java +++ b/src/main/java/htsjdk/samtools/util/nio/DeleteOnExitPathHook.java @@ -1,5 +1,7 @@ package htsjdk.samtools.util.nio; +import htsjdk.samtools.util.Log; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -16,6 +18,7 @@ * @author Daniel Gomez-Sanchez (magicDGS) */ public class DeleteOnExitPathHook { + private static final Log LOG = Log.getInstance(DeleteOnExitPathHook.class); private static LinkedHashSet paths = new LinkedHashSet<>(); static { Runtime.getRuntime().addShutdownHook(new Thread(DeleteOnExitPathHook::runHooks)); @@ -55,8 +58,8 @@ static void runHooks() { for (Path path : toBeDeleted) { try { Files.delete(path); - } catch (IOException | SecurityException e) { - // do nothing if cannot be deleted, because it is a shutdown hook + } catch (Exception e) { + LOG.debug(e, "Failed to delete file: ", path, " during shutdown because we encountered an exception."); } } }