From c9d684897a14117b27902d9cd85794abe20ba35d Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Sun, 6 Feb 2022 19:36:47 +0200 Subject: [PATCH] Fix various Files.list resource leaks The stream returned by Files.list must be closed according to the Javadoc. In most usages it was correctly done, but there were a few that had slipped through --- .../image/jib/deployment/JibProcessor.java | 33 ++++++++++--------- .../qute/deployment/QuteProcessor.java | 18 +++++----- .../deployment/SmallRyeOpenApiProcessor.java | 9 +++-- .../WebJarLocatorStandaloneBuildStep.java | 5 +-- .../client/maven/MavenRegistryCache.java | 5 +-- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java index 9e465a1879caf..71496c00db2d7 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java @@ -426,22 +426,25 @@ private JibContainerBuilder createContainerBuilderFromFastJar(String baseJvmImag FileEntriesLayer.Builder bootLibsLayerBuilder = FileEntriesLayer.builder(); Path bootLibPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.BOOT_LIB); - Files.list(bootLibPath).forEach(lib -> { - try { - AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB) - .resolve(JarResultBuildStep.BOOT_LIB) - .resolve(lib.getFileName()); - if (appCDSResult.isPresent()) { - // the boot lib jars need to preserve the modification time because otherwise AppCDS won't work - bootLibsLayerBuilder.addEntry(lib, libPathInContainer, Files.getLastModifiedTime(lib).toInstant()); - } else { - bootLibsLayerBuilder.addEntry(lib, libPathInContainer); - } + try (Stream boolLibPaths = Files.list(bootLibPath)) { + boolLibPaths.forEach(lib -> { + try { + AbsoluteUnixPath libPathInContainer = workDirInContainer.resolve(JarResultBuildStep.LIB) + .resolve(JarResultBuildStep.BOOT_LIB) + .resolve(lib.getFileName()); + if (appCDSResult.isPresent()) { + // the boot lib jars need to preserve the modification time because otherwise AppCDS won't work + bootLibsLayerBuilder.addEntry(lib, libPathInContainer, + Files.getLastModifiedTime(lib).toInstant()); + } else { + bootLibsLayerBuilder.addEntry(lib, libPathInContainer); + } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } jibContainerBuilder.addFileEntriesLayer(bootLibsLayerBuilder.build()); Path deploymentPath = componentsPath.resolve(JarResultBuildStep.LIB).resolve(JarResultBuildStep.DEPLOYMENT_LIB); diff --git a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java index ed0fdaae43b3e..50dd9d684b6f1 100644 --- a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java +++ b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java @@ -1272,12 +1272,14 @@ void collectTemplates(ApplicationArchivesBuildItem applicationArchives, for (Path path : artifact.getResolvedPaths()) { if (Files.isDirectory(path)) { // Try to find the templates in the root dir - Path basePath = Files.list(path).filter(QuteProcessor::isBasePath).findFirst().orElse(null); - if (basePath != null) { - LOGGER.debugf("Found extension templates dir: %s", path); - scan(basePath, basePath, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources, - config); - break; + try (Stream paths = Files.list(path)) { + Path basePath = paths.filter(QuteProcessor::isBasePath).findFirst().orElse(null); + if (basePath != null) { + LOGGER.debugf("Found extension templates dir: %s", path); + scan(basePath, basePath, BASE_PATH + "/", watchedPaths, templatePaths, nativeImageResources, + config); + break; + } } } else { try (FileSystem artifactFs = ZipUtils.newFileSystem(path)) { @@ -1298,8 +1300,8 @@ void collectTemplates(ApplicationArchivesBuildItem applicationArchives, for (Path rootDir : tree.getRoots()) { // Note that we cannot use ApplicationArchive.getChildPath(String) here because we would not be able to detect // a wrong directory name on case-insensitive file systems - try { - Path basePath = Files.list(rootDir).filter(QuteProcessor::isBasePath).findFirst().orElse(null); + try (Stream rootDirPaths = Files.list(rootDir)) { + Path basePath = rootDirPaths.filter(QuteProcessor::isBasePath).findFirst().orElse(null); if (basePath != null) { LOGGER.debugf("Found templates dir: %s", basePath); basePaths.add(basePath); diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index 37e3fa4a7a2b2..e2a6e0bbc441c 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -20,6 +20,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; @@ -847,9 +848,11 @@ private List getResourceFiles(String pathName, Path target) throws IOExc if (classes != null) { Path path = classes.resolve(pathName); if (Files.exists(path)) { - return Files.list(path).map((t) -> { - return pathName + "/" + t.getFileName().toString(); - }).collect(Collectors.toList()); + try (Stream paths = Files.list(path)) { + return paths.map((t) -> { + return pathName + "/" + t.getFileName().toString(); + }).collect(Collectors.toList()); + } } } } diff --git a/extensions/webjars-locator/deployment/src/main/java/io/quarkus/webjar/locator/deployment/WebJarLocatorStandaloneBuildStep.java b/extensions/webjars-locator/deployment/src/main/java/io/quarkus/webjar/locator/deployment/WebJarLocatorStandaloneBuildStep.java index 6861f50076f21..95dd06ed48fb7 100644 --- a/extensions/webjars-locator/deployment/src/main/java/io/quarkus/webjar/locator/deployment/WebJarLocatorStandaloneBuildStep.java +++ b/extensions/webjars-locator/deployment/src/main/java/io/quarkus/webjar/locator/deployment/WebJarLocatorStandaloneBuildStep.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Stream; import org.jboss.logging.Logger; @@ -65,8 +66,8 @@ public void findWebjarsAndCreateHandler( provider.apply(tree -> { final Path webjarsDir = tree.getPath(WEBJARS_PREFIX); final Path nameDir; - try { - nameDir = Files.list(webjarsDir).filter(Files::isDirectory).findFirst().get(); + try (Stream webjarsDirPaths = Files.list(webjarsDir)) { + nameDir = webjarsDirPaths.filter(Files::isDirectory).findFirst().get(); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryCache.java b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryCache.java index e6dd61eb72fc7..9db9cd0d56096 100644 --- a/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryCache.java +++ b/independent-projects/tools/registry-client/src/main/java/io/quarkus/registry/client/maven/MavenRegistryCache.java @@ -13,6 +13,7 @@ import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.stream.Stream; import org.eclipse.aether.artifact.DefaultArtifact; public class MavenRegistryCache implements RegistryCache { @@ -50,8 +51,8 @@ public void clearCache() throws RegistryResolutionException { throw new RegistryResolutionException("Failed to resolve " + coords + " locally", e); } if (Files.exists(dir)) { - try { - Files.list(dir).forEach(path -> { + try (Stream dirPaths = Files.list(dir)) { + dirPaths.forEach(path -> { try { Files.delete(path); } catch (IOException e) {