Skip to content

Commit 53fb783

Browse files
authored
Merge pull request #215 from cosmtrek/fix_cannot_kill_issue
Fix cannot kill issue
2 parents 7ec1b3c + 3d5a93a commit 53fb783

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

runner/engine.go

+7
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ func (e *Engine) runBin() error {
424424
e.withLock(func() {
425425
e.binRunning = true
426426
})
427+
e.mainDebug("running process pid %v", cmd.Process.Pid)
427428

428429
go func(cmd *exec.Cmd, stdin io.WriteCloser, stdout io.ReadCloser, stderr io.ReadCloser) {
429430
defer func() {
@@ -472,25 +473,31 @@ func (e *Engine) cleanup() {
472473
e.binStopCh <- true
473474
}
474475
})
476+
e.mainDebug("wating for close watchers..")
475477

476478
e.withLock(func() {
477479
for i := 0; i < int(e.watchers); i++ {
478480
e.watcherStopCh <- true
479481
}
480482
})
481483

484+
e.mainDebug("waiting for buildRun...")
482485
var err error
483486
if err = e.watcher.Close(); err != nil {
484487
e.mainLog("failed to close watcher, error: %s", err.Error())
485488
}
486489

490+
e.mainDebug("waiting for clean ...")
491+
487492
if e.config.Misc.CleanOnExit {
488493
e.mainLog("deleting %s", e.config.tmpPath())
489494
if err = os.RemoveAll(e.config.tmpPath()); err != nil {
490495
e.mainLog("failed to delete tmp dir, err: %+v", err)
491496
}
492497
}
493498

499+
e.mainDebug("waiting for exit...")
500+
494501
<-e.canExit
495502
}
496503

runner/util.go

+9
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,12 @@ func (a *checksumMap) updateFileChecksum(filename, newChecksum string) (ok bool)
270270
}
271271
return false
272272
}
273+
274+
// trying to force kill a process by it's pid.
275+
func killByPid(pid int) error {
276+
proc, err := os.FindProcess(pid)
277+
if err != nil {
278+
return err
279+
}
280+
return proc.Kill()
281+
}

runner/util_darwin.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
1414
if e.config.Build.SendInterrupt {
1515
// Sending a signal to make it clear to the process that it is time to turn off
1616
if err = syscall.Kill(-pid, syscall.SIGINT); err != nil {
17+
e.mainDebug("trying to send signal failed %v", err)
1718
return
1819
}
1920
time.Sleep(e.config.Build.KillDelay * time.Millisecond)
2021
}
21-
// https://stackoverflow.com/questions/22470193/why-wont-go-kill-a-child-process-correctly
22-
err = syscall.Kill(-pid, syscall.SIGKILL)
22+
err = killByPid(pid)
23+
if err != nil {
24+
return pid, err
25+
}
2326
// Wait releases any resources associated with the Process.
24-
_, _ = cmd.Process.Wait()
25-
return pid, err
27+
_, err = cmd.Process.Wait()
28+
if err != nil {
29+
return pid, err
30+
}
31+
32+
e.mainDebug("killed process pid %d successed", pid)
33+
return pid, nil
2634
}
2735

2836
func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.WriteCloser, io.ReadCloser, io.ReadCloser, error) {

0 commit comments

Comments
 (0)