Skip to content

Commit

Permalink
Merge pull request #78 from masterzen/bugfix/77-fix-race-condition
Browse files Browse the repository at this point in the history
Fix #77 - fix RunWithString race condition
  • Loading branch information
Brice Figureau authored Feb 24, 2018
2 parents a2df6b1 + 8934d57 commit 7e40f93
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ language: go
go:
- 1.5
- 1.6
- tip
- 1.7
- 1.8
- 1.9
- '1.10'
install:
- make deps
- go build -o winrm-cli
Expand Down
33 changes: 28 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down

0 comments on commit 7e40f93

Please sign in to comment.