Skip to content

Commit 6b61fa9

Browse files
Implement graceful shutdown on Mac (#619)
* hold cleanup until procKilledCh signal * signal to procKilledChan in existing deferred func * without this can't exit when non zero exit code it seems like `os.Exit(state.ExitCode())` belongs here... but unclear on intent of `e.config.Build.Rerun` This at least lets you ctrl+c out of error condition instead of killing pid via external means. * Revert "without this can't exit when non zero exit code" This reverts commit fec348c. * use cleanup * fix test case rerun * upgrade gomod version * remove unused `WaitGroup` --------- Co-authored-by: Seth Brasile <seth.brasile@gmail.com>
1 parent 7d43628 commit 6b61fa9

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

runner/engine.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Engine struct {
3131
binStopCh chan bool
3232
exitCh chan bool
3333

34+
procKillWg sync.WaitGroup
3435
mu sync.RWMutex
3536
watchers uint
3637
fileChecksums *checksumMap
@@ -478,8 +479,7 @@ func (e *Engine) runPostCmd() error {
478479
}
479480

480481
func (e *Engine) runBin() error {
481-
killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}, wg *sync.WaitGroup) {
482-
defer wg.Done()
482+
killFunc := func(cmd *exec.Cmd, stdout io.ReadCloser, stderr io.ReadCloser, killCh chan struct{}, processExit chan struct{}) {
483483
select {
484484
// listen to binStopCh
485485
// cleanup() will close binStopCh when engine stop
@@ -518,13 +518,11 @@ func (e *Engine) runBin() error {
518518

519519
e.runnerLog("running...")
520520
go func() {
521-
wg := sync.WaitGroup{}
522521

523522
defer func() {
524523
select {
525524
case <-e.exitCh:
526525
e.mainDebug("exit in runBin")
527-
wg.Wait()
528526
default:
529527
}
530528
}()
@@ -536,6 +534,7 @@ func (e *Engine) runBin() error {
536534
case <-killCh:
537535
return
538536
default:
537+
e.procKillWg.Add(1)
539538
command := strings.Join(append([]string{e.config.Build.Bin}, e.runArgs...), " ")
540539
cmd, stdout, stderr, _ := e.startCmd(command)
541540
processExit := make(chan struct{})
@@ -544,11 +543,10 @@ func (e *Engine) runBin() error {
544543
e.proxy.Reload()
545544
}
546545

547-
wg.Add(1)
548546
e.withLock(func() {
549547
close(e.binStopCh)
550548
e.binStopCh = make(chan bool)
551-
go killFunc(cmd, stdout, stderr, killCh, processExit, &wg)
549+
go killFunc(cmd, stdout, stderr, killCh, processExit)
552550
})
553551

554552
go func() {
@@ -570,6 +568,7 @@ func (e *Engine) runBin() error {
570568
default:
571569
e.runnerLog("Process Exit with Code: %v", state.ExitCode())
572570
}
571+
e.procKillWg.Done()
573572

574573
if !e.config.Build.Rerun {
575574
return
@@ -621,7 +620,7 @@ func (e *Engine) cleanup() {
621620
}
622621

623622
e.mainDebug("waiting for exit...")
624-
623+
e.procKillWg.Wait()
625624
e.running = false
626625
e.mainDebug("exited")
627626
}

0 commit comments

Comments
 (0)