Skip to content

Commit

Permalink
use to kill process
Browse files Browse the repository at this point in the history
use  to kill process

use  to kill process
  • Loading branch information
xiantang committed Nov 24, 2021
1 parent 1995822 commit 3d5a93a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -473,25 +474,31 @@ func (e *Engine) cleanup() {
e.binStopCh <- true
}
})
e.mainDebug("wating for close watchers..")

e.withLock(func() {
for i := 0; i < int(e.watchers); i++ {
e.watcherStopCh <- true
}
})

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 {
e.mainLog("failed to delete tmp dir, err: %+v", err)
}
}

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

<-e.canExit
}

Expand Down
9 changes: 9 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
16 changes: 12 additions & 4 deletions runner/util_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3d5a93a

Please sign in to comment.