Skip to content

Commit

Permalink
Add Signal Forwarding to Entrypoint Runner
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
waveywaves committed Apr 16, 2020
1 parent 6b1579c commit fcb1bea
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion cmd/entrypoint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"
"os/exec"
"os/signal"
"syscall"

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

// Receive system signals on "signals"
signals := make(chan os.Signal, 1)
defer close(signals)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
defer signal.Reset()

cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// dedicated PID group used to forward signals to
// main process and all children
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}

// Goroutine for signals forwarding
go func() {
for s := range signals {
// Forward signal to main process and all children
syscall.Kill(-cmd.Process.Pid, s.(syscall.Signal))
}
}()

// Start defined command
err := cmd.Start()
if err != nil {
return err
}

if err := cmd.Run(); err != nil {
// Wait for command to exit
err = cmd.Wait()
if err != nil {
return err
}

return nil
}

0 comments on commit fcb1bea

Please sign in to comment.