Skip to content

Commit

Permalink
Now forwarding all signals to process group
Browse files Browse the repository at this point in the history
By doing this, we make sure piping through zap-pretty will still send all
signals to the process group ensuring they can do signal handling themselves
removing some weird problem killing process can bring.
  • Loading branch information
Matthieu Vachon committed Jun 15, 2019
1 parent 61cc8e5 commit b72f5a0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func main() {
os.Exit(0)
}

go NewSignaler().forwardAllSignalsToProcessGroup()

processor := &processor{
scanner: bufio.NewScanner(os.Stdin),
output: os.Stdout,
Expand Down
38 changes: 38 additions & 0 deletions signaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"os"
"os/signal"
"syscall"
)

type signaler struct {
processGroupID int
}

func NewSignaler() *signaler {
signaler := &signaler{}

pgid, err := syscall.Getpgid(os.Getpid())
if err != nil {
signaler.processGroupID = pgid
} else {
debug.Println("[Warning] unable to determine process group, signaling will be broken")
}

return signaler
}

func (s *signaler) forwardAllSignalsToProcessGroup() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan)

for {
signal := <-signalChan

syscallSignal, isSyscallSignalType := signal.(syscall.Signal)
if s.processGroupID != 0 && isSyscallSignalType {
syscall.Kill(s.processGroupID, syscallSignal)
}
}
}

0 comments on commit b72f5a0

Please sign in to comment.