aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar laszlocsomor <laszlocsomor@google.com>2018-07-05 01:12:47 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-05 01:14:00 -0700
commit09d20311d982606093ed881d779bb05a5ee70ed3 (patch)
tree4d78d0c6321dbb534593280595b05be3cd947dbc /src/main/java/com/google/devtools
parent8ff87c164f48dbabe3b20becd00dde90c50d46f5 (diff)
Bazel server: ensure OutputStreams are closed
Use try-with-resources to ensure OutputStreams that we open via FileSystem.OutputStream(path) are closed. Eagerly closing OutputStreams avoids hanging on to file handles until the garbage collector finalizes the OutputStream, meaning Bazel on Windows (and other processes) can delete or mutate these files. Hopefully this avoids intermittent file deletion errors that sometimes occur on Windows. See https://github.com/bazelbuild/bazel/issues/5512 RELNOTES: none PiperOrigin-RevId: 203342889
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/util/DependencySet.java5
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java37
2 files changed, 34 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/util/DependencySet.java b/src/main/java/com/google/devtools/build/lib/util/DependencySet.java
index e2ce5fb6ce..66f1bd0f25 100644
--- a/src/main/java/com/google/devtools/build/lib/util/DependencySet.java
+++ b/src/main/java/com/google/devtools/build/lib/util/DependencySet.java
@@ -228,15 +228,12 @@ public final class DependencySet {
Path dotdFile =
outFile.getRelative(FileSystemUtils.replaceExtension(outFile.asFragment(), suffix));
- PrintStream out = new PrintStream(dotdFile.getOutputStream());
- try {
+ try (PrintStream out = new PrintStream(dotdFile.getOutputStream())) {
out.print(outFile.relativeTo(root) + ": ");
for (Path d : dependencies) {
out.print(" \\\n " + d.getPathString()); // should already be root relative
}
out.println();
- } finally {
- out.close();
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
index e2ab1d8002..9883030cb6 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemUtils.java
@@ -698,7 +698,14 @@ public class FileSystemUtils {
*/
public static void writeContent(Path outputFile, Charset charset, String content)
throws IOException {
- asByteSink(outputFile).asCharSink(charset).write(content);
+ try (OutputStream out = outputFile.getOutputStream()) {
+ new ByteSink() {
+ @Override
+ public OutputStream openStream() throws IOException {
+ return out;
+ }
+ }.asCharSink(charset).write(content);
+ }
}
/**
@@ -729,7 +736,14 @@ public class FileSystemUtils {
public static void writeLinesAs(Path file, Charset charset, Iterable<String> lines)
throws IOException {
createDirectoryAndParents(file.getParentDirectory());
- asByteSink(file).asCharSink(charset).writeLines(lines);
+ try (OutputStream out = file.getOutputStream()) {
+ new ByteSink() {
+ @Override
+ public OutputStream openStream() throws IOException {
+ return out;
+ }
+ }.asCharSink(charset).writeLines(lines);
+ }
}
/**
@@ -740,7 +754,14 @@ public class FileSystemUtils {
public static void appendLinesAs(Path file, Charset charset, Iterable<String> lines)
throws IOException {
createDirectoryAndParents(file.getParentDirectory());
- asByteSink(file, true).asCharSink(charset).writeLines(lines);
+ try (OutputStream out = file.getOutputStream(true)) {
+ new ByteSink() {
+ @Override
+ public OutputStream openStream() throws IOException {
+ return out;
+ }
+ }.asCharSink(charset).writeLines(lines);
+ }
}
/**
@@ -749,7 +770,15 @@ public class FileSystemUtils {
* @throws IOException if there was an error
*/
public static void writeContent(Path outputFile, byte[] content) throws IOException {
- asByteSink(outputFile).write(content);
+ try (OutputStream out = outputFile.getOutputStream()) {
+ new ByteSink() {
+ @Override
+ public OutputStream openStream() throws IOException {
+ return out;
+ }
+ }.write(content);
+ ;
+ }
}
/**