From 90b981bf36355fe8cf83689c477e0b46d8fd2f7f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 6 Jun 2019 21:47:07 -0400 Subject: [PATCH] Only ignore IOException when fsyncing on dirs Today in the method IOUtils#fsync we ignore IOExceptions when fsyncing a directory. However, the catch block here is too broad, for example it would be ignoring IOExceptions when we try to open a non-existant file. This commit addresses that by scoping the ignored exceptions only to the invocation of FileChannel#force. --- .../core/internal/io/IOUtils.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java b/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java index 46d19d2a814fe..26395cb2c59ea 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java +++ b/libs/core/src/main/java/org/elasticsearch/core/internal/io/IOUtils.java @@ -264,17 +264,19 @@ public FileVisitResult visitFileFailed(final Path file, final IOException exc) t */ public static void fsync(final Path fileToSync, final boolean isDir) throws IOException { try (FileChannel file = FileChannel.open(fileToSync, isDir ? StandardOpenOption.READ : StandardOpenOption.WRITE)) { - file.force(true); - } catch (final IOException ioe) { - if (isDir) { - assert (LINUX || MAC_OS_X) == false : - "on Linux and MacOSX fsyncing a directory should not throw IOException, "+ - "we just don't want to rely on that in production (undocumented); got: " + ioe; - // ignore exception if it is a directory - return; + try { + file.force(true); + } catch (final IOException e) { + if (isDir) { + assert (LINUX || MAC_OS_X) == false : + "on Linux and MacOSX fsyncing a directory should not throw IOException, "+ + "we just don't want to rely on that in production (undocumented); got: " + e; + // ignore exception if it is a directory + return; + } + // throw original exception + throw e; } - // throw original exception - throw ioe; } } }