From 3ff46369b93cdd734668d36945cbdda8876d50d2 Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Tue, 28 May 2024 18:58:55 +0700 Subject: [PATCH] grader: try to clean up Isolate box if already exists when init-ed (#633) --- .../sandboxes/isolate/IsolateSandbox.java | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/judgels-backends/judgels-grader-engines/src/main/java/judgels/gabriel/sandboxes/isolate/IsolateSandbox.java b/judgels-backends/judgels-grader-engines/src/main/java/judgels/gabriel/sandboxes/isolate/IsolateSandbox.java index d020777cf..f9abfe756 100644 --- a/judgels-backends/judgels-grader-engines/src/main/java/judgels/gabriel/sandboxes/isolate/IsolateSandbox.java +++ b/judgels-backends/judgels-grader-engines/src/main/java/judgels/gabriel/sandboxes/isolate/IsolateSandbox.java @@ -277,22 +277,34 @@ public SandboxExecutionResult getResult(int exitCode) { } private void initIsolate() { - ImmutableList.Builder 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 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); } } @@ -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) {