ExecutionTool: close output file of "--explain"
Close the OutputStream of "explanationHandler"
after the handler is uninstalled. This guarantees
that Bazel releases this file promptly, instead of
relying on the GC and finalizers to do so.
Closing streams is important to avoid stream
contention which may prevent deleting files. (Open
files may not be deleted on Windows.)
I (laszlocsomor@) am not currently unaware of any
such contention bugs in this code, but we have
seen intermittent file deletion failures on
Windows, so eliminating a potential source of such
a bug should not hurt.
RELNOTES: none
PiperOrigin-RevId: 222545449
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
index e48494f..3cf5184 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/ExecutionTool.java
@@ -404,6 +404,11 @@
if (explanationHandler != null) {
uninstallExplanationHandler(explanationHandler);
+ try {
+ explanationHandler.close();
+ } catch (IOException _ignored) {
+ // Ignored
+ }
}
// Finalize output service last, so that if we do throw an exception, we know all the other
// code has already run.
@@ -508,10 +513,10 @@
}
/**
- * An ErrorEventListener implementation that records DEPCHECKER events into a log
- * file, iff the --explain flag is specified during a build.
+ * An ErrorEventListener implementation that records DEPCHECKER events into a log file, iff the
+ * --explain flag is specified during a build.
*/
- private static class ExplanationHandler implements EventHandler {
+ private static class ExplanationHandler implements EventHandler, AutoCloseable {
private final PrintWriter log;
private ExplanationHandler(OutputStream log, String optionsDescription) {
@@ -519,6 +524,10 @@
this.log.println("Build options: " + optionsDescription);
}
+ @Override
+ public void close() throws IOException {
+ this.log.close();
+ }
@Override
public void handle(Event event) {