From f277d4d21bbb8c536dff2c77efe72ceb77e88cd2 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Fri, 3 Dec 2021 11:38:25 +0100 Subject: [PATCH] fix resource leak --- .../common/mountpoint/MountPointHelper.java | 10 ++++++---- src/main/java/org/cryptomator/ipc/Server.java | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/cryptomator/common/mountpoint/MountPointHelper.java b/src/main/java/org/cryptomator/common/mountpoint/MountPointHelper.java index 704f2f62d..fe64902bd 100644 --- a/src/main/java/org/cryptomator/common/mountpoint/MountPointHelper.java +++ b/src/main/java/org/cryptomator/common/mountpoint/MountPointHelper.java @@ -66,9 +66,9 @@ class MountPointHelper { private void clearIrregularUnmountDebris(Path dirContainingMountPoints) { IOException cleanupFailed = new IOException("Cleanup failed"); - try { + try (var ds = Files.newDirectoryStream(dirContainingMountPoints)) { LOG.debug("Performing cleanup of mountpoint dir {}.", dirContainingMountPoints); - for (Path p : Files.newDirectoryStream(dirContainingMountPoints)) { + for (Path p : ds) { try { var attr = Files.readAttributes(p, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (attr.isOther() && attr.isDirectory()) { // yes, this is possible with windows junction points -.- @@ -113,8 +113,10 @@ class MountPointHelper { } private void ensureIsEmpty(Path dir) throws IOException { - if (Files.newDirectoryStream(dir).iterator().hasNext()) { - throw new DirectoryNotEmptyException(dir.toString()); + try (var ds = Files.newDirectoryStream(dir)) { + if (ds.iterator().hasNext()){ + throw new DirectoryNotEmptyException(dir.toString()); + } } } } diff --git a/src/main/java/org/cryptomator/ipc/Server.java b/src/main/java/org/cryptomator/ipc/Server.java index 6058a608f..770373681 100644 --- a/src/main/java/org/cryptomator/ipc/Server.java +++ b/src/main/java/org/cryptomator/ipc/Server.java @@ -7,9 +7,11 @@ import java.io.EOFException; import java.io.IOException; import java.net.StandardProtocolFamily; import java.net.UnixDomainSocketAddress; +import java.nio.channels.AlreadyBoundException; import java.nio.channels.AsynchronousCloseException; import java.nio.channels.ClosedChannelException; import java.nio.channels.ServerSocketChannel; +import java.nio.channels.UnsupportedAddressTypeException; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.Executor; @@ -29,10 +31,18 @@ class Server implements IpcCommunicator { public static Server create(Path socketPath) throws IOException { Files.createDirectories(socketPath.getParent()); var address = UnixDomainSocketAddress.of(socketPath); - var serverSocketChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX); - serverSocketChannel.bind(address); - LOG.info("Spawning IPC server listening on socket {}", socketPath); - return new Server(serverSocketChannel, socketPath); + ServerSocketChannel ch = null; + try { + ch = ServerSocketChannel.open(StandardProtocolFamily.UNIX); + ch.bind(address); + LOG.info("Spawning IPC server listening on socket {}", socketPath); + return new Server(ch, socketPath); + } catch (IOException | AlreadyBoundException | UnsupportedAddressTypeException e) { + if (ch != null) { + ch.close(); + } + throw e; + } } @Override