Skip to content

Commit

Permalink
gps: avoid race condition in variable assignment
Browse files Browse the repository at this point in the history
Instead, we use the existing `waitDone` channel to wait for the
command to return and then clean up the timer unconditionally. This
may hold the goroutine open for longer than strictly necessary, but
that seems harmless.

Closes golang#1194.
  • Loading branch information
tamird authored and zknill committed Oct 6, 2017
1 parent 702f3c0 commit 1c6d75f
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions internal/gps/cmd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ func (c cmd) CombinedOutput() ([]byte, error) {
return nil, err
}

var t *time.Timer
defer func() {
if t != nil {
t.Stop()
}
}()
// Adapted from (*os/exec.Cmd).Start
waitDone := make(chan struct{})
defer close(waitDone)
Expand All @@ -68,7 +62,9 @@ func (c cmd) CombinedOutput() ([]byte, error) {
// immediately to hard kill.
c.cancel()
} else {
t = time.AfterFunc(time.Minute, c.cancel)
stopCancel := time.AfterFunc(time.Minute, c.cancel).Stop
<-waitDone
stopCancel()
}
case <-waitDone:
}
Expand Down

0 comments on commit 1c6d75f

Please sign in to comment.