@@ -371,17 +371,31 @@ func (pm *ProcessMonitor) setupOutputCapture() error {
371371 return fmt .Errorf ("failed to create stderr pipe: %w" , err )
372372 }
373373
374+ // Use a WaitGroup to ensure capture goroutines are ready before starting process
375+ var captureReady sync.WaitGroup
376+ captureReady .Add (2 ) // stdout + stderr
377+
374378 // Start output capture goroutines
375- go pm .captureOutput (stdoutPipe , & pm .stdoutBuf , "stdout" )
376- go pm .captureOutput (stderrPipe , & pm .stderrBuf , "stderr" )
379+ go pm .captureOutput (stdoutPipe , & pm .stdoutBuf , "stdout" , & captureReady )
380+ go pm .captureOutput (stderrPipe , & pm .stderrBuf , "stderr" , & captureReady )
381+
382+ // Wait for both capture goroutines to be ready
383+ captureReady .Wait ()
384+ pm .logger .Debug ("Output capture goroutines ready" )
377385
378386 return nil
379387}
380388
381389// captureOutput captures output from a pipe
382- func (pm * ProcessMonitor ) captureOutput (pipe io.ReadCloser , buf * strings.Builder , streamName string ) {
390+ func (pm * ProcessMonitor ) captureOutput (pipe io.ReadCloser , buf * strings.Builder , streamName string , ready * sync. WaitGroup ) {
383391 defer pipe .Close ()
384392
393+ // Signal that this goroutine is ready to read
394+ if ready != nil {
395+ pm .logger .Debug ("Output capture goroutine starting" , "stream" , streamName )
396+ ready .Done ()
397+ }
398+
385399 scanner := bufio .NewScanner (pipe )
386400 for scanner .Scan () {
387401 line := scanner .Text ()
0 commit comments