Description: fix FTBFS with Go 1.10
Origin: vendor
Author: Brice Figureau <brice@daysofwonder.com>
Last-Update: 2018-02-25

---

From 8934d57803f8e9072807336affe550ce217e8d71 Mon Sep 17 00:00:00 2001
From: Brice Figureau <brice@daysofwonder.com>
Date: Sat, 24 Feb 2018 16:51:42 +0100
Subject: [PATCH] Fix #77 - fix RunWithString race condition

This doesn't happen with Go < 1.10, but there was a race condition in
`RunWithString` and `RunWithInput` which had already been fixed in `Run`.
It was possible for the function to return while the `stdout` or `stderr`
copy was still in progress. Since `bytes.Buffer` is not thread safe, the
`outwriter.String()` could see an intermediate buffer state while it
was growing, leading to strange results.

diff --git a/client.go b/client.go
index 732dd61..c195151 100644
--- a/client.go
+++ b/client.go
@@ -152,10 +152,20 @@ func (c *Client) RunWithString(command string, stdin string) (string, string, in
 	}
 
 	var outWriter, errWriter bytes.Buffer
-	go io.Copy(&outWriter, cmd.Stdout)
-	go io.Copy(&errWriter, cmd.Stderr)
+	var wg sync.WaitGroup
+	wg.Add(2)
+	go func() {
+		defer wg.Done()
+		io.Copy(&outWriter, cmd.Stdout)
+	}()
+
+	go func() {
+		defer wg.Done()
+		io.Copy(&errWriter, cmd.Stderr)
+	}()
 
 	cmd.Wait()
+	wg.Wait()
 
 	return outWriter.String(), errWriter.String(), cmd.ExitCode(), cmd.err
 }
@@ -176,11 +186,24 @@ func (c Client) RunWithInput(command string, stdout, stderr io.Writer, stdin io.
 		return 1, err
 	}
 
-	go io.Copy(cmd.Stdin, stdin)
-	go io.Copy(stdout, cmd.Stdout)
-	go io.Copy(stderr, cmd.Stderr)
+	var wg sync.WaitGroup
+	wg.Add(3)
+
+	go func() {
+		defer wg.Done()
+		io.Copy(cmd.Stdin, stdin)
+	}()
+	go func() {
+		defer wg.Done()
+		io.Copy(stdout, cmd.Stdout)
+	}()
+	go func() {
+		defer wg.Done()
+		io.Copy(stderr, cmd.Stderr)
+	}()
 
 	cmd.Wait()
+	wg.Wait()
 
 	return cmd.ExitCode(), cmd.err
 
