Skip to content

Commit 9b534ea

Browse files
larsrc-googlecopybara-github
authored andcommitted
Pepper the code that sets up sandboxes for workers and non-workers with interrupt checks.
This will make it faster to interrupt them under dynamic execution. PiperOrigin-RevId: 530574318 Change-Id: I98e8322858ce6b7bd67661bc23d105d7a82382a7
1 parent b1af8fc commit 9b534ea

File tree

11 files changed

+35
-18
lines changed

11 files changed

+35
-18
lines changed

src/main/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawn.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public String getMnemonic() {
9999
}
100100

101101
@Override
102-
public void createFileSystem() throws IOException {
102+
public void createFileSystem() throws IOException, InterruptedException {
103103
// First compute all the inputs and directories that we need. This is based only on
104104
// `workerFiles`, `inputs` and `outputs` and won't do any I/O.
105105
Set<PathFragment> inputsToCreate = new LinkedHashSet<>();
@@ -128,7 +128,7 @@ public void createFileSystem() throws IOException {
128128

129129
protected void filterInputsAndDirsToCreate(
130130
Set<PathFragment> inputsToCreate, LinkedHashSet<PathFragment> dirsToCreate)
131-
throws IOException {}
131+
throws IOException, InterruptedException {}
132132

133133
/**
134134
* Creates all inputs needed for this spawn's sandbox.
@@ -138,8 +138,11 @@ protected void filterInputsAndDirsToCreate(
138138
* @param inputs All the inputs for this spawn.
139139
*/
140140
void createInputs(Iterable<PathFragment> inputsToCreate, SandboxInputs inputs)
141-
throws IOException {
141+
throws IOException, InterruptedException {
142142
for (PathFragment fragment : inputsToCreate) {
143+
if (Thread.interrupted()) {
144+
throw new InterruptedException("Interrupted creating inputs");
145+
}
143146
Path key = sandboxExecRoot.getRelative(fragment);
144147
if (inputs.getFiles().containsKey(fragment)) {
145148
RootedPath fileDest = inputs.getFiles().get(fragment);

src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedSpawnRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public void createFileSystem() throws IOException {
316316
statisticsPath,
317317
spawn.getMnemonic()) {
318318
@Override
319-
public void createFileSystem() throws IOException {
319+
public void createFileSystem() throws IOException, InterruptedException {
320320
super.createFileSystem();
321321
writeConfig(
322322
sandboxConfigPath,

src/main/java/com/google/devtools/build/lib/sandbox/SandboxHelpers.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,14 @@ public static void cleanExisting(
132132
Set<PathFragment> inputsToCreate,
133133
Set<PathFragment> dirsToCreate,
134134
Path workDir)
135-
throws IOException {
135+
throws IOException, InterruptedException {
136136
// To avoid excessive scanning of dirsToCreate for prefix dirs, we prepopulate this set of
137137
// prefixes.
138138
Set<PathFragment> prefixDirs = new HashSet<>();
139139
for (PathFragment dir : dirsToCreate) {
140+
if (Thread.interrupted()) {
141+
throw new InterruptedException();
142+
}
140143
PathFragment parent = dir.getParentDirectory();
141144
while (parent != null && !prefixDirs.contains(parent)) {
142145
prefixDirs.add(parent);
@@ -157,9 +160,12 @@ private static void cleanRecursively(
157160
Set<PathFragment> dirsToCreate,
158161
Path workDir,
159162
Set<PathFragment> prefixDirs)
160-
throws IOException {
163+
throws IOException, InterruptedException {
161164
Path execroot = workDir.getParentDirectory();
162165
for (Dirent dirent : root.readdir(Symlinks.NOFOLLOW)) {
166+
if (Thread.interrupted()) {
167+
throw new InterruptedException();
168+
}
163169
Path absPath = root.getChild(dirent.getName());
164170
PathFragment pathRelativeToWorkDir;
165171
if (absPath.startsWith(workDir)) {
@@ -280,7 +286,8 @@ public static void createDirectoryAndParentsInSandboxRoot(
280286
* are disallowed, for stricter sandboxing.
281287
*/
282288
public static void createDirectories(
283-
Iterable<PathFragment> dirsToCreate, Path dir, boolean strict) throws IOException {
289+
Iterable<PathFragment> dirsToCreate, Path dir, boolean strict)
290+
throws IOException, InterruptedException {
284291
Set<Path> knownDirectories = new HashSet<>();
285292
// Add sandboxExecRoot and it's parent -- all paths must fall under the parent of
286293
// sandboxExecRoot and we know that sandboxExecRoot exists. This stops the recursion in
@@ -289,6 +296,9 @@ public static void createDirectories(
289296
knownDirectories.add(dir.getParentDirectory());
290297

291298
for (PathFragment path : dirsToCreate) {
299+
if (Thread.interrupted()) {
300+
throw new InterruptedException();
301+
}
292302
if (strict) {
293303
Preconditions.checkArgument(!path.isAbsolute(), path);
294304
if (path.containsUplevelReferences() && path.isMultiSegment()) {

src/main/java/com/google/devtools/build/lib/sandbox/SandboxedSpawn.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ default boolean useSubprocessTimeout() {
4141

4242
/**
4343
* Creates the sandboxed execution root, making all {@code inputs} available for reading, making
44-
* sure that the parent directories of all {@code outputs} and that all {@code writableDirs}
45-
* exist and can be written into.
44+
* sure that the parent directories of all {@code outputs} and that all {@code writableDirs} exist
45+
* and can be written into.
4646
*
4747
* @throws IOException
4848
*/
49-
void createFileSystem() throws IOException;
49+
void createFileSystem() throws IOException, InterruptedException;
5050

5151
/**
5252
* Moves all {@code outputs} to {@code execRoot} while keeping the directory structure.

src/main/java/com/google/devtools/build/lib/sandbox/SymlinkedSandboxedSpawn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public SymlinkedSandboxedSpawn(
6565
@Override
6666
public void filterInputsAndDirsToCreate(
6767
Set<PathFragment> inputsToCreate, LinkedHashSet<PathFragment> dirsToCreate)
68-
throws IOException {
68+
throws IOException, InterruptedException {
6969
boolean gotStash = SandboxStash.takeStashedSandbox(sandboxPath, mnemonic);
7070
sandboxExecRoot.createDirectoryAndParents();
7171
if (gotStash) {

src/main/java/com/google/devtools/build/lib/worker/SandboxedWorkerProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public boolean isSandboxed() {
6767
@Override
6868
public void prepareExecution(
6969
SandboxInputs inputFiles, SandboxOutputs outputs, Set<PathFragment> workerFiles)
70-
throws IOException {
70+
throws IOException, InterruptedException {
7171
workerMultiplexer.createSandboxedProcess(workDir, workerFiles, inputFiles);
7272

7373
sandboxDir.createDirectoryAndParents();

src/main/java/com/google/devtools/build/lib/worker/WorkerExecRoot.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public WorkerExecRoot(Path workDir, List<PathFragment> extraDirs) {
4646

4747
public void createFileSystem(
4848
Set<PathFragment> workerFiles, SandboxInputs inputs, SandboxOutputs outputs)
49-
throws IOException {
49+
throws IOException, InterruptedException {
5050
workDir.createDirectoryAndParents();
5151

5252
// First compute all the inputs and directories that we need. This is based only on
@@ -77,8 +77,11 @@ public void createFileSystem(
7777
}
7878

7979
static void createInputs(Iterable<PathFragment> inputsToCreate, SandboxInputs inputs, Path dir)
80-
throws IOException {
80+
throws IOException, InterruptedException {
8181
for (PathFragment fragment : inputsToCreate) {
82+
if (Thread.interrupted()) {
83+
throw new InterruptedException();
84+
}
8285
Path key = dir.getRelative(fragment);
8386
if (inputs.getFiles().containsKey(fragment)) {
8487
RootedPath fileDest = inputs.getFiles().get(fragment);

src/main/java/com/google/devtools/build/lib/worker/WorkerMultiplexer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ private synchronized void report(@Nullable String s) {
140140
* sets up the sandbox root dir with the required worker files.
141141
*/
142142
public synchronized void createSandboxedProcess(
143-
Path workDir, Set<PathFragment> workerFiles, SandboxInputs inputFiles) throws IOException {
143+
Path workDir, Set<PathFragment> workerFiles, SandboxInputs inputFiles)
144+
throws IOException, InterruptedException {
144145
// TODO: Make blaze clean remove the workdir.
145146
if (this.process == null) {
146147
// This should be a once-only operation.

src/main/java/com/google/devtools/build/lib/worker/WorkerProxy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void setReporter(EventHandler reporter) {
5959
@Override
6060
public void prepareExecution(
6161
SandboxInputs inputFiles, SandboxOutputs outputs, Set<PathFragment> workerFiles)
62-
throws IOException {
62+
throws IOException, InterruptedException {
6363
workerMultiplexer.createProcess(workDir);
6464
}
6565

src/test/java/com/google/devtools/build/lib/sandbox/SandboxHelpersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void atomicallyWriteVirtualInput_writesArbitraryVirtualInput() throws Exc
240240
}
241241

242242
@Test
243-
public void cleanExisting_updatesDirs() throws IOException {
243+
public void cleanExisting_updatesDirs() throws IOException, InterruptedException {
244244
RootedPath inputTxt =
245245
RootedPath.toRootedPath(
246246
Root.fromPath(scratch.getFileSystem().getPath("/")), PathFragment.create("hello.txt"));

0 commit comments

Comments
 (0)