88import java .io .IOException ;
99import java .io .InputStream ;
1010import java .io .OutputStream ;
11+ import java .io .PrintStream ;
1112import java .nio .charset .StandardCharsets ;
1213import java .util .concurrent .CountDownLatch ;
1314import java .util .concurrent .TimeUnit ;
1617import org .apache .commons .exec .ExecuteWatchdog ;
1718import org .junit .jupiter .api .Test ;
1819import org .junit .jupiter .api .Timeout ;
20+ import org .junit .jupiter .api .parallel .Isolated ;
21+ import org .junit .jupiter .params .ParameterizedTest ;
22+ import org .junit .jupiter .params .provider .ValueSource ;
1923
2024import static org .junit .jupiter .api .Assertions .assertEquals ;
2125import static org .junit .jupiter .api .Assertions .assertFalse ;
2226import static org .junit .jupiter .api .Assertions .assertSame ;
2327import static org .junit .jupiter .api .Assertions .assertTrue ;
2428
2529/** Verifies that reader-thread I/O faults survive into the terminal result. */
30+ @ Isolated
2631@ Timeout (5 )
2732class SubprocessStreamFailureTest {
2833
@@ -44,6 +49,56 @@ void partialOutputAndReadFailureRemainVisibleAfterRootExit() throws Exception {
4449 assertTrue (session .getExecutionDetails ().isStreamsDrained ());
4550 }
4651
52+ @ ParameterizedTest
53+ @ ValueSource (booleans = {false , true })
54+ void uncheckedPipeFailureKeepsPartialOutputAndDoesNotEscapeTheReader (boolean duringClose ) throws Exception {
55+ String canary = "private-provider-pipe-fault" ;
56+ RuntimeException nativeFailure = new IllegalStateException (canary );
57+ InputStream output = new ByteArrayInputStream ("abc" .getBytes (StandardCharsets .UTF_8 )) {
58+ @ Override
59+ public synchronized int read (byte [] bytes , int offset , int length ) {
60+ int read = super .read (bytes , offset , length );
61+ if (read < 0 && !duringClose ) {
62+ throw nativeFailure ;
63+ }
64+ return read ;
65+ }
66+
67+ @ Override
68+ public void close () {
69+ if (duringClose ) {
70+ throw nativeFailure ;
71+ }
72+ }
73+ };
74+ SubprocessExecutionSupport .Runtime runtime = new SubprocessExecutionSupport .Runtime (1 ,
75+ builder -> new ExitedProcess (output ), timeout -> new IdleWatchdog ());
76+ SubprocessExecutionSupport .ExecutionRequest request = new SubprocessExecutionSupport .ExecutionRequest (
77+ CommandLine .parse ("fake" ), null , null , 1_000L , 32 , 32 , 100L ,
78+ System .nanoTime (), new OpenCliCancellationToken ());
79+
80+ ByteArrayOutputStream diagnostics = new ByteArrayOutputStream ();
81+ PrintStream previous = System .err ;
82+ SubprocessExecutionSupport .RunSession session ;
83+ try (PrintStream capture = new PrintStream (diagnostics , true , StandardCharsets .UTF_8 .name ())) {
84+ System .setErr (capture );
85+ try {
86+ session = runtime .execute (request );
87+ if (session .getHandler ().getException () != null ) {
88+ session .getHandler ().getException ().printStackTrace (capture );
89+ }
90+ } finally {
91+ System .setErr (previous );
92+ }
93+ }
94+
95+ assertEquals (TerminationReason .IO_FAILURE , session .getExecutionDetails ().getTerminationReason ());
96+ assertSame (nativeFailure , session .getIoFailure ().getCause ());
97+ assertEquals ("abc" , new String (session .getStdout ().toByteArray (), StandardCharsets .UTF_8 ));
98+ assertTrue (session .getExecutionDetails ().isStreamsDrained ());
99+ assertFalse (diagnostics .toString (StandardCharsets .UTF_8 .name ()).contains (canary ));
100+ }
101+
47102 @ Test
48103 void readerFailureDuringRootExitPollDoesNotReportSuccessfulCompletion () throws Exception {
49104 IOException readFailure = new IOException ("reader failed while root exit was observed" );
0 commit comments