From d0bf589f2716b3d139c210930371a684c6e158eb Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Mon, 20 Nov 2017 05:34:25 -0800 Subject: Add a random number to action temp dir Fix for #4035 @laszlocsomor Closes #4110. PiperOrigin-RevId: 176346381 --- .../build/lib/exec/local/LocalSpawnRunner.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java index c6892e6479..e33c010263 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java +++ b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java @@ -47,6 +47,7 @@ import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -130,15 +131,15 @@ public final class LocalSpawnRunner implements SpawnRunner { } private static Path createActionTemp(Path execRoot) throws IOException { - // Use this executor thread's ID as the directory name's suffix. - // The ID is safe for the following reasons: - // - being a thread ID, it's guaranteed to be unique among other action executor threads - // - this thread will only execute one action at a time, so there's no risk of concurrently - // running actions using the same temp directory. - // The next action that this thread executes can reuse the temp directory name. The only caveat - // is, if {@link #start} fails to delete directory after the action is done, the next - // action will see stale files in its temp directory. - String idStr = Long.toHexString(Thread.currentThread().getId()); + String idStr = + // Make the name unique among other executor threads. + Long.toHexString(Thread.currentThread().getId()) + + "_" + // Make the name unique among other temp directories that this thread has ever created. + // On Windows, file and directory deletion is asynchronous, meaning the previous temp + // directory name isn't immediately available for the next action that this thread runs. + // See https://github.com/bazelbuild/bazel/issues/4035 + + Long.toHexString(ThreadLocalRandom.current().nextLong()); Path result = execRoot.getRelative("tmp" + idStr); if (!result.exists() && !result.createDirectory()) { throw new IOException(String.format("Could not create temp directory '%s'", result)); -- cgit v1.2.3