Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
542ffbd
run: act on SIGINT, SIGTERM and SIGHUP at once in --filter and --para…
robobun Sep 6, 2026
162e287
run: keep a signal the runner inherited as ignored ignored
robobun Sep 6, 2026
ca4a99c
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 6, 2026
e90b21c
run_abort: shorten comments
robobun Sep 6, 2026
a767eef
run_abort: query the disposition before hooking, guard kill on Runnin…
robobun Sep 6, 2026
00f39e1
run: uninstall the abort handler before the final status read
robobun Sep 6, 2026
7939565
run: forward a signal that arrives after a failure cascade, keep errn…
robobun Sep 6, 2026
6de0ff3
run: shorten the abort doc comment
robobun Sep 6, 2026
c85d9ec
ci: retrigger
robobun Sep 6, 2026
3c6df0c
test: --no-orphans members receive the forwarded SIGTERM before the r…
robobun Sep 6, 2026
3366181
run: hook the abort signals before the first script is spawned
robobun Sep 18, 2026
6d54755
Add test for Ctrl+C on Windows with an idle filtered script
robobun Sep 16, 2026
09ee5ff
Clear the exit timer in the Ctrl+C fixture
robobun Sep 16, 2026
c4b69e5
test: shorten the Windows Ctrl+C test comment
robobun Sep 18, 2026
04bfa58
run: always hook SIGINT, make the Windows Ctrl+C test independent of …
robobun Sep 18, 2026
f7a7513
run_abort: shorten the SIGINT comment
robobun Sep 18, 2026
f8e895b
run: end an aborted run by the signal, not by exit code 128 + signal
robobun Sep 18, 2026
250f105
Global: shorten the exit_by_signal doc
robobun Sep 18, 2026
5a6486e
run: shorten the abort exit comment
robobun Sep 18, 2026
36234d4
run: docs and tests for the Windows exit code, run the filter signal …
robobun Sep 18, 2026
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 docs/pm/filter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,13 @@ Use `--if-present` with `--workspaces` to skip packages that don't have the requ
### Dependency Order

Bun respects package dependency order when running scripts. Say you have a package `foo` that depends on another package `bar` in your workspace, and both have a `build` script. When you run `bun --filter '*' build`, `foo` only starts once `bar` is done.

### Signals

When `bun run --filter`, `--parallel`, or `--sequential` receives `SIGINT`, `SIGTERM`, or `SIGHUP`, Bun sends the same signal to every script that is still running. After that, Bun starts no more scripts. When the scripts exit, Bun ends itself with that signal, even if every script exited with 0. A shell reports this as exit code `128 + signal` (130, 143, or 129). When Bun is PID 1 in a Linux container, Bun exits with that code instead, because PID 1 does not die of such a signal.

On Windows, `Ctrl+C` does the same, and Bun exits with code 130.

Bun waits for the scripts without a time limit. A script that ignores the signal keeps the run alive. A second signal ends Bun at once and leaves that script running.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

When a signal reaches the scripts directly as well, each script receives it twice. A terminal `Ctrl+C` and a supervisor that signals the whole process group both do that.
13 changes: 13 additions & 0 deletions src/bun_core/Global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,19 @@ pub fn exit(code: u32) -> ! {
}
}

/// Runs the exit callbacks, then ends the process by `sig` (which must be at
/// `SIG_DFL`), so the parent sees a signal death. Where the signal does not
/// kill (PID 1, Windows), the exit code is `128 + sig`.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub fn exit_by_signal(sig: c_int) -> ! {
#[cfg(unix)]
{
IS_EXITING.store(true, Ordering::Relaxed);
Bun__onExit();
let _ = libc_raise(sig);
}
exit(128 + sig as u32)
}

pub fn raise_ignoring_panic_handler(sig: crate::SignalCode) -> ! {
raise_ignoring_panic_handler_raw(sig as c_int)
}
Expand Down
121 changes: 34 additions & 87 deletions src/runtime/cli/filter_run.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use core::ffi::{c_char, c_void};
use std::io::Write as _;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;

#[cfg(unix)]
use crate::api::bun::process::SpawnResultExt as _;
use crate::api::bun::process::{self as spawn, Rusage, SpawnOptions, Status};
use crate::cli::Command;
use crate::cli::filter_arg as FilterArg;
use crate::cli::run_abort;
use crate::cli::run_command::{ConfigureEnvOptions, RunCommand};
use bun_collections::StringHashMap;
use bun_core::{Global, Output};
Expand Down Expand Up @@ -652,27 +652,35 @@ impl<'a> State<'a> {
let _ = bun_sys::File::stdout().write_all(&self.draw_buf);
}

fn abort(&mut self) {
/// Sends `signal` to every script that is still running.
fn kill_running(&self, signal: bun_sys::SignalCode) {
for handle in self.handles.iter() {
if let Some(proc) = &handle.process
&& matches!(proc.status, Status::Running)
{
let _ = proc.process.kill(signal.0);
}
}
}

/// Sends `signal` to every running script and finishes the rest.
fn abort(&mut self, signal: bun_sys::SignalCode) {
if self.aborted {
self.kill_running(signal);
return;
}
// we perform an abort by sending SIGINT to all processes
self.aborted = true;
self.kill_running(signal);
// Raw ptrs so `self.maybe_finish` can be called while walking (the
// file-wide State/handle backref pattern).
let handles: Vec<*mut ProcessHandle<'a>> =
self.handles.iter_mut().map(std::ptr::from_mut).collect();
for handle in handles {
// SAFETY: points into `self.handles`, live for the whole run loop.
if let Some(proc) = unsafe { (*handle).process.as_ref() } {
// if we get an error here we simply ignore it
let _ = proc.process.kill(bun_sys::SignalCode::SIGINT.0);
}
// An already-exited handle may be waiting on pipes a grandchild
// still holds; with `aborted` set this finishes it now. Killed
// handles finish when their exit arrives.
// SAFETY: same `self.handles` element as above; the exclusive
// reborrow is confined to this call.
// SAFETY: points into `self.handles`, live for the whole run loop;
// the exclusive reborrow is confined to this call.
let _ = self.maybe_finish(unsafe { &mut *handle });
}
}
Expand Down Expand Up @@ -700,75 +708,6 @@ impl<'a> State<'a> {
}
}

struct AbortHandler;

static SHOULD_ABORT: AtomicBool = AtomicBool::new(false);
// Atomic because it is set from a signal handler.

impl AbortHandler {
#[cfg(unix)]
extern "C" fn posix_signal_handler(
sig: i32,
info: *const bun_sys::posix::siginfo_t,
_: *const c_void,
) {
let _ = sig;
let _ = info;
SHOULD_ABORT.store(true, Ordering::SeqCst);
}

#[cfg(windows)]
extern "system" fn windows_ctrl_handler(
dw_ctrl_type: bun_sys::windows::DWORD,
) -> bun_sys::windows::BOOL {
if dw_ctrl_type == bun_sys::windows::CTRL_C_EVENT {
SHOULD_ABORT.store(true, Ordering::SeqCst);
return bun_sys::windows::TRUE;
}
bun_sys::windows::FALSE
}

fn install() {
#[cfg(unix)]
{
// SAFETY: libc::sigaction is #[repr(C)] POD; all-zero is a valid value (fields overwritten below).
let mut act: libc::sigaction = bun_core::ffi::zeroed();
act.sa_sigaction = Self::posix_signal_handler as *const () as usize;
act.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART | libc::SA_RESETHAND;
// SAFETY: sa_mask is a valid out-pointer; act is on the stack.
unsafe {
libc::sigemptyset(&raw mut act.sa_mask);
libc::sigaction(libc::SIGINT, &raw const act, core::ptr::null_mut());
}
}
#[cfg(not(unix))]
{
let res = bun_sys::c::SetConsoleCtrlHandler(
Some(Self::windows_ctrl_handler),
bun_sys::windows::TRUE,
);
if res == 0 {
if bun_core::env::IS_DEBUG {
bun_core::warn!("Failed to set abort handler\n");
}
}
}
}

fn uninstall() {
// only necessary on Windows, as on posix we pass the SA_RESETHAND flag
#[cfg(windows)]
{
// (None, FALSE) clears the ignore attribute; it does NOT unregister
// a handler routine — pass the address.
let _ = bun_sys::c::SetConsoleCtrlHandler(
Some(Self::windows_ctrl_handler),
bun_sys::windows::FALSE,
);
}
}
}

#[cfg(windows)]
fn windows_is_terminal() -> bool {
let res = bun_sys::windows::GetFileType(bun_sys::Fd::stdout().native());
Expand Down Expand Up @@ -1083,6 +1022,11 @@ pub(crate) fn run_scripts_with_filter(
}
}

// Before the first spawn: a signal that still has its default action
// would end the runner and leave the scripts already started running.
// SAFETY: event_loop is the live thread-local MiniEventLoop singleton.
run_abort::install(unsafe { (*event_loop).loop_ptr() });

// Collect the roots before starting any: a script that has already exited
// when `start()` watches it can finish (and cascade) inside `start()`,
// which zeroes `remaining_dependencies` of later handles it started.
Expand All @@ -1100,15 +1044,12 @@ pub(crate) fn run_scripts_with_filter(
}
}

AbortHandler::install();

let mut signaled = false;
while !state.is_done() {
if SHOULD_ABORT.load(Ordering::SeqCst) && !state.aborted {
// We uninstall the custom abort handler so that if the user presses Ctrl+C again,
// the process is aborted immediately and doesn't wait for the event loop to tick.
// This can be useful if one of the processes is stuck and doesn't react to SIGINT.
AbortHandler::uninstall();
state.abort();
if !signaled && let Some(signal) = run_abort::pending() {
signaled = true;
run_abort::uninstall();
state.abort(signal);
// The abort sweep may have finished the last script; re-check
// before blocking in a tick no event may ever wake.
continue;
Expand All @@ -1117,7 +1058,13 @@ pub(crate) fn run_scripts_with_filter(
unsafe { (*event_loop).tick_once(&raw const state as *mut c_void) };
}

// A signal from here on ends the process through its default disposition.
run_abort::uninstall();
let status = state.finalize();
// An aborted run ends by the signal, even when every started script exited 0.
if let Some(signal) = run_abort::pending() {
Global::exit_by_signal(signal.0.into());
}

Global::exit(status as u32);
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ pub(crate) mod prune_command;
pub mod publish_command;
#[path = "remove_command.rs"]
pub(crate) mod remove_command;
#[path = "run_abort.rs"]
pub(crate) mod run_abort;
#[path = "scan_command.rs"]
pub mod scan_command;
#[path = "unlink_command.rs"]
Expand Down
Loading
Loading