startSignalForwarding registers signal handlers synchronously then forwards caught signals to the child in a background goroutine. Registering before the goroutine starts ensures no signal is lost between ForkExec and the handler being ready.
(logger slog.Logger, pid int, sigs []os.Signal)
| 24 | // goroutine. Registering before the goroutine starts ensures no |
| 25 | // signal is lost between ForkExec and the handler being ready. |
| 26 | func startSignalForwarding(logger slog.Logger, pid int, sigs []os.Signal) { |
| 27 | if len(sigs) == 0 { |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | sc := make(chan os.Signal, 1) |
| 32 | signal.Notify(sc, sigs...) |
| 33 | |
| 34 | logger.Info(context.Background(), "reaper catching signals", |
| 35 | slog.F("signals", sigs), |
| 36 | slog.F("child_pid", pid), |
| 37 | ) |
| 38 | |
| 39 | go func() { |
| 40 | defer signal.Stop(sc) |
| 41 | for s := range sc { |
| 42 | sig, ok := s.(syscall.Signal) |
| 43 | if ok { |
| 44 | logger.Info(context.Background(), "reaper caught signal, killing child process", |
| 45 | slog.F("signal", sig.String()), |
| 46 | slog.F("child_pid", pid), |
| 47 | ) |
| 48 | _ = syscall.Kill(pid, sig) |
| 49 | } |
| 50 | } |
| 51 | }() |
| 52 | } |
| 53 | |
| 54 | // ForkReap spawns a goroutine that reaps children. In order to avoid |
| 55 | // complications with spawning `exec.Commands` in the same process that |