@@ -186,19 +186,25 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
186186 reason = TerminationReason .IO_FAILURE ;
187187 }
188188 while (reason == null ) {
189+ IOException readFailure = readerFailure (stdoutReader , stderrReader );
189190 if (out .isTruncated () || err .isTruncated ()) {
190191 reason = TerminationReason .OUTPUT_LIMIT ;
191192 } else if (Thread .interrupted ()) {
192193 interrupted = true ;
193194 reason = TerminationReason .CANCELLED ;
194195 } else if (request .cancellationToken .isCancelled ()) {
195196 reason = TerminationReason .CANCELLED ;
196- } else if (Objects .nonNull (stdoutReader .failure ) || Objects .nonNull (stderrReader .failure )) {
197- ioFailure = Objects .nonNull (stdoutReader .failure )
198- ? stdoutReader .failure : stderrReader .failure ;
197+ } else if (Objects .nonNull (readFailure )) {
198+ ioFailure = readFailure ;
199199 reason = TerminationReason .IO_FAILURE ;
200200 } else if (!process .isAlive () && !stdoutReader .isAlive () && !stderrReader .isAlive ()) {
201- reason = watchdog .killedProcess () ? TerminationReason .EXECUTION_TIMEOUT : TerminationReason .PROCESS_EXIT ;
201+ // A reader can publish its failure between the earlier check and thread exit.
202+ ioFailure = readerFailure (stdoutReader , stderrReader );
203+ if (Objects .nonNull (ioFailure )) {
204+ reason = TerminationReason .IO_FAILURE ;
205+ } else {
206+ reason = watchdog .killedProcess () ? TerminationReason .EXECUTION_TIMEOUT : TerminationReason .PROCESS_EXIT ;
207+ }
202208 } else if (watchdog .killedProcess () || remaining (request .submittedAtNanos , budget ) <= 0 ) {
203209 reason = TerminationReason .EXECUTION_TIMEOUT ;
204210 } else {
@@ -464,6 +470,11 @@ public synchronized void write(byte[] bytes, int offset, int length) {
464470 synchronized void freeze () { frozen = true ; }
465471 }
466472
473+ private static IOException readerFailure (Reader stdout , Reader stderr ) {
474+ IOException failure = stdout .failure ;
475+ return Objects .nonNull (failure ) ? failure : stderr .failure ;
476+ }
477+
467478 private static final class Reader extends Thread {
468479 private final InputStream input ;
469480 private final BoundedCapture capture ;
@@ -498,6 +509,8 @@ public static final class ExecutionRequest {
498509 private final File workingDirectory ;
499510 private final Map <String , String > environment ;
500511 private final long timeoutMillis ;
512+ /** Requested legacy ceiling, or zero for independently configured stream limits. */
513+ private final long maxOutputBytes ;
501514 private final int stdoutLimitBytes ;
502515 private final int stderrLimitBytes ;
503516 private final long cleanupGraceMillis ;
@@ -517,7 +530,7 @@ public ExecutionRequest(CommandLine commandLine, File workingDirectory,
517530 this (commandLine , workingDirectory , environment , timeoutMillis ,
518531 legacyLimit (maxOutputBytes , DEFAULT_STDOUT_LIMIT ),
519532 legacyLimit (maxOutputBytes , DEFAULT_STDERR_LIMIT ), WAIT_GRACE_MILLIS ,
520- System .nanoTime (), new OpenCliCancellationToken ());
533+ System .nanoTime (), new OpenCliCancellationToken (), maxOutputBytes );
521534 }
522535
523536 private static int legacyLimit (long maxOutputBytes , int defaultLimit ) {
@@ -528,6 +541,14 @@ public ExecutionRequest(CommandLine commandLine, File workingDirectory,
528541 Map <String , String > environment , long timeoutMillis , int stdoutLimitBytes ,
529542 int stderrLimitBytes , long cleanupGraceMillis , long submittedAtNanos ,
530543 OpenCliCancellationToken cancellationToken ) {
544+ this (commandLine , workingDirectory , environment , timeoutMillis , stdoutLimitBytes ,
545+ stderrLimitBytes , cleanupGraceMillis , submittedAtNanos , cancellationToken , 0L );
546+ }
547+
548+ private ExecutionRequest (CommandLine commandLine , File workingDirectory ,
549+ Map <String , String > environment , long timeoutMillis , int stdoutLimitBytes ,
550+ int stderrLimitBytes , long cleanupGraceMillis , long submittedAtNanos ,
551+ OpenCliCancellationToken cancellationToken , long maxOutputBytes ) {
531552 this .commandLine = Objects .requireNonNull (commandLine , "commandLine" );
532553 List <String > argv = new ArrayList <>(Arrays .asList (commandLine .toStrings ()));
533554 for (int i = 0 ; i < argv .size (); i ++) {
@@ -542,6 +563,7 @@ public ExecutionRequest(CommandLine commandLine, File workingDirectory,
542563 throw new IllegalArgumentException ("stdout/stderr capture budgets must be positive" );
543564 }
544565 this .timeoutMillis = timeoutMillis ;
566+ this .maxOutputBytes = maxOutputBytes ;
545567 this .stdoutLimitBytes = stdoutLimitBytes ;
546568 this .stderrLimitBytes = stderrLimitBytes ;
547569 this .cleanupGraceMillis = cleanupGraceMillis ;
@@ -550,10 +572,41 @@ public ExecutionRequest(CommandLine commandLine, File workingDirectory,
550572 }
551573 }
552574
575+ /** Bounded stdout or stderr snapshot returned by the low-level execution API. */
576+ public static final class BoundedOutputStream extends OutputStream {
577+ private final BoundedCapture capture ;
578+
579+ private BoundedOutputStream (BoundedCapture capture ) {
580+ this .capture = capture ;
581+ }
582+
583+ @ Override
584+ public void write (int value ) {
585+ capture .write (value );
586+ }
587+
588+ @ Override
589+ public void write (byte [] bytes , int offset , int length ) {
590+ capture .write (bytes , offset , length );
591+ }
592+
593+ public byte [] toByteArray () {
594+ return capture .toByteArray ();
595+ }
596+
597+ public boolean isOverflowed () {
598+ return capture .isTruncated ();
599+ }
600+
601+ int bufferCapacity () {
602+ return capture .bufferCapacity ();
603+ }
604+ }
605+
553606 @ Getter
554607 public static final class RunSession {
555- private final ByteArrayOutputStream stdout ;
556- private final ByteArrayOutputStream stderr ;
608+ private final BoundedOutputStream stdout ;
609+ private final BoundedOutputStream stderr ;
557610 private final DefaultExecuteResultHandler handler ;
558611 private final ExecuteWatchdog watchdog ;
559612 private final long timeoutMillis ;
@@ -563,12 +616,12 @@ public static final class RunSession {
563616 /** Raw low-level I/O evidence, which can include local paths. Exclude it from default diagnostics. */
564617 private final IOException ioFailure ;
565618
566- RunSession (ByteArrayOutputStream stdout , ByteArrayOutputStream stderr ,
619+ RunSession (BoundedCapture stdout , BoundedCapture stderr ,
567620 DefaultExecuteResultHandler handler , ExecuteWatchdog watchdog , long timeoutMillis ,
568621 boolean waitTimedOut , OpenCliExecutionDetails executionDetails ,
569622 Integer observedExitCode , IOException ioFailure ) {
570- this .stdout = stdout ;
571- this .stderr = stderr ;
623+ this .stdout = new BoundedOutputStream ( stdout ) ;
624+ this .stderr = new BoundedOutputStream ( stderr ) ;
572625 this .handler = handler ;
573626 this .watchdog = watchdog ;
574627 this .timeoutMillis = timeoutMillis ;
0 commit comments