Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use channels and non-blocking Wait to fix a potential race. #15

Merged
merged 1 commit into from
Dec 31, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"path"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand All @@ -36,8 +36,7 @@ const setpgidName = "Setpgid"

var (
hasSetPGID bool
pid = -1
pidLock sync.Mutex
killChan = make(chan time.Time, 1)
)

type ui interface {
Expand Down Expand Up @@ -126,35 +125,58 @@ func run(ui ui) time.Time {
cmd.SysProcAttr = &attr
}
io.WriteString(out, strings.Join(flag.Args(), " ")+"\n")
start := time.Now()
if err := cmd.Start(); err != nil {
io.WriteString(out, err.Error()+"\n")
}
pidLock.Lock()
pid = cmd.Process.Pid
pidLock.Unlock()
if err := cmd.Wait(); err != nil {
io.WriteString(out, err.Error()+"\n")
if s := wait(start, cmd); s != 0 {
io.WriteString(out, "exit status "+strconv.Itoa(s)+"\n")
}
// Technically a race between the process finishing an setting pid = -1. ☹
pidLock.Lock()
pid = -1
pidLock.Unlock()
io.WriteString(out, time.Now().String()+"\n")
})

return time.Now()
}

func kill() {
pidLock.Lock()
p := pid
pidLock.Unlock()
if p >= 0 {
if hasSetPGID {
p = -p
func wait(start time.Time, cmd *exec.Cmd) int {
var n int
tick := time.Tick(5 * time.Millisecond)
for {
select {
case t := <-killChan:
if t.Before(start) {
continue
}
p := cmd.Process.Pid
if hasSetPGID {
p = -p
}
if n == 0 {
debugPrint("Sending SIGTERM")
syscall.Kill(p, syscall.SIGTERM)
} else {
debugPrint("Sending SIGKILL")
syscall.Kill(p, syscall.SIGKILL)
}
n++

case <-tick:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a ticker because the busy loop bothered me. Does this actually matter? The syscall is going to move the Go routine and park it on a new thread. That probably takes longer than the ticker in the first place, right?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, 5ms is pretty quick. Should I make it longer if we decide to keep it?

var status syscall.WaitStatus
p := cmd.Process.Pid
switch q, err := syscall.Wait4(p, &status, syscall.WNOHANG, nil); {
case err != nil:
panic(err)
case q > 0:
return status.ExitStatus()
}
}
debugPrint("Killing %d", p)
syscall.Kill(p, syscall.SIGTERM)
}
}

func kill() {
select {
case killChan <- time.Now():
debugPrint("Killing")
}
}

Expand Down