From 31d036e27f8ad5797280bc736ae94b734ea7c97f Mon Sep 17 00:00:00 2001 From: Andrew Halaney Date: Thu, 7 May 2026 15:40:22 -0500 Subject: [PATCH] fix: handle SIGTERM gracefully on unix We weren't catching SIGTERM and cancelling our context, so the go runtime would have us exit with a non-zero code when the signal was received. Let's catch it and try to exit gracefully. i.e. before: (...) I0508 11:08:00.426917 1787442 log_monitor.go:237] Initialize condition generated: [] ^C $ echo $? 130 and now: (...) I0508 11:07:42.150562 1787146 log_monitor.go:237] Initialize condition generated: [] ^C I0508 11:07:43.372093 1787146 log_monitor.go:123] Stop log monitor /tmp/npd-test.json I0508 11:07:43.652876 1787146 log_watcher.go:104] Stop watching filelog I0508 11:07:43.652923 1787146 log_monitor.go:144] Log monitor stopped: /tmp/npd-test.json $ echo $? 0 --- cmd/nodeproblemdetector/node_problem_detector_unix.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/nodeproblemdetector/node_problem_detector_unix.go b/cmd/nodeproblemdetector/node_problem_detector_unix.go index a07f0356f..3e44b1637 100644 --- a/cmd/nodeproblemdetector/node_problem_detector_unix.go +++ b/cmd/nodeproblemdetector/node_problem_detector_unix.go @@ -21,6 +21,9 @@ package main import ( "context" "flag" + "os" + "os/signal" + "syscall" "github.com/spf13/pflag" "k8s.io/klog/v2" @@ -54,7 +57,9 @@ func main() { } pflag.Parse() - if err := npdMain(context.Background(), npdo); err != nil { + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + if err := npdMain(ctx, npdo); err != nil { klog.Fatalf("Problem detector failed with error: %v", err) } }