Skip to content

Commit 68e4f1f

Browse files
committed
Make sure we wait for stdout/stderr pipes to be closed before calling runner.Wait()
1 parent 586d14f commit 68e4f1f

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

client.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ type Client struct {
104104
// goroutines.
105105
clientWaitGroup sync.WaitGroup
106106

107-
// stderrWaitGroup is used to prevent the command's Wait() function from
108-
// being called before we've finished reading from the stderr pipe.
109-
stderrWaitGroup sync.WaitGroup
107+
// pipesWaitGroup is used to prevent the command's Wait() function from
108+
// being called before we've finished reading from the stdout and stderr pipe.
109+
pipesWaitGroup sync.WaitGroup
110110

111111
// processKilled is used for testing only, to flag when the process was
112112
// forcefully killed.
@@ -756,8 +756,8 @@ func (c *Client) Start() (addr net.Addr, err error) {
756756

757757
// Start goroutine that logs the stderr
758758
c.clientWaitGroup.Add(1)
759-
c.stderrWaitGroup.Add(1)
760-
// logStderr calls Done()
759+
c.pipesWaitGroup.Add(1)
760+
// logStderr calls c.pipesWaitGroup.Done()
761761
go c.logStderr(runner.Name(), runner.Stderr())
762762

763763
c.clientWaitGroup.Add(1)
@@ -767,9 +767,9 @@ func (c *Client) Start() (addr net.Addr, err error) {
767767

768768
defer c.clientWaitGroup.Done()
769769

770-
// wait to finish reading from stderr since the stderr pipe reader
770+
// wait to finish reading from stdout/stderr since the stdout/stderr pipe readers
771771
// will be closed by the subsequent call to cmd.Wait().
772-
c.stderrWaitGroup.Wait()
772+
c.pipesWaitGroup.Wait()
773773

774774
// Wait for the command to end.
775775
err := runner.Wait(context.Background())
@@ -792,8 +792,10 @@ func (c *Client) Start() (addr net.Addr, err error) {
792792
// out of stdout
793793
linesCh := make(chan string)
794794
c.clientWaitGroup.Add(1)
795+
c.pipesWaitGroup.Add(1)
795796
go func() {
796797
defer c.clientWaitGroup.Done()
798+
defer c.pipesWaitGroup.Done()
797799
defer close(linesCh)
798800

799801
scanner := bufio.NewScanner(runner.Stdout())
@@ -1159,7 +1161,7 @@ func (c *Client) getGRPCMuxer(addr net.Addr) (*grpcmux.GRPCClientMuxer, error) {
11591161

11601162
func (c *Client) logStderr(name string, r io.Reader) {
11611163
defer c.clientWaitGroup.Done()
1162-
defer c.stderrWaitGroup.Done()
1164+
defer c.pipesWaitGroup.Done()
11631165
l := c.logger.Named(filepath.Base(name))
11641166

11651167
reader := bufio.NewReaderSize(r, c.config.PluginLogBufferSize)

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ this line is short
15031503

15041504
reader := strings.NewReader(msg)
15051505

1506-
c.stderrWaitGroup.Add(1)
1506+
c.pipesWaitGroup.Add(1)
15071507
c.logStderr(c.config.Cmd.Path, reader)
15081508
read := stderr.String()
15091509

@@ -1531,7 +1531,7 @@ func TestClient_logStderrParseJSON(t *testing.T) {
15311531
{"@message": "this is a large message that is more than 64 bytes long", "@level": "info"}`
15321532
reader := strings.NewReader(msg)
15331533

1534-
c.stderrWaitGroup.Add(1)
1534+
c.pipesWaitGroup.Add(1)
15351535
c.logStderr(c.config.Cmd.Path, reader)
15361536
logs := strings.Split(strings.TrimSpace(logBuf.String()), "\n")
15371537

0 commit comments

Comments
 (0)