diff options
author | laszlocsomor <laszlocsomor@google.com> | 2018-07-05 00:17:55 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-05 00:19:21 -0700 |
commit | 59f17d6e0550bf63a0b6ef182e2d63474e058ede (patch) | |
tree | 4cbec855321ead722363687b9983685e2b1bf648 /src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java | |
parent | b40b7e715307de416b786c1d95f6cf3a6b69c9d3 (diff) |
Bazel server: ensure InputStreams are closed
Use try-with-resources to ensure InputStreams that
we open via FileSystem.InputStream(path) are
closed.
Eagerly closing InputStreams avoids hanging on to
file handles until the garbage collector finalizes
the InputStream, 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: 203338148
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java index 8258dccfc0..01c016905f 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java @@ -268,12 +268,14 @@ public abstract class FileSystem { * @throws IOException if the digest could not be computed for any reason */ protected byte[] getDigest(final Path path, DigestHashFunction hashFunction) throws IOException { - return new ByteSource() { - @Override - public InputStream openStream() throws IOException { - return getInputStream(path); - } - }.hash(hashFunction.getHash()).asBytes(); + try (InputStream in = getInputStream(path)) { + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return in; + } + }.hash(hashFunction.getHash()).asBytes(); + } } /** |