Skip to content

Commit 5e2cd13

Browse files
committed
fix: contain process setup failures after spawn
1 parent 177d782 commit 5e2cd13

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
172172
stdoutReader.start();
173173
stderrReader.start();
174174
long remaining = remaining(request.submittedAtNanos, budget);
175-
watchdog = watchdogFactory.create(
176-
Math.max(1L, TimeUnit.NANOSECONDS.toMillis(Math.max(0L, remaining))));
177175
try {
176+
watchdog = watchdogFactory.create(
177+
Math.max(1L, TimeUnit.NANOSECONDS.toMillis(Math.max(0L, remaining))));
178178
watchdog.start(process);
179179
} catch (RuntimeException ex) {
180-
ioFailure = new IOException("Failed to start execution watchdog", ex);
180+
ioFailure = new IOException("Failed to initialize execution watchdog", ex);
181181
reason = TerminationReason.IO_FAILURE;
182182
}
183183
while (reason == null) {
@@ -208,6 +208,9 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
208208
} catch (IOException ex) {
209209
ioFailure = ex;
210210
reason = process == null ? TerminationReason.SPAWN_FAILED : TerminationReason.IO_FAILURE;
211+
} catch (RuntimeException ex) {
212+
ioFailure = new IOException("Native process setup or execution failure", ex);
213+
reason = process == null ? TerminationReason.SPAWN_FAILED : TerminationReason.IO_FAILURE;
211214
} finally {
212215
try {
213216
try {

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,70 @@ void watchdogStartFailureStillCleansOwnedChildAndReleasesPermit() throws Excepti
5656
assertEquals(2, starts[0]);
5757
}
5858

59+
@Test
60+
void watchdogCreationFailureAfterSpawnStillCleansChildAndReleasesPermit() throws Exception {
61+
int[] starts = new int[1];
62+
int[] watchdogCreations = new int[1];
63+
FakeProcess[] last = new FakeProcess[1];
64+
SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1,
65+
builder -> {
66+
starts[0]++;
67+
last[0] = new FakeProcess(false);
68+
return last[0];
69+
}, timeout -> {
70+
if (++watchdogCreations[0] % 2 == 0) {
71+
throw new IllegalStateException("injected watchdog creation failure");
72+
}
73+
return new FakeWatchdog(false);
74+
});
75+
76+
for (int attempt = 0; attempt < 2; attempt++) {
77+
SubprocessExecutionSupport.ExecutionRequest request = new SubprocessExecutionSupport.ExecutionRequest(
78+
CommandLine.parse("fake"), null, null, 1000L, 16, 16, 200L,
79+
System.nanoTime(), new OpenCliCancellationToken());
80+
81+
SubprocessExecutionSupport.RunSession session = runtime.execute(request);
82+
83+
assertEquals(TerminationReason.IO_FAILURE,
84+
session.getExecutionDetails().getTerminationReason());
85+
assertInstanceOf(IllegalStateException.class, session.getIoFailure().getCause());
86+
assertEquals(CleanupState.ROOT_EXIT_CONFIRMED,
87+
session.getExecutionDetails().getCleanupState());
88+
assertFalse(last[0].isAlive());
89+
assertFalse(runtime.isQuarantined());
90+
}
91+
assertEquals(2, starts[0]);
92+
}
93+
94+
@Test
95+
void processStreamSetupFailureStillCleansChildAndReleasesPermit() throws Exception {
96+
int[] starts = new int[1];
97+
FakeProcess[] last = new FakeProcess[1];
98+
SubprocessExecutionSupport.Runtime runtime = new SubprocessExecutionSupport.Runtime(1,
99+
builder -> {
100+
starts[0]++;
101+
last[0] = new FakeProcess(false, false, true);
102+
return last[0];
103+
}, timeout -> new FakeWatchdog(false));
104+
105+
for (int attempt = 0; attempt < 2; attempt++) {
106+
SubprocessExecutionSupport.ExecutionRequest request = new SubprocessExecutionSupport.ExecutionRequest(
107+
CommandLine.parse("fake"), null, null, 1000L, 16, 16, 200L,
108+
System.nanoTime(), new OpenCliCancellationToken());
109+
110+
SubprocessExecutionSupport.RunSession session = runtime.execute(request);
111+
112+
assertEquals(TerminationReason.IO_FAILURE,
113+
session.getExecutionDetails().getTerminationReason());
114+
assertInstanceOf(IllegalStateException.class, session.getIoFailure().getCause());
115+
assertEquals(CleanupState.ROOT_EXIT_CONFIRMED,
116+
session.getExecutionDetails().getCleanupState());
117+
assertFalse(last[0].isAlive());
118+
assertFalse(runtime.isQuarantined());
119+
}
120+
assertEquals(2, starts[0]);
121+
}
122+
59123
@Test
60124
void unconfirmedForcedStopQuarantinesRuntimeWithoutFabricatingExit() throws Exception {
61125
OpenCliCancellationToken token = new OpenCliCancellationToken();
@@ -157,16 +221,27 @@ private static final class FakeProcess extends Process {
157221
private boolean alive = true;
158222

159223
private final boolean failForce;
224+
private final boolean failStreamSetup;
160225

161-
FakeProcess(boolean failDestroy) { this(failDestroy, false); }
226+
FakeProcess(boolean failDestroy) { this(failDestroy, false, false); }
162227

163228
FakeProcess(boolean failDestroy, boolean failForce) {
229+
this(failDestroy, failForce, false);
230+
}
231+
232+
FakeProcess(boolean failDestroy, boolean failForce, boolean failStreamSetup) {
164233
this.failDestroy = failDestroy;
165234
this.failForce = failForce;
235+
this.failStreamSetup = failStreamSetup;
166236
}
167237

168238
@Override
169-
public OutputStream getOutputStream() { return new ByteArrayOutputStream(); }
239+
public OutputStream getOutputStream() {
240+
if (failStreamSetup) {
241+
throw new IllegalStateException("injected process stream setup failure");
242+
}
243+
return new ByteArrayOutputStream();
244+
}
170245

171246
@Override
172247
public InputStream getInputStream() { return new ByteArrayInputStream(new byte[0]); }

0 commit comments

Comments
 (0)