Skip to content

Commit

Permalink
Merge pull request #215 from cosmtrek/fix_cannot_kill_issue
Browse files Browse the repository at this point in the history
Fix cannot kill issue
  • Loading branch information
xiantang authored Nov 24, 2021
2 parents 7ec1b3c + 3d5a93a commit 53fb783
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 @@ -424,6 +424,7 @@ func (e *Engine) runBin() error {
e.withLock(func() {
e.binRunning = true
})
e.mainDebug("running process pid %v", cmd.Process.Pid)

go func(cmd *exec.Cmd, stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser) {
defer func() {
Expand Down Expand Up @@ -472,25 +473,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 @@ -14,15 +14,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.WriteCloser, io.ReadCloser, io.ReadCloser, error) {
Expand Down

0 comments on commit 53fb783

Please sign in to comment.