Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #498 from bergwolf/streamcopy
Browse files Browse the repository at this point in the history
read until EOF in streamCopy
  • Loading branch information
laijs authored May 8, 2017
2 parents c6dd76c + 7339655 commit 9a99c10
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hypervisor/vm_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func streamCopy(tty *TtyIO, stdinPipe io.WriteCloser, stdoutPipe, stderrPipe io.
var wg sync.WaitGroup
// old way cleanup all(expect stdinPipe) no matter what kinds of fails, TODO: change it
var once sync.Once
// cleanup closes tty.Stdin and thus terminates the first go routine
cleanup := func() {
tty.Close()
// stdinPipe is directly closed in the first go routine
Expand All @@ -181,30 +182,32 @@ func streamCopy(tty *TtyIO, stdinPipe io.WriteCloser, stdoutPipe, stderrPipe io.
}
}
if tty.Stdin != nil {
wg.Add(1)
go func() {
_, err := io.Copy(stdinPipe, tty.Stdin)
stdinPipe.Close()
if err != nil {
// we should not call cleanup when tty.Stdin reaches EOF
once.Do(cleanup)
}
wg.Done()
}()
}
if tty.Stdout != nil {
wg.Add(1)
go func() {
io.Copy(tty.Stdout, stdoutPipe)
once.Do(cleanup)
_, err := io.Copy(tty.Stdout, stdoutPipe)
if err != nil {
once.Do(cleanup)
}
wg.Done()
}()
}
if tty.Stderr != nil && stderrPipe != nil {
wg.Add(1)
go func() {
io.Copy(tty.Stderr, stderrPipe)
once.Do(cleanup)
_, err := io.Copy(tty.Stderr, stderrPipe)
if err != nil {
once.Do(cleanup)
}
wg.Done()
}()
}
Expand Down

0 comments on commit 9a99c10

Please sign in to comment.