From 67ae7a0b6c8d61897a6b5f8ee4f76375f79bdaed Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 3 May 2015 17:36:01 +0200 Subject: [PATCH] Wait for stdout/stderr to close (fixes #1754) --- cmd/syncthing/monitor.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/syncthing/monitor.go b/cmd/syncthing/monitor.go index 5182efab7..5555bf3e3 100644 --- a/cmd/syncthing/monitor.go +++ b/cmd/syncthing/monitor.go @@ -106,12 +106,24 @@ func monitorMain() { stdoutLastLines = make([]string, 0, 50) stdoutMut.Unlock() - go copyStderr(stderr, dst) - go copyStdout(stdout, dst) + wg := sync.NewWaitGroup() + + wg.Add(1) + go func() { + copyStderr(stderr, dst) + wg.Done() + }() + + wg.Add(1) + go func() { + copyStdout(stdout, dst) + wg.Done() + }() exit := make(chan error) go func() { + wg.Wait() exit <- cmd.Wait() }() @@ -149,7 +161,7 @@ func monitorMain() { } } -func copyStderr(stderr io.ReadCloser, dst io.Writer) { +func copyStderr(stderr io.Reader, dst io.Writer) { br := bufio.NewReader(stderr) var panicFd *os.File @@ -192,7 +204,7 @@ func copyStderr(stderr io.ReadCloser, dst io.Writer) { } } -func copyStdout(stdout io.ReadCloser, dst io.Writer) { +func copyStdout(stdout io.Reader, dst io.Writer) { br := bufio.NewReader(stdout) for { line, err := br.ReadString('\n')