Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resource leak due to Files.walk and Files.lines #79335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix resource leak due to Files.walk and Files.lines
  • Loading branch information
lujiefsi committed Oct 18, 2021
commit 2b72d64ceb35787c0eb4aa0650e9c8ecb29440f6
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

Expand Down Expand Up @@ -301,12 +302,13 @@ private static void walkJavaFiles(Path root, String suffix, Consumer<Path> class
if (Files.exists(root) == false) {
return;
}
Files.walk(root)
.filter(p -> p.toString().endsWith(suffix))
.map(root::relativize)
.filter(p -> p.getNameCount() > 1) // module-info or other things without a package can be skipped
.filter(p -> p.toString().startsWith("META-INF") == false)
.forEach(classConsumer);
try (Stream<Path> stream = Files.walk(root)) {
stream.filter(p -> p.toString().endsWith(suffix))
.map(root::relativize)
.filter(p -> p.getNameCount() > 1) // module-info or other things without a package can be skipped
.filter(p -> p.toString().startsWith("META-INF") == false)
.forEach(classConsumer);
}
}

private static String getPackageName(Path path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.inject.Inject;

Expand Down Expand Up @@ -84,13 +85,13 @@ public void setSpoolOutput(boolean spoolOutput) {
File spoolFile = new File(getProject().getBuildDir() + "/buffered-output/" + this.getName());
out = new LazyFileOutputStream(spoolFile);
outputLogger = logger -> {
try {
// the file may not exist if the command never output anything
if (Files.exists(spoolFile.toPath())) {
Files.lines(spoolFile.toPath()).forEach(logger::error);
// the file may not exist if the command never output anything
if (Files.exists(spoolFile.toPath())) {
try (Stream<String> stream = Files.lines(spoolFile.toPath())) {
stream.forEach(logger::error);
} catch (IOException e) {
throw new RuntimeException("could not log", e);
}
} catch (IOException e) {
throw new RuntimeException("could not log", e);
}
};
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Locale;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This class looks for plugins whose "type" is "bootstrap". Such plugins
Expand All @@ -39,29 +40,31 @@ public static List<String> bootstrapJvmOptions(Path plugins) throws IOException
private static List<PluginInfo> getPluginInfo(Path plugins) throws IOException {
final List<PluginInfo> pluginInfo = new ArrayList<>();

final List<Path> pluginDirs = Files.list(plugins).collect(Collectors.toList());

for (Path pluginDir : pluginDirs) {
final List<String> jarFiles = new ArrayList<>();
final Properties props = new Properties();

final List<Path> pluginFiles = Files.list(pluginDir).collect(Collectors.toList());
for (Path pluginFile : pluginFiles) {
final String lowerCaseName = pluginFile.getFileName().toString().toLowerCase(Locale.ROOT);

if (lowerCaseName.endsWith(".jar")) {
jarFiles.add(pluginFile.toString());
} else if (lowerCaseName.equals("plugin-descriptor.properties")) {
try (InputStream stream = Files.newInputStream(pluginFile)) {
props.load(stream);
} catch (IOException e) {
throw new UncheckedIOException(e);
try (Stream<Path> pluginsList = Files.list(plugins)) {
final List<Path> pluginDirs = pluginsList.collect(Collectors.toList());
for (Path pluginDir : pluginDirs) {
final List<String> jarFiles = new ArrayList<>();
final Properties props = new Properties();
try (Stream<Path> pluginDirList = Files.list(pluginDir)) {
final List<Path> pluginFiles = pluginDirList.collect(Collectors.toList());
for (Path pluginFile : pluginFiles) {
final String lowerCaseName = pluginFile.getFileName().toString().toLowerCase(Locale.ROOT);

if (lowerCaseName.endsWith(".jar")) {
jarFiles.add(pluginFile.toString());
} else if (lowerCaseName.equals("plugin-descriptor.properties")) {
try (InputStream stream = Files.newInputStream(pluginFile)) {
props.load(stream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
}
}

if (props.isEmpty() == false) {
pluginInfo.add(new PluginInfo(jarFiles, props));
if (props.isEmpty() == false) {
pluginInfo.add(new PluginInfo(jarFiles, props));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.elasticsearch.common.ssl.PemUtils.parsePKCS8PemString;
import static org.elasticsearch.discovery.SettingsBasedSeedHostsProvider.DISCOVERY_SEED_HOSTS_SETTING;
Expand Down Expand Up @@ -115,11 +116,15 @@ protected void execute(Terminal terminal, OptionSet options, Environment env) th
// TODO: Files.list leaks a file handle because the stream is not closed
// this effectively doesn't matter since enroll is run in a separate, short lived, process
// but it should be fixed...
if (Files.isDirectory(dataPath) && Files.list(dataPath).findAny().isPresent()) {
throw new UserException(
ExitCodes.CONFIG,
"Aborting enrolling to cluster. It appears that this is not the first time this node starts."
);
if (Files.isDirectory(dataPath)) {
try (Stream<Path> stream = Files.list(dataPath)) {
if (stream.findAny().isPresent()) {
throw new UserException(
ExitCodes.CONFIG,
"Aborting enrolling to cluster. It appears that this is not the first time this node starts."
);
}
}
}
}

Expand Down