Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cli/util/file_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ where
// Dispatch SIGTERM to give JS code a chance for async cleanup
// before restarting. If handlers are registered, wait for the
// operation to finish gracefully before restarting.
//
// Suppress the OS default action for a terminating signal during this
// window: a JS handler may unregister itself and re-raise a real
// SIGTERM (the `signal-exit` "unload + re-raise" pattern bundled in
// Vite 8 / rolldown), which would otherwise terminate the watcher
// instead of restarting it. Set this before `raise()` because the
// re-raise happens synchronously inside the handler.
// https://github.com/denoland/deno/issues/35942
deno_signals::set_suppress_default_exit(true);
if deno_signals::raise(deno_signals::SIGTERM) {
info!(
"{} Waiting for graceful termination...",
Expand All @@ -468,6 +477,7 @@ where
)
.await;
}
deno_signals::set_suppress_default_exit(false);
deno_runtime::deno_inspector_server::notify_restart();
print_after_restart();
continue;
Expand Down
37 changes: 36 additions & 1 deletion ext/signals/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;

Expand All @@ -19,6 +20,26 @@ static SIGWINCH: i32 = 28;

static COUNTER: AtomicU32 = AtomicU32::new(0);

// While set, an unhandled terminating signal (SIGTERM/SIGINT/SIGHUP) delivered
// to the OS signal thread is consumed instead of running the exit callbacks and
// the OS default action. The file watcher enables this for the duration of a
// graceful restart, where a JS signal handler may unregister itself and re-raise
// a real OS signal (the `signal-exit` "unload + re-raise" pattern used by Vite
// 8 / rolldown) that would otherwise kill the watcher instead of restarting it.
// See https://github.com/denoland/deno/issues/35942.
static SUPPRESS_DEFAULT_EXIT: AtomicBool = AtomicBool::new(false);

/// Suppress (or restore) the OS default action for an unhandled terminating
/// signal. While suppressed, a SIGTERM/SIGINT/SIGHUP with no registered handler
/// is consumed rather than terminating the process.
pub fn set_suppress_default_exit(suppress: bool) {
SUPPRESS_DEFAULT_EXIT.store(suppress, Ordering::SeqCst);
}

fn is_terminating_signal(signal: i32) -> bool {
signal == SIGHUP || signal == SIGTERM || signal == SIGINT
}

type Handler = Box<dyn Fn() + Send>;
type Handlers = HashMap<i32, Vec<(u32, bool, Handler)>>;
static HANDLERS: OnceLock<(Handle, Mutex<Handlers>)> = OnceLock::new();
Expand Down Expand Up @@ -62,7 +83,13 @@ fn init() -> Handle {
for signal in signals.forever() {
let handled = handle_signal(signal);
if !handled {
if signal == SIGHUP || signal == SIGTERM || signal == SIGINT {
if is_terminating_signal(signal) {
// A graceful restart (file watcher) may have unregistered the JS
// handler and re-raised this signal; consume it instead of killing
// the process. See https://github.com/denoland/deno/issues/35942.
if SUPPRESS_DEFAULT_EXIT.load(Ordering::SeqCst) {
continue;
}
run_exit();
}
signal_hook::low_level::emulate_default_handler(signal).unwrap();
Expand All @@ -85,6 +112,14 @@ fn init() -> Handle {
_ => return 0,
};
let handled = handle_signal(signal);
if !handled
&& is_terminating_signal(signal)
&& SUPPRESS_DEFAULT_EXIT.load(Ordering::SeqCst)
{
// Consume a re-raised terminating signal during a graceful restart
// (https://github.com/denoland/deno/issues/35942).
return 1;
}
handled as _
}

Expand Down
51 changes: 51 additions & 0 deletions tests/integration/watcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,57 @@ async fn run_watch_sigterm_on_restart() {
check_alive_then_kill(child);
}

/// Test that a JS SIGTERM handler which unregisters itself and re-raises a
/// real OS SIGTERM (the `signal-exit` "unload + re-raise" pattern bundled in
/// Vite 8 / rolldown) does not kill the watcher: the process must restart on
/// file change instead of terminating.
/// Regression test for https://github.com/denoland/deno/issues/35942.
#[cfg(unix)]
#[test(flaky)]
async fn run_watch_sigterm_reraise_on_restart() {
let t = TempDir::new();
let file_to_watch = t.path().join("file_to_watch.js");
let contents = r#"
import process from "node:process";
const handler = () => {
console.log("SIGTERM handler");
// Unregister self, then re-raise a real OS SIGTERM.
process.off("SIGTERM", handler);
process.kill(process.pid, "SIGTERM");
};
process.on("SIGTERM", handler);
setInterval(() => {}, 1000);
"#;
file_to_watch.write(contents);

let mut child = util::deno_cmd()
.current_dir(t.path())
.arg("run")
.arg("--watch")
.arg("-L")
.arg("debug")
.arg("--allow-all")
.arg(&file_to_watch)
.env("NO_COLOR", "1")
.piped_output()
.spawn()
.unwrap();
let (mut stdout_lines, mut stderr_lines) = child_lines(&mut child);

wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;

// Trigger a restart; the handler fires, unregisters itself and re-raises a
// real SIGTERM.
file_to_watch.write(format!("{contents}\n// changed"));

wait_contains("SIGTERM handler", &mut stdout_lines).await;
// The re-raised real SIGTERM must be suppressed so the watcher restarts
// instead of dying.
wait_contains("Restarting", &mut stderr_lines).await;
wait_for_watcher("file_to_watch.js", &mut stderr_lines).await;
check_alive_then_kill(child);
}

/// Test that both SIGINT and SIGTERM are dispatched on Ctrl+C in watch mode.
#[cfg(unix)]
#[test(flaky)]
Expand Down
Loading