Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 379102b

Browse files
sdboyerkrisnova
authored andcommitted
Fix races in monitoredCmd
Fixes #221.
1 parent e7fc7d2 commit 379102b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

gps/cmd.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,30 @@ func (c *monitoredCmd) run(ctx context.Context) error {
4444
ticker := time.NewTicker(c.timeout)
4545
done := make(chan error, 1)
4646
defer ticker.Stop()
47-
go func() { done <- c.cmd.Run() }()
47+
48+
err := c.cmd.Start()
49+
if err != nil {
50+
return err
51+
}
52+
53+
go func() {
54+
done <- c.cmd.Wait()
55+
}()
4856

4957
for {
5058
select {
5159
case <-ticker.C:
5260
if c.hasTimedOut() {
53-
// On windows it is apparently (?) possible for the process
54-
// pointer to become nil without Run() having returned (and
55-
// thus, passing through the done channel). Guard against this.
56-
if c.cmd.Process != nil {
57-
if err := c.cmd.Process.Kill(); err != nil {
58-
return &killCmdError{err}
59-
}
61+
if err := c.cmd.Process.Kill(); err != nil {
62+
return &killCmdError{err}
6063
}
6164

6265
return &timeoutError{c.timeout}
6366
}
6467
case <-ctx.Done():
68+
if err := c.cmd.Process.Kill(); err != nil {
69+
return &killCmdError{err}
70+
}
6571
return ctx.Err()
6672
case err := <-done:
6773
return err

0 commit comments

Comments
 (0)