From 3d5a93ab88db90c285118384569d0750ee150b14 Mon Sep 17 00:00:00 2001 From: xiantang Date: Wed, 24 Nov 2021 23:52:45 +0800 Subject: [PATCH] use to kill process use to kill process use to kill process --- runner/engine.go | 7 +++++++ runner/util.go | 9 +++++++++ runner/util_darwin.go | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/runner/engine.go b/runner/engine.go index 2fb6929a..bd48d85d 100644 --- a/runner/engine.go +++ b/runner/engine.go @@ -421,6 +421,7 @@ func (e *Engine) runBin() error { e.withLock(func() { e.binRunning = true }) + e.mainDebug("running process pid %v", cmd.Process.Pid) go func() { _, _ = io.Copy(os.Stdout, stdout) @@ -473,6 +474,7 @@ func (e *Engine) cleanup() { e.binStopCh <- true } }) + e.mainDebug("wating for close watchers..") e.withLock(func() { for i := 0; i < int(e.watchers); i++ { @@ -480,11 +482,14 @@ func (e *Engine) cleanup() { } }) + e.mainDebug("waiting for buildRun...") var err error if err = e.watcher.Close(); err != nil { e.mainLog("failed to close watcher, error: %s", err.Error()) } + e.mainDebug("waiting for clean ...") + if e.config.Misc.CleanOnExit { e.mainLog("deleting %s", e.config.tmpPath()) if err = os.RemoveAll(e.config.tmpPath()); err != nil { @@ -492,6 +497,8 @@ func (e *Engine) cleanup() { } } + e.mainDebug("waiting for exit...") + <-e.canExit } diff --git a/runner/util.go b/runner/util.go index c09d6c1e..b8f99ac2 100644 --- a/runner/util.go +++ b/runner/util.go @@ -270,3 +270,12 @@ func (a *checksumMap) updateFileChecksum(filename, newChecksum string) (ok bool) } return false } + +// trying to force kill a process by it's pid. +func killByPid(pid int) error { + proc, err := os.FindProcess(pid) + if err != nil { + return err + } + return proc.Kill() +} diff --git a/runner/util_darwin.go b/runner/util_darwin.go index 32f52cb6..c9e8a9b2 100644 --- a/runner/util_darwin.go +++ b/runner/util_darwin.go @@ -15,15 +15,23 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) { if e.config.Build.SendInterrupt { // Sending a signal to make it clear to the process that it is time to turn off if err = syscall.Kill(-pid, syscall.SIGINT); err != nil { + e.mainDebug("trying to send signal failed %v", err) return } time.Sleep(e.config.Build.KillDelay * time.Millisecond) } - // https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly - err = syscall.Kill(-pid, syscall.SIGKILL) + err = killByPid(pid) + if err != nil { + return pid, err + } // Wait releases any resources associated with the Process. - _, _ = cmd.Process.Wait() - return pid, err + _, err = cmd.Process.Wait() + if err != nil { + return pid, err + } + + e.mainDebug("killed process pid %d successed", pid) + return pid, nil } func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) {