Fix Gradle File leaks (#105597)

Fixing a couple of file leaks (and cleaning up one missing
try-with-resources). The directory descriptor leaks in particular
were leaking massively on every precommit run, to the point where it
slows down the whole system and/or we're running into descriptor limits.
This commit is contained in:
Armin Braun 2024-02-18 17:56:22 +01:00 committed by GitHub
parent 6fec837e32
commit 3f8bc36788
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 9 deletions

View file

@ -21,11 +21,8 @@ import java.util.Properties;
public class VersionPropertiesLoader { public class VersionPropertiesLoader {
static Properties loadBuildSrcVersion(File input, ProviderFactory providerFactory) throws IOException { static Properties loadBuildSrcVersion(File input, ProviderFactory providerFactory) throws IOException {
Properties props = new Properties(); Properties props = new Properties();
InputStream is = new FileInputStream(input); try (InputStream is = new FileInputStream(input)) {
try {
props.load(is); props.load(is);
} finally {
is.close();
} }
loadBuildSrcVersion(props, providerFactory); loadBuildSrcVersion(props, providerFactory);
return props; return props;

View file

@ -146,13 +146,16 @@ public class CopyRestApiTask extends DefaultTask {
try { try {
// check source folder for tests // check source folder for tests
if (sourceResourceDir != null && new File(sourceResourceDir, REST_TEST_PREFIX).exists()) { if (sourceResourceDir != null && new File(sourceResourceDir, REST_TEST_PREFIX).exists()) {
return Files.walk(sourceResourceDir.toPath().resolve(REST_TEST_PREFIX)) try (var files = Files.walk(sourceResourceDir.toPath().resolve(REST_TEST_PREFIX))) {
.anyMatch(p -> p.getFileName().toString().endsWith("yml")); return files.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
}
} }
// check output for cases where tests are copied programmatically // check output for cases where tests are copied programmatically
File yamlTestOutputDir = new File(additionalYamlTestsDir.get().getAsFile(), REST_TEST_PREFIX); File yamlTestOutputDir = new File(additionalYamlTestsDir.get().getAsFile(), REST_TEST_PREFIX);
if (yamlTestOutputDir.exists()) { if (yamlTestOutputDir.exists()) {
return Files.walk(yamlTestOutputDir.toPath()).anyMatch(p -> p.getFileName().toString().endsWith("yml")); try (var files = Files.walk(yamlTestOutputDir.toPath())) {
return files.anyMatch(p -> p.getFileName().toString().endsWith("yml"));
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(String.format("Error determining if this project [%s] has rest tests.", getProject()), e); throw new IllegalStateException(String.format("Error determining if this project [%s] has rest tests.", getProject()), e);

View file

@ -118,7 +118,9 @@ public abstract class LoggedExec extends DefaultTask implements FileSystemOperat
try { try {
// the file may not exist if the command never output anything // the file may not exist if the command never output anything
if (Files.exists(spoolFile.toPath())) { if (Files.exists(spoolFile.toPath())) {
Files.lines(spoolFile.toPath()).forEach(logger::error); try (var lines = Files.lines(spoolFile.toPath())) {
lines.forEach(logger::error);
}
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("could not log", e); throw new RuntimeException("could not log", e);

View file

@ -458,7 +458,9 @@ public class ElasticsearchNode implements TestClusterConfiguration {
// make sure we always start fresh // make sure we always start fresh
if (Files.exists(workingDir)) { if (Files.exists(workingDir)) {
if (preserveDataDir) { if (preserveDataDir) {
Files.list(workingDir).filter(path -> path.equals(confPathData) == false).forEach(this::uncheckedDeleteWithRetry); try (var files = Files.list(workingDir)) {
files.filter(path -> path.equals(confPathData) == false).forEach(this::uncheckedDeleteWithRetry);
}
} else { } else {
deleteWithRetry(workingDir); deleteWithRetry(workingDir);
} }