Skip to content

Commit

Permalink
Merge pull request #23464 from geoand/list-leak
Browse files Browse the repository at this point in the history
Fix various Files.list resource leaks
  • Loading branch information
famod authored Feb 6, 2022
2 parents d19cbbf + c9d6848 commit 07a86fe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> 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)) {
Expand All @@ -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<Path> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -847,9 +848,11 @@ private List<String> 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<Path> paths = Files.list(path)) {
return paths.map((t) -> {
return pathName + "/" + t.getFileName().toString();
}).collect(Collectors.toList());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Path> webjarsDirPaths = Files.list(webjarsDir)) {
nameDir = webjarsDirPaths.filter(Files::isDirectory).findFirst().get();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Path> dirPaths = Files.list(dir)) {
dirPaths.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
Expand Down

0 comments on commit 07a86fe

Please sign in to comment.