Skip to content

Commit

Permalink
Merge pull request #9 from upfluence/avoid-race-condition-on-restart-…
Browse files Browse the repository at this point in the history
…and-process-watching

Avoid race condition on the runner's process watcher
  • Loading branch information
AlexisMontagne committed Dec 30, 2015
2 parents 205b24c + 358c695 commit 655ffa9
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions etcdenv/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type Runner struct {
Command []string
DefaultEnv []string
Command []string
DefaultEnv []string
IsRestarting bool

cmd *exec.Cmd
}
Expand Down Expand Up @@ -63,6 +64,9 @@ func (r *Runner) Stop() error {
}

func (r *Runner) Restart(envVariables map[string]string) error {
r.IsRestarting = true
defer func() { r.IsRestarting = false }()

r.Stop()

return r.Start(envVariables)
Expand All @@ -79,15 +83,21 @@ func (r *Runner) Wait() error {
}

func (r *Runner) WatchProcess(exitStatus chan int) {
time.Sleep(200 * time.Millisecond)
err := r.Wait()
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitStatus <- status.ExitStatus()
} else {
exitStatus <- 0
for {
time.Sleep(200 * time.Millisecond)
err := r.Wait()

if !r.IsRestarting {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitStatus <- status.ExitStatus()
} else {
exitStatus <- 0
}
} else {
exitStatus <- 0
}
break
}
} else {
exitStatus <- 0
}
}

0 comments on commit 655ffa9

Please sign in to comment.