Skip to content

Commit

Permalink
Implement graceful shutdown on Mac (#619)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
xiantang and sethbrasile authored Jun 24, 2024
1 parent 7d43628 commit 6b61fa9
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Engine struct {
binStopCh chan bool
exitCh chan bool

procKillWg sync.WaitGroup
mu sync.RWMutex
watchers uint
fileChecksums *checksumMap
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
}
}()
Expand All @@ -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{})
Expand All @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -621,7 +620,7 @@ func (e *Engine) cleanup() {
}

e.mainDebug("waiting for exit...")

e.procKillWg.Wait()
e.running = false
e.mainDebug("exited")
}
Expand Down

0 comments on commit 6b61fa9

Please sign in to comment.