Skip to content

Commit

Permalink
Fix 846 Formatter leaks threads and memory
Browse files Browse the repository at this point in the history
I've signed the CLA.

Fixes #847

FUTURE_COPYBARA_INTEGRATE_REVIEW=#847 from stiemannkj1:fix-846-mem-thread-leak 0ca1e9b
PiperOrigin-RevId: 506945523
  • Loading branch information
java-team-github-bot authored and google-java-format Team committed Feb 3, 2023
1 parent 4a22aab commit a70d5b7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/src/main/java/com/google/googlejavaformat/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.googlejavaformat.FormatterDiagnostic;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import java.io.IOError;
Expand All @@ -35,6 +36,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/** The main class for the Java formatter CLI. */
public final class Main {
Expand Down Expand Up @@ -187,6 +189,19 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
outWriter.write(formatted);
}
}

// #846 Clean up any resources and threads created by the executorService.
final boolean completedShutdown =
MoreExecutors.shutdownAndAwaitTermination(executorService, 100, TimeUnit.MILLISECONDS);

if (!completedShutdown) {
// We wait for all formatting tasks to complete when looping through the formatting tasks
// before shutdown, so no tasks should be left over by the time we shut down the executor
// service.
throw new IllegalStateException(
"Failed to complete all formatting tasks and shut down the formatter.");
}

return allOk ? 0 : 1;
}

Expand Down

0 comments on commit a70d5b7

Please sign in to comment.