MCPcopy Index your code
hub / github.com/coder/coder / startSignalForwarding

Function startSignalForwarding

agent/reaper/reaper_unix.go:26–52  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

24// goroutine. Registering before the goroutine starts ensures no
25// signal is lost between ForkExec and the handler being ready.
26func 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

Callers 1

ForkReapFunction · 0.85

Calls 5

StopMethod · 0.65
KillMethod · 0.65
NotifyMethod · 0.45
InfoMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected