Skip to content

Commit af392b7

Browse files
committed
handle lock file deletion before checkoputRevision
Signed-off-by: reggie-k <regina.voloshin@codefresh.io>
1 parent 7050d0b commit af392b7

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

util/exec/exec.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
204204
cmd.Stdout = &stdout
205205
cmd.Stderr = &stderr
206206

207+
// Configure the child to run in its own process group so we can signal the whole group on timeout/cancel.
208+
// On Unix this sets Setpgid; on Windows this is a no-op.
209+
if cmd.SysProcAttr == nil {
210+
cmd.SysProcAttr = newSysProcAttr(true)
211+
}
212+
207213
start := time.Now()
208214
err = cmd.Start()
209215
if err != nil {
@@ -247,14 +253,19 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
247253
// noinspection ALL
248254
case <-timoutCh:
249255
// send timeout signal
250-
_ = cmd.Process.Signal(timeoutBehavior.Signal)
256+
// signal the process group (negative PID) so children are terminated as well
257+
if cmd.Process != nil {
258+
_ = sysCallSignal(-cmd.Process.Pid, timeoutBehavior.Signal)
259+
}
251260
// wait on timeout signal and fallback to fatal timeout signal
252261
if timeoutBehavior.ShouldWait {
253262
select {
254263
case <-done:
255264
case <-fatalTimeoutCh:
256-
// upgrades to SIGKILL if cmd does not respect SIGTERM
257-
_ = cmd.Process.Signal(fatalTimeoutBehaviour)
265+
// upgrades to fatal signal (default SIGKILL) if cmd does not respect the initial signal
266+
if cmd.Process != nil {
267+
_ = sysCallSignal(-cmd.Process.Pid, fatalTimeoutBehaviour)
268+
}
258269
// now original cmd should exit immediately after SIGKILL
259270
<-done
260271
// return error with a marker indicating that cmd exited only after fatal SIGKILL

util/exec/exec_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package exec
5+
6+
import "syscall"
7+
8+
func newSysProcAttr(setpgid bool) *syscall.SysProcAttr {
9+
return &syscall.SysProcAttr{Setpgid: setpgid}
10+
}
11+
func sysCallSignal(pid int, sig syscall.Signal) error {
12+
return syscall.Kill(pid, sig)
13+
}

util/exec/exec_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package exec
5+
6+
import "syscall"
7+
8+
func newSysProcAttr(_ bool) *syscall.SysProcAttr { return &syscall.SysProcAttr{} }
9+
10+
func sysCallSignal(_ int, _ syscall.Signal) error { return nil }

0 commit comments

Comments
 (0)