diff --git a/runner/engine.go b/runner/engine.go index ee81bf30..0a69fa10 100644 --- a/runner/engine.go +++ b/runner/engine.go @@ -31,6 +31,7 @@ type Engine struct { binStopCh chan bool exitCh chan bool + procKillWg sync.WaitGroup mu sync.RWMutex watchers uint fileChecksums *checksumMap @@ -478,8 +479,7 @@ func (e *Engine) runPostCmd() error { } func (e *Engine) runBin() error { - killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}, wg *sync.WaitGroup) { - defer wg.Done() + killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}) { select { // listen to binStopCh // cleanup() will close binStopCh when engine stop @@ -518,13 +518,11 @@ func (e *Engine) runBin() error { e.runnerLog("running...") go func() { - wg := sync.WaitGroup{} defer func() { select { case <-e.exitCh: e.mainDebug("exit in runBin") - wg.Wait() default: } }() @@ -536,6 +534,7 @@ func (e *Engine) runBin() error { case <-killCh: return default: + e.procKillWg.Add(1) command := strings.Join(append([]string{e.config.Build.Bin}, e.runArgs...), " ") cmd, stdout, stderr, _ := e.startCmd(command) processExit := make(chan struct{}) @@ -544,11 +543,10 @@ func (e *Engine) runBin() error { e.proxy.Reload() } - wg.Add(1) e.withLock(func() { close(e.binStopCh) e.binStopCh = make(chan bool) - go killFunc(cmd, stdout, stderr, killCh, processExit, &wg) + go killFunc(cmd, stdout, stderr, killCh, processExit) }) go func() { @@ -570,6 +568,7 @@ func (e *Engine) runBin() error { default: e.runnerLog("Process Exit with Code: %v", state.ExitCode()) } + e.procKillWg.Done() if !e.config.Build.Rerun { return @@ -621,7 +620,7 @@ func (e *Engine) cleanup() { } e.mainDebug("waiting for exit...") - + e.procKillWg.Wait() e.running = false e.mainDebug("exited") }