Skip to content

Commit 0ca1e9b

Browse files
committed
Fix #846 Formatter leaks threads and memory
1 parent 4a22aab commit 0ca1e9b

File tree

1 file changed

+15
-0
lines changed
  • core/src/main/java/com/google/googlejavaformat/java

1 file changed

+15
-0
lines changed

core/src/main/java/com/google/googlejavaformat/java/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static java.nio.charset.StandardCharsets.UTF_8;
1919

2020
import com.google.common.io.ByteStreams;
21+
import com.google.common.util.concurrent.MoreExecutors;
2122
import com.google.googlejavaformat.FormatterDiagnostic;
2223
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2324
import java.io.IOError;
@@ -35,6 +36,7 @@
3536
import java.util.concurrent.ExecutorService;
3637
import java.util.concurrent.Executors;
3738
import java.util.concurrent.Future;
39+
import java.util.concurrent.TimeUnit;
3840

3941
/** The main class for the Java formatter CLI. */
4042
public final class Main {
@@ -187,6 +189,19 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
187189
outWriter.write(formatted);
188190
}
189191
}
192+
193+
// #846 Clean up any resources and threads created by the executorService.
194+
final boolean completedShutdown =
195+
MoreExecutors.shutdownAndAwaitTermination(executorService, 100, TimeUnit.MILLISECONDS);
196+
197+
if (!completedShutdown) {
198+
// We wait for all formatting tasks to complete when looping through the formatting tasks
199+
// before shutdown, so no tasks should be left over by the time we shut down the executor
200+
// service.
201+
throw new IllegalStateException(
202+
"Failed to complete all formatting tasks and shut down the formatter.");
203+
}
204+
190205
return allOk ? 0 : 1;
191206
}
192207

0 commit comments

Comments
 (0)