Skip to content

Commit

Permalink
ExecutionTool: close output file of "--explain"
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laszlocsomor authored and Copybara-Service committed Nov 22, 2018
1 parent fb4924a commit 482235b
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ void executeBuild(

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.
Expand Down Expand Up @@ -508,17 +513,21 @@ private void uninstallExplanationHandler(ExplanationHandler handler) {
}

/**
* 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) {
this.log = new PrintWriter(new OutputStreamWriter(log, StandardCharsets.UTF_8));
this.log.println("Build options: " + optionsDescription);
}

@Override
public void close() throws IOException {
this.log.close();
}

@Override
public void handle(Event event) {
Expand Down

0 comments on commit 482235b

Please sign in to comment.