Skip to content

Commit

Permalink
grader: try to clean up Isolate box if already exists when init-ed (#633
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fushar committed May 28, 2024
1 parent c5ec885 commit 3ff4636
Showing 1 changed file with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,34 @@ public SandboxExecutionResult getResult(int exitCode) {
}

private void initIsolate() {
ImmutableList.Builder<String> command = ImmutableList.builder();
command.add(isolatePath, "-b" + boxId);
command.add("--cg");
command.add("--init");

ProcessBuilder pb = new ProcessBuilder(command.build()).redirectErrorStream(true);

try {
ProcessExecutionResult result = SandboxExecutor.executeProcessBuilder(pb);
if (result.getExitCode() != 0) {
for (int tries = 0;; tries++) {
ImmutableList.Builder<String> command = ImmutableList.builder();
command.add(isolatePath, "-b" + boxId);
command.add("--cg");
command.add("--init");

ProcessBuilder pb = new ProcessBuilder(command.build()).redirectErrorStream(true);

try {
ProcessExecutionResult result = SandboxExecutor.executeProcessBuilder(pb);
if (result.getExitCode() == 0) {
boxDir = new File(result.getOutputLines().get(0), "box");
return;
}
if (tries < 1) {
String errorMessage = result.getOutputLines().isEmpty() ? "" : result.getOutputLines().get(0);
if (errorMessage.startsWith("Box already exists")) {
// Clean up the box,
cleanUpIsolate();

// and try initializing it again.
continue;
}
}
throw new SandboxException("Cannot initialize Isolate!");
} catch (IOException | InterruptedException e) {
throw new SandboxException(e);
}

boxDir = new File(result.getOutputLines().get(0), "box");
} catch (IOException | InterruptedException e) {
throw new SandboxException(e);
}
}

Expand All @@ -304,7 +316,7 @@ private void cleanUpIsolate() {
if (result.getExitCode() != 0) {
throw new SandboxException("Cannot clean up Isolate!");
}
if (boxDir.exists()) {
if (boxDir != null && boxDir.exists()) {
FileUtils.forceDelete(boxDir);
}
} catch (IOException | InterruptedException e) {
Expand Down

0 comments on commit 3ff4636

Please sign in to comment.