Skip to content

Commit 5e6a36c

Browse files
committed
Initialize OpenCLI watchdog only for spawned processes
1 parent 97a142d commit 5e6a36c

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎src/main/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupport.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ public static final class Runtime {
9595
private volatile boolean quarantined;
9696

9797
public Runtime(int maxConcurrent) {
98-
this(maxConcurrent, ProcessBuilder::start, timeoutMillis -> ExecuteWatchdog.builder()
99-
.setTimeout(Duration.ofMillis(timeoutMillis)).get());
98+
this(maxConcurrent, ProcessBuilder::start, SubprocessExecutionSupport::newWatchdog);
10099
}
101100

102101
Runtime(int maxConcurrent, ProcessStarter processStarter, WatchdogFactory watchdogFactory) {
@@ -120,7 +119,7 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
120119
Process process = null;
121120
Reader stdoutReader = null;
122121
Reader stderrReader = null;
123-
ExecuteWatchdog watchdog = watchdogFactory.create(request.timeoutMillis);
122+
ExecuteWatchdog watchdog = null;
124123
TerminationReason reason = null;
125124
IOException ioFailure = null;
126125
boolean acquired = false;
@@ -214,7 +213,9 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
214213
} finally {
215214
try {
216215
try {
217-
watchdog.stop();
216+
if (Objects.nonNull(watchdog)) {
217+
watchdog.stop();
218+
}
218219
} catch (RuntimeException ex) {
219220
cleanupOperationFailed = true;
220221
}
@@ -282,12 +283,18 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
282283
.stderrCapturedBytes(err.size()).stderrObservedBytes(err.observed()).stderrTruncated(err.isTruncated())
283284
.elapsedMillis(elapsedMillis(request.submittedAtNanos)).queueWaitMillis(queueWaitMillis)
284285
.descendantsExitConfirmed(false).build();
285-
return new RunSession(out, err, handler, watchdog, request.timeoutMillis,
286+
// Preserve the legacy RunSession watchdog view even when nothing was started.
287+
ExecuteWatchdog watchdogView = Objects.nonNull(watchdog) ? watchdog : newWatchdog(request.timeoutMillis);
288+
return new RunSession(out, err, handler, watchdogView, request.timeoutMillis,
286289
reason == TerminationReason.QUEUE_TIMEOUT || reason == TerminationReason.EXECUTION_TIMEOUT,
287290
details, exit, ioFailure);
288291
}
289292
}
290293

294+
private static ExecuteWatchdog newWatchdog(long timeoutMillis) {
295+
return ExecuteWatchdog.builder().setTimeout(Duration.ofMillis(timeoutMillis)).get();
296+
}
297+
291298
private static boolean alive(Thread thread) { return thread != null && thread.isAlive(); }
292299
private static boolean tryForceStop(Process process) {
293300
try {

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessCleanupFaultTest.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@ void watchdogStartFailureStillCleansOwnedChildAndReleasesPermit() throws Excepti
5959
@Test
6060
void watchdogCreationFailureAfterSpawnStillCleansChildAndReleasesPermit() throws Exception {
6161
int[] starts = new int[1];
62-
int[] watchdogCreations = new int[1];
6362
FakeProcess[] last = new FakeProcess[1];
6463
SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1,
6564
builder -> {
6665
starts[0]++;
6766
last[0] = new FakeProcess(false);
6867
return last[0];
6968
}, timeout -> {
70-
if (++watchdogCreations[0] % 2 == 0) {
71-
throw new IllegalStateException("injected watchdog creation failure");
72-
}
73-
return new FakeWatchdog(false);
69+
assertNotNull(last[0], "watchdog must be created after spawn");
70+
throw new IllegalStateException("injected watchdog creation failure");
7471
});
7572

7673
for (int attempt = 0; attempt < 2; attempt++) {
@@ -151,6 +148,24 @@ void unconfirmedForcedStopQuarantinesRuntimeWithoutFabricatingExit() throws Exce
151148
assertEquals(1, starts[0]);
152149
}
153150

151+
@Test
152+
void cancelledSubmissionDoesNotInitializeWatchdogOrSpawn() throws Exception {
153+
OpenCliCancellationToken token = new OpenCliCancellationToken();
154+
token.cancel();
155+
SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1,
156+
builder -> fail("cancelled submission must not spawn"),
157+
timeout -> fail("cancelled submission must not initialize a watchdog"));
158+
SubprocessExecutionSupport.ExecutionRequest request = new SubprocessExecutionSupport.ExecutionRequest(
159+
CommandLine.parse("fake"), null, null, 1000L, 16, 16, 200L, System.nanoTime(), token);
160+
161+
SubprocessExecutionSupport.RunSession session = runtime.execute(request);
162+
163+
assertEquals(TerminationReason.CANCELLED, session.getExecutionDetails().getTerminationReason());
164+
assertEquals(CleanupState.NOT_STARTED, session.getExecutionDetails().getCleanupState());
165+
assertNull(session.getObservedExitCode());
166+
assertNotNull(session.getWatchdog());
167+
}
168+
154169
private static void assertCleanupSurvives(boolean failStop, boolean failDestroy) throws Exception {
155170
OpenCliCancellationToken token = new OpenCliCancellationToken();
156171
AtomicReference<OpenCliCancellationToken> currentToken = new AtomicReference<>(token);

0 commit comments

Comments
 (0)