@@ -2526,13 +2526,30 @@ impl UChild {
25262526 self . close_stdin ( ) ;
25272527 eprintln ! ( "[TIMING] after close_stdin: {:?}" , start. elapsed( ) ) ;
25282528
2529+ // If we have custom captured output, we must use wait() instead of wait_with_output()
2530+ // to avoid a race condition where wait_with_output()'s internal reader threads
2531+ // close the pipes while the child process is still writing to them, causing SIGPIPE.
2532+ // The custom CapturedOutput threads will handle reading the output.
2533+ let has_custom_capture = self . captured_stdout . is_some ( ) || self . captured_stderr . is_some ( ) ;
2534+
25292535 let output = if let Some ( timeout) = self . timeout {
2530- let child = self . raw ;
2536+ let mut child = self . raw ;
25312537
25322538 let ( sender, receiver) = mpsc:: channel ( ) ;
25332539 let handle = thread:: Builder :: new ( )
25342540 . name ( "wait_with_output" . to_string ( ) )
2535- . spawn ( move || sender. send ( child. wait_with_output ( ) ) )
2541+ . spawn ( move || {
2542+ if has_custom_capture {
2543+ // Use wait() to avoid internal reader threads that would race with our custom captures
2544+ sender. send ( child. wait ( ) . map ( |status| Output {
2545+ status,
2546+ stdout : Vec :: new ( ) ,
2547+ stderr : Vec :: new ( ) ,
2548+ } ) )
2549+ } else {
2550+ sender. send ( child. wait_with_output ( ) )
2551+ }
2552+ } )
25362553 . unwrap ( ) ;
25372554
25382555 match receiver. recv_timeout ( timeout) {
@@ -2552,8 +2569,20 @@ impl UChild {
25522569 }
25532570 }
25542571 } else {
2555- eprintln ! ( "[TIMING] calling wait_with_output: {:?}" , start. elapsed( ) ) ;
2556- self . raw . wait_with_output ( )
2572+ eprintln ! (
2573+ "[TIMING] calling wait/wait_with_output: {:?}" ,
2574+ start. elapsed( )
2575+ ) ;
2576+ if has_custom_capture {
2577+ // Use wait() to avoid internal reader threads that would race with our custom captures
2578+ self . raw . wait ( ) . map ( |status| Output {
2579+ status,
2580+ stdout : Vec :: new ( ) ,
2581+ stderr : Vec :: new ( ) ,
2582+ } )
2583+ } else {
2584+ self . raw . wait_with_output ( )
2585+ }
25572586 } ;
25582587
25592588 eprintln ! ( "[TIMING] after wait_with_output: {:?}" , start. elapsed( ) ) ;
0 commit comments