Skip to content

Commit

Permalink
Wait for process instead of waiting for stdout to be closed (#28)
Browse files Browse the repository at this point in the history
* Wait for process instead of waiting for stdout to be closed

* Update pkg/ssh/ssh.go

Co-authored-by: Ramiro Berrelleza <[email protected]>

Co-authored-by: Ramiro Berrelleza <[email protected]>
  • Loading branch information
pchico83 and rberrelleza authored Apr 28, 2021
1 parent 9f2f161 commit 99de42c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,28 @@ func handlePTY(logger *log.Entry, cmd *exec.Cmd, s ssh.Session, ptyReq ssh.Pty,
}
}()

wg := &sync.WaitGroup{}
go func() {
io.Copy(f, s) // stdin
}()

wg.Add(1)
waitCh := make(chan struct{})
go func() {
defer wg.Done()
defer close(waitCh)
io.Copy(s, f) // stdout
}()

wg.Wait()
if err := cmd.Wait(); err != nil {
logger.WithError(err).Errorf("pty command failed while waiting")
return err
}

select {
case <-waitCh:
logger.Info("stdout finished")
case <-time.NewTicker(1 * time.Second).C:
logger.Info("stdout didn't finish after 1s")
}

return nil
}

Expand Down

0 comments on commit 99de42c

Please sign in to comment.