Only ignore IOException when fsyncing on dirs (#42972)

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.
This commit is contained in:
Jason Tedor 2019-06-07 08:34:19 -04:00
parent dea935ac31
commit 45bbd7f7f1
No known key found for this signature in database
GPG Key ID: FA89F05560F16BC5
1 changed files with 12 additions and 10 deletions

View File

@ -264,17 +264,19 @@ public final class IOUtils {
*/
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;
}
}
}