3030 * compatibility with the earlier low-level RunSession API.
3131 */
3232public final class SubprocessExecutionSupport {
33+ @ FunctionalInterface
34+ interface ProcessStarter {
35+ Process start (ProcessBuilder builder ) throws IOException ;
36+ }
37+
38+ @ FunctionalInterface
39+ interface WatchdogFactory {
40+ ExecuteWatchdog create (long timeoutMillis );
41+ }
42+
3343 public static final long WAIT_GRACE_MILLIS = 5_000L ;
3444 public static final int DEFAULT_STDOUT_LIMIT = 8 * 1024 * 1024 ;
3545 public static final int DEFAULT_STDERR_LIMIT = 2 * 1024 * 1024 ;
@@ -80,14 +90,23 @@ public static RunSession execute(ExecutionRequest request) throws IOException, I
8090 public static final class Runtime {
8191 private final int maxConcurrentExecutions ;
8292 private final Semaphore permits ;
93+ private final ProcessStarter processStarter ;
94+ private final WatchdogFactory watchdogFactory ;
8395 private volatile boolean quarantined ;
8496
8597 public Runtime (int maxConcurrent ) {
98+ this (maxConcurrent , ProcessBuilder ::start , timeoutMillis -> ExecuteWatchdog .builder ()
99+ .setTimeout (Duration .ofMillis (timeoutMillis )).get ());
100+ }
101+
102+ Runtime (int maxConcurrent , ProcessStarter processStarter , WatchdogFactory watchdogFactory ) {
86103 if (maxConcurrent < 0 ) {
87104 throw new IllegalArgumentException ("maxConcurrentExecutions must not be negative" );
88105 }
89106 maxConcurrentExecutions = maxConcurrent == 0 ? DEFAULT_MAX_CONCURRENT : maxConcurrent ;
90107 permits = new Semaphore (maxConcurrentExecutions , true );
108+ this .processStarter = Objects .requireNonNull (processStarter , "processStarter" );
109+ this .watchdogFactory = Objects .requireNonNull (watchdogFactory , "watchdogFactory" );
91110 }
92111
93112 public int getMaxConcurrentExecutions () { return maxConcurrentExecutions ; }
@@ -101,12 +120,12 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
101120 Process process = null ;
102121 Reader stdoutReader = null ;
103122 Reader stderrReader = null ;
104- ExecuteWatchdog watchdog = ExecuteWatchdog .builder ()
105- .setTimeout (Duration .ofMillis (request .timeoutMillis )).get ();
123+ ExecuteWatchdog watchdog = watchdogFactory .create (request .timeoutMillis );
106124 TerminationReason reason = null ;
107125 IOException ioFailure = null ;
108126 boolean acquired = false ;
109127 boolean interrupted = false ;
128+ boolean cleanupOperationFailed = false ;
110129 long queueWaitMillis = 0L ;
111130 try {
112131 while (!acquired && reason == null ) {
@@ -146,15 +165,15 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
146165 builder .environment ().clear ();
147166 builder .environment ().putAll (request .environment );
148167 }
149- process = builder .start ();
168+ process = processStarter .start (builder );
150169 process .getOutputStream ().close ();
151170 stdoutReader = new Reader (process .getInputStream (), out , "opencli-stdout" );
152171 stderrReader = new Reader (process .getErrorStream (), err , "opencli-stderr" );
153172 stdoutReader .start ();
154173 stderrReader .start ();
155174 long remaining = remaining (request .submittedAtNanos , budget );
156- watchdog = ExecuteWatchdog . builder (). setTimeout ( Duration . ofMillis (
157- Math .max (1L , TimeUnit .NANOSECONDS .toMillis (Math .max (0L , remaining ))))). get () ;
175+ watchdog = watchdogFactory . create (
176+ Math .max (1L , TimeUnit .NANOSECONDS .toMillis (Math .max (0L , remaining ))));
158177 watchdog .start (process );
159178 while (reason == null ) {
160179 if (out .isTruncated () || err .isTruncated ()) {
@@ -183,38 +202,53 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
183202 ioFailure = ex ;
184203 reason = process == null ? TerminationReason .SPAWN_FAILED : TerminationReason .IO_FAILURE ;
185204 } finally {
186- watchdog .stop ();
187- long cleanupStart = System .nanoTime ();
188- long cleanupBudget = nanos (request .cleanupGraceMillis , "cleanupGraceMillis" );
189- if (process != null ) {
190- if (process .isAlive ()) { process .destroy (); }
191- boolean forceSent = false ;
192- while (remaining (cleanupStart , cleanupBudget ) > 0
193- && (process .isAlive () || alive (stdoutReader ) || alive (stderrReader ))) {
194- if (Thread .interrupted ()) { interrupted = true ; }
195- if (process .isAlive () && !forceSent
196- && System .nanoTime () - cleanupStart >= Math .min (TimeUnit .MILLISECONDS .toNanos (100L ), cleanupBudget / 2 )) {
197- process .destroyForcibly ();
198- forceSent = true ;
205+ try {
206+ try {
207+ watchdog .stop ();
208+ } catch (RuntimeException ex ) {
209+ cleanupOperationFailed = true ;
210+ }
211+ long cleanupStart = System .nanoTime ();
212+ long cleanupBudget = nanos (request .cleanupGraceMillis , "cleanupGraceMillis" );
213+ if (Objects .nonNull (process )) {
214+ boolean forceSent = false ;
215+ if (process .isAlive ()) {
216+ try {
217+ process .destroy ();
218+ } catch (RuntimeException ex ) {
219+ cleanupOperationFailed = true ;
220+ forceSent = true ;
221+ if (!tryForceStop (process )) { cleanupOperationFailed = true ; }
222+ }
199223 }
200- try {
201- TimeUnit .NANOSECONDS .sleep (Math .min (POLL_NANOS ,
202- Math .max (1L , remaining (cleanupStart , cleanupBudget ))));
203- } catch (InterruptedException ex ) {
204- interrupted = true ;
224+ while (remaining (cleanupStart , cleanupBudget ) > 0
225+ && (process .isAlive () || alive (stdoutReader ) || alive (stderrReader ))) {
226+ if (Thread .interrupted ()) { interrupted = true ; }
227+ if (process .isAlive () && !forceSent
228+ && System .nanoTime () - cleanupStart >= Math .min (TimeUnit .MILLISECONDS .toNanos (100L ), cleanupBudget / 2 )) {
229+ if (!tryForceStop (process )) { cleanupOperationFailed = true ; }
230+ forceSent = true ;
231+ }
232+ try {
233+ TimeUnit .NANOSECONDS .sleep (Math .min (POLL_NANOS ,
234+ Math .max (1L , remaining (cleanupStart , cleanupBudget ))));
235+ } catch (InterruptedException ex ) {
236+ interrupted = true ;
237+ }
238+ }
239+ if (process .isAlive () && !tryForceStop (process )) { cleanupOperationFailed = true ; }
240+ if (process .isAlive () || alive (stdoutReader ) || alive (stderrReader )) {
241+ // A fixed runtime cannot accumulate unlimited uncertain children/readers.
242+ quarantined = true ;
243+ if (reason == TerminationReason .PROCESS_EXIT ) { reason = TerminationReason .CLEANUP_UNCONFIRMED ; }
205244 }
206245 }
207- if (process .isAlive ()) { process .destroyForcibly (); }
208- if (process .isAlive () || alive (stdoutReader ) || alive (stderrReader )) {
209- // A fixed runtime cannot accumulate unlimited uncertain children/readers.
210- quarantined = true ;
211- if (reason == TerminationReason .PROCESS_EXIT ) { reason = TerminationReason .CLEANUP_UNCONFIRMED ; }
212- }
246+ } finally {
247+ out .freeze ();
248+ err .freeze ();
249+ if (acquired ) { permits .release (); }
250+ if (interrupted ) { Thread .currentThread ().interrupt (); }
213251 }
214- out .freeze ();
215- err .freeze ();
216- if (acquired ) { permits .release (); }
217- if (interrupted ) { Thread .currentThread ().interrupt (); }
218252 }
219253 if (reason == TerminationReason .PROCESS_EXIT && (out .isTruncated () || err .isTruncated ())) {
220254 reason = TerminationReason .OUTPUT_LIMIT ;
@@ -233,6 +267,7 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
233267 .cleanupState (process == null ? CleanupState .NOT_STARTED
234268 : process .isAlive () ? CleanupState .UNCONFIRMED : CleanupState .ROOT_EXIT_CONFIRMED )
235269 .processStarted (process != null ).streamsDrained (!alive (stdoutReader ) && !alive (stderrReader ))
270+ .cleanupOperationFailed (cleanupOperationFailed )
236271 .stdoutCapturedBytes (out .size ()).stdoutObservedBytes (out .observed ()).stdoutTruncated (out .isTruncated ())
237272 .stderrCapturedBytes (err .size ()).stderrObservedBytes (err .observed ()).stderrTruncated (err .isTruncated ())
238273 .elapsedMillis (elapsedMillis (request .submittedAtNanos )).queueWaitMillis (queueWaitMillis )
@@ -244,6 +279,14 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
244279 }
245280
246281 private static boolean alive (Thread thread ) { return thread != null && thread .isAlive (); }
282+ private static boolean tryForceStop (Process process ) {
283+ try {
284+ process .destroyForcibly ();
285+ return true ;
286+ } catch (RuntimeException ex ) {
287+ return false ;
288+ }
289+ }
247290 private static long remaining (long start , long budget ) { return budget - (System .nanoTime () - start ); }
248291 private static long elapsedMillis (long start ) { return TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - start ); }
249292
0 commit comments