Skip to content

Commit

Permalink
Fix #846 Formatter leaks threads and memory
Browse files Browse the repository at this point in the history
  • Loading branch information
stiemannkj1 committed Feb 3, 2023
1 parent 4a22aab commit e61f7d9
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 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,18 @@ 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 above, so no tasks should be left over by this
// point.
throw new IllegalStateException(
"Failed to complete all formatting tasks and shut down the formatter.");
}

return allOk ? 0 : 1;
}

Expand Down

0 comments on commit e61f7d9

Please sign in to comment.