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
12 changes: 9 additions & 3 deletions library/core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Most process-related functionality requires std, but [`crash`] is available
//! on all targets.

/// Terminates the process in a violent fashion.
/// Terminates the process in a violent but safe fashion.
///
/// The function will never return and will immediately terminate the current
/// process in a platform specific "abnormal" manner. As a consequence,
Expand All @@ -15,8 +15,14 @@
/// to match C `abort()` or otherwise perform a "clean" abort. Instead, it emits
/// code that will crash the process with as little overhead as possible, such
/// as a "halt and catch fire" style instruction. You should generally prefer
/// using `abort` instead except where the absolute minimum overhead is
/// required.
/// using `abort` or one of the other options mentioned in [Exiting the current
/// process], unless absolute minimum overhead is required.
///
/// While this function provides an abrupt exit, note that it is still a safe
/// form of crashing—unlike, for example, a segfault caused by a null pointer
/// dereference.
///
/// [Exiting the current process]: ../../std/process/index.html#exiting-the-current-process
///
/// # Platform-specific behavior
///
Expand Down
50 changes: 45 additions & 5 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! successfully spawned, any error that happens afterwards will be reported through
//! its [`ExitStatus`].
//!
//! # Handling I/O
//! ## Handling I/O
//!
//! The [`stdout`], [`stdin`], and [`stderr`] of a child process can be
//! configured by passing an [`Stdio`] to the corresponding method on
Expand Down Expand Up @@ -100,7 +100,7 @@
//! assert_eq!(b"test", output.stdout.as_slice());
//! ```
//!
//! # Windows argument splitting
//! ## Windows argument splitting
//!
//! On Unix systems arguments are passed to a new process as an array of strings,
//! but on Windows arguments are passed as a single commandline string and it is
Expand Down Expand Up @@ -141,6 +141,42 @@
//! Also note that running batch scripts in this way may be removed in the
//! future and so should not be relied upon.
//!
//! # Exiting the current process
//!
//! Rust provides a few different ways to terminate the currently running program:
//!
//! | API | Unix exit code | Windows exit code | Unix signal | Cleans up resources |
//! |-----------------------------------------------|----------------|----------------------------------------------------|-----------------------------------------------|---------------------|
//! | Return [`ExitCode`] or a `Result` from `main` | User-provided | User-provided | None | Yes |
//! | [`panic!`] | 101 | 101 | None | Yes |
//! | [`exit`] | User-provided | User-provided | None | Partial |
//! | [`abort`] | None | `STATUS_STACK_BUFFER_OVERRUN`[^win-status-overrun] | `SIGABRT` | No |
//! | [`crash`] | None | Unspecified | Unspecified (typically `SIGTRAP` or `SIGILL`) | No |
//!
//! Use these guidelines to pick which to use:
//!
//! * Use `exit` or return `ExitCode` for exiting as part of normal program
//! behavior. These exit with no additional message, just the provided code.

@RalfJung RalfJung Nov 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#165125 makes exit do basically no cleanup at all, doesn't it?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that's correct. I think it should be fine to have "partial" in the table then change it to "no" in that PR (assuming this may merge first).

//! * Use `panic!` for unexpected exits, such as bug conditions. This will print a
//! message with the code location and give the option of printing a backtrace.
//! * Prefer versions that clean up resources where possible (return `ExitCode`
//! rather than call `exit`), since these flush buffers and run destructors.
//! * Use `abort` for cases where cleanup must not run.
//! * Use `crash` only when it is critical to terminate with minimal overhead.
//!
//! A few things to note:
//!
//! * `panic!` can be caught be default, other options cannot.
//! * The behavior of `panic!` can be changed via the `-Cpanic` compiler flag.
//! Refer to <https://doc.rust-lang.org/rustc/codegen-options/index.html#panic>
//! for details.
//! * Where specified, exact exit codes are subject to change.
//!
//! For further details, refer to the documentation for each API.
//!
//! [^win-status-overrun]: `STATUS_STACK_BUFFER_OVERRUN` is the exit status used
//! by [`__fastfail()`]. It does not mean there is an actual buffer overrun.
//!
//! [`spawn`]: Command::spawn
//! [`output`]: Command::output
//!
Expand All @@ -156,6 +192,7 @@
//! [`raw_arg`]: crate::os::windows::process::CommandExt::raw_arg
//!
//! [`CreateProcessW`]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw
//! [`__fastfail()`]: https://learn.microsoft.com/en-us/cpp/intrinsics/fastfail?view=msvc-170

#![stable(feature = "process", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]
Expand Down Expand Up @@ -2597,14 +2634,17 @@ pub fn exit(code: i32) -> ! {
/// crate's Cargo.toml, [`panic!`] and `abort` are similar. However,
/// [`panic!`] will still call the [panic hook] while `abort` will not.
///
/// If a clean shutdown is needed it is recommended to only call
/// this function at a known point where there are no more destructors left
/// to run.
/// If a clean shutdown is needed it is recommended to only call this function
/// at a known point where there are no more destructors left to run. If an
/// abrupt exit is not needed, one of the other forms of exiting discussed at
/// [Exiting the current process] is likely preferable.
///
/// The process's termination will be similar to that from the C `abort()`
/// function. On Unix, the process will terminate with signal `SIGABRT`, which
/// typically means that the shell prints "Aborted".
///
/// [Exiting the current process]: self#exiting-the-current-process
///
/// # Examples
///
/// ```no_run
Expand Down
63 changes: 0 additions & 63 deletions tests/run-make/abort-behavior/rmake.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ fn main() {
match arg.as_str() {
"abort" => std::process::abort(),
"crash" => std::process::crash(),
"panic" => panic!("panic!"),
"exit-0" => std::process::exit(0),
"exit-1" => std::process::exit(1),
_ => panic!("unrecognized command {arg}"),
}
}
93 changes: 93 additions & 0 deletions tests/run-make/exit-behavior/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//! Check exit codes and signals of terminating library functions.

//@ ignore-cross-compile

use run_make_support::{bin_name, run_fail_with_args, run_with_args, rustc};

#[derive(Debug)]
enum Variant {
Abort,
Crash,
Panic,
Exit0,
Exit1,
}

fn main() {
rustc().input("main.rs").edition("2024").run();

check_one(Variant::Abort);
check_one(Variant::Crash);
check_one(Variant::Panic);
check_one(Variant::Exit0);
check_one(Variant::Exit1);
}

fn check_one(variant: Variant) {
println!("checking variant {variant:?}");
let arg = match variant {
Variant::Abort => "abort",
Variant::Crash => "crash",
Variant::Panic => "panic",
Variant::Exit0 => "exit-0",
Variant::Exit1 => "exit-1",
};
let bin = &bin_name("main");

let (out, expected_success) = if matches!(variant, Variant::Exit0) {
(run_with_args(bin, &[arg]), true)
} else {
(run_fail_with_args(bin, &[arg]), false)
};

let status = out.status();
let code = status.code();
println!("output status for {variant:?}: {status:?}");
assert_eq!(status.success(), expected_success);

cfg_select! {
unix => {
use std::assert_matches;
use std::os::unix::process::ExitStatusExt;

use run_make_support::libc;

let expected_code = match variant {
Variant::Abort => None,
Variant::Crash => None,
Variant::Panic => Some(101),
Variant::Exit0 => Some(0),
Variant::Exit1 => Some(1),
};
assert_eq!(code, expected_code);

let sig = status.signal();
match variant {
Variant::Abort => assert_eq!(sig, Some(libc::SIGABRT)),
Variant::Crash => assert_matches!(sig, Some(libc::SIGILL | libc::SIGTRAP)),
Variant::Panic => assert_eq!(sig, None),
Variant::Exit0 => assert_eq!(sig, None),
Variant::Exit1 => assert_eq!(sig, None),
};
}
windows => {
use run_make_support::windows;
use windows::Win32::Foundation::{
STATUS_ILLEGAL_INSTRUCTION, STATUS_STACK_BUFFER_OVERRUN,
};

let code = code.expect("Windows always has an exit code");
let expected_code = match variant {
Variant::Abort => STATUS_STACK_BUFFER_OVERRUN.0,
Variant::Crash => STATUS_ILLEGAL_INSTRUCTION.0,
Variant::Panic => 101,
Variant::Exit0 => 0,
Variant::Exit1 => 1,
};
assert_eq!(code, expected_code);
}
_ => {
unimplemented!("target may need a new branch")
}
}
}
Loading