fix resource leak

This commit is contained in:
Sebastian Stenzel 2021-12-03 11:38:25 +01:00
parent 085304ec04
commit f277d4d21b
No known key found for this signature in database
GPG Key ID: 667B866EA8240A09
2 changed files with 20 additions and 8 deletions

View File

@ -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());
}
}
}
}

View File

@ -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