Skip to content

Commit 170d5be

Browse files
committed
Add Signal Forwarding to Entrypoint Runner
The Entrypoint process should be notified and signals received should be propogated as necessary. This signal forwarding mimics the one in https://github.com/pablo-ruth/go-init which is an golang implementation of https://github.com/Yelp/dumb-init . The cmd.Run() has also been replaced with a cmd.Start() and cmd.Wait() to systematically start the command and Wait for it's completion without prematurely exiting.
1 parent 6b1579c commit 170d5be

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

cmd/entrypoint/runner.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"os"
55
"os/exec"
6+
"os/signal"
7+
"syscall"
68

79
"github.com/tektoncd/pipeline/pkg/entrypoint"
810
)
@@ -21,12 +23,38 @@ func (*realRunner) Run(args ...string) error {
2123
}
2224
name, args := args[0], args[1:]
2325

26+
// Receive system signals on "signals"
27+
signals := make(chan os.Signal, 1)
28+
defer close(signals)
29+
signal.Notify(signals)
30+
defer signal.Reset()
31+
2432
cmd := exec.Command(name, args...)
2533
cmd.Stdout = os.Stdout
2634
cmd.Stderr = os.Stderr
35+
// dedicated PID group used to forward signals to
36+
// main process and all children
37+
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
38+
39+
// Goroutine for signals forwarding
40+
go func() {
41+
for s := range signals {
42+
// Forward signal to main process and all children
43+
syscall.Kill(-cmd.Process.Pid, s.(syscall.Signal))
44+
}
45+
}()
46+
47+
// Start defined command
48+
err := cmd.Start()
49+
if err != nil {
50+
return err
51+
}
2752

28-
if err := cmd.Run(); err != nil {
53+
// Wait for command to exit
54+
err = cmd.Wait()
55+
if err != nil {
2956
return err
3057
}
58+
3159
return nil
3260
}

0 commit comments

Comments
 (0)