Fix issue where output could be truncated

This commit is contained in:
Mitchell Hashimoto 2014-10-03 13:02:16 -07:00
parent 2f2be4e936
commit 238ec05f2f
1 changed files with 28 additions and 5 deletions

33
main.go
View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"sync"
"github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -47,8 +48,9 @@ func realMain() int {
// Setup the prefixed readers that send data properly to // Setup the prefixed readers that send data properly to
// stdout/stderr. // stdout/stderr.
doneCh := make(chan struct{})
outR, outW := io.Pipe() outR, outW := io.Pipe()
go copyOutput(outR) go copyOutput(outR, doneCh)
// Create the configuration for panicwrap and wrap our executable // Create the configuration for panicwrap and wrap our executable
wrapConfig.Handler = panicHandler(logTempFile) wrapConfig.Handler = panicHandler(logTempFile)
@ -62,6 +64,12 @@ func realMain() int {
// If >= 0, we're the parent, so just exit // If >= 0, we're the parent, so just exit
if exitStatus >= 0 { if exitStatus >= 0 {
// Close the stdout writer so that our copy process can finish
outW.Close()
// Wait for the output copying to finish
<-doneCh
return exitStatus return exitStatus
} }
@ -175,7 +183,9 @@ func cliConfigFile() (string, error) {
// copyOutput uses output prefixes to determine whether data on stdout // copyOutput uses output prefixes to determine whether data on stdout
// should go to stdout or stderr. This is due to panicwrap using stderr // should go to stdout or stderr. This is due to panicwrap using stderr
// as the log and error channel. // as the log and error channel.
func copyOutput(r io.Reader) { func copyOutput(r io.Reader, doneCh chan<- struct{}) {
defer close(doneCh)
pr, err := prefixedio.NewReader(r) pr, err := prefixedio.NewReader(r)
if err != nil { if err != nil {
panic(err) panic(err)
@ -194,7 +204,20 @@ func copyOutput(r io.Reader) {
panic(err) panic(err)
} }
go io.Copy(os.Stderr, stderrR) var wg sync.WaitGroup
go io.Copy(os.Stdout, stdoutR) wg.Add(3)
go io.Copy(os.Stdout, defaultR) go func() {
defer wg.Done()
io.Copy(os.Stderr, stderrR)
}()
go func() {
defer wg.Done()
io.Copy(os.Stdout, stdoutR)
}()
go func() {
defer wg.Done()
io.Copy(os.Stdout, defaultR)
}()
wg.Wait()
} }