Skip to content

Commit

Permalink
fix(2351): teardown steps do not get the environment variables (#413)
Browse files Browse the repository at this point in the history
Co-authored-by: parthasl <[email protected]>
  • Loading branch information
parthasl and parthasl authored Apr 15, 2021
1 parent 5e474d4 commit 50f0194
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package executor

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -354,13 +355,16 @@ 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 {
firstError = stepAbort
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 {
Expand Down Expand Up @@ -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()))
}
}

0 comments on commit 50f0194

Please sign in to comment.