diff --git a/executor/executor.go b/executor/executor.go index 4dadbbd..05baeaf 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -2,6 +2,7 @@ package executor import ( "bufio" + "bytes" "fmt" "io" "io/ioutil" @@ -354,6 +355,8 @@ func Run(path string, env []string, emitter screwdriver.Emitter, build screwdriv code = 3 } _ = c.Process.Signal(syscall.SIGABRT) + terminateSleep(shellBin, sourceDir, true) // kill all running sleep + case stepAbort := <-sig: f.Write([]byte{4}) if firstError == nil { @@ -361,6 +364,7 @@ func Run(path string, env []string, emitter screwdriver.Emitter, build screwdriv code = 1 } _ = c.Process.Signal(syscall.SIGABRT) + terminateSleep(shellBin, sourceDir, false) // kill all running sleep other than sleep $SD_TERMINATION_GRACE_PERIOD_SECS } if err := api.UpdateStepStop(buildID, cmd.Name, code); err != nil { @@ -396,6 +400,25 @@ func Run(path string, env []string, emitter screwdriver.Emitter, build screwdriv firstError = cmdErr } } - + terminateSleep(shellBin, sourceDir, true) // kill running sleep $SD_TERMINATION_GRACE_PERIOD_SECS return firstError } + +// terminate long running sleep process for abort, timeout, n after teardown steps +func terminateSleep(shellBin, sourceDir string, killAll bool) { + var stdout, stderr bytes.Buffer + shargs := []string{"-e", "-c"} + cmdStr := "pids=$(ps -ef | grep '[s]leep' | awk '{print $2}'); pidcnt=$(echo $pids | wc -w); if [ $pidcnt -gt 1 ]; then kill $(echo $pids | awk '{$NF=\"\"}1'); else echo $pids; fi;" + if killAll { + cmdStr = "pids=$(ps -ef | grep '[s]leep' | awk '{print $2}'); if [ ! -z $pids ]; then kill $pids; else echo $pids; fi;" + } + shargs = append(shargs, cmdStr) + c := exec.Command(shellBin, shargs...) + c.Stdout = &stdout + c.Stderr = &stderr + c.Dir = sourceDir + err := c.Run() + if err != nil || strings.TrimSpace(stderr.String()) != "" { + fmt.Printf("error %v, %v, in terminating sleep", err, strings.TrimSpace(stderr.String())) + } +}