From cc351cd6db825a57ccd447643592d758bc5a7902 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Sun, 2 Aug 2026 00:14:04 +0300 Subject: [PATCH] src: add --trace-sigterm to print a stack trace on SIGTERM Neither `--report-on-signal` nor a `process.on('SIGTERM')` handler runs while JavaScript is stuck, which is exactly when a trace is wanted: a pod whose liveness probe stopped responding gets terminated with SIGTERM, and there is currently no way to find out where it was stuck. Print the stack trace from an interrupt instead, like `--trace-sigint` does. The signal is watched through libuv on a dedicated thread and event loop rather than through a signal handler of our own, so that it stays multiplexed with `process.on('SIGTERM')` handlers: applications that shut down gracefully are the ones most likely to have a handler installed, and disabling the trace for them would defeat the purpose. Watching the signal takes its default disposition away, so restore it and re-raise once the trace has been printed, unless the application handles SIGTERM itself, in which case libuv has already delivered the signal to its handler as well. Co-Authored-By: Claude Opus 5 Fixes: https://github.com/nodejs/node/issues/56879 Signed-off-by: Orgad Shaneh --- doc/api/cli.md | 14 +++++ doc/node-config-schema.json | 4 ++ doc/node.1 | 9 ++++ src/node.cc | 4 ++ src/node_options.cc | 5 ++ src/node_options.h | 1 + src/node_watchdog.cc | 84 +++++++++++++++++++++++++++++ src/node_watchdog.h | 28 ++++++++++ test/parallel/test-trace-sigterm.js | 56 +++++++++++++++++++ 9 files changed, 205 insertions(+) create mode 100644 test/parallel/test-trace-sigterm.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 9434e19d3b73..fe6422392251 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3333,6 +3333,19 @@ added: Prints a stack trace on SIGINT. +### `--trace-sigterm` + + + +Prints a stack trace on SIGTERM. + +Unlike a `SIGTERM` handler installed with `process.on('SIGTERM')`, the trace is +also printed while JavaScript is stuck, for example in an infinite loop. If +the application does not handle `SIGTERM` itself, the process is terminated by +the signal as usual once the trace has been printed. + ### `--trace-sync-io`