Rollup merge of #136320 - RalfJung:exit, r=the8472

exit: document interaction with C

Cc https://github.com/rust-lang/rust/issues/126600
This commit is contained in:
Matthias Krüger 2025-03-18 10:09:28 +01:00 committed by GitHub
commit 098db784c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 4 deletions

View File

@ -333,7 +333,7 @@ impl Error for VarError {
/// ///
/// Discussion of this unsafety on Unix may be found in: /// Discussion of this unsafety on Unix may be found in:
/// ///
/// - [Austin Group Bugzilla](https://austingroupbugs.net/view.php?id=188) /// - [Austin Group Bugzilla (for POSIX)](https://austingroupbugs.net/view.php?id=188)
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2) /// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=15607#c2)
/// ///
/// To pass an environment variable to a child process, you can instead use [`Command::env`]. /// To pass an environment variable to a child process, you can instead use [`Command::env`].

View File

@ -2018,9 +2018,9 @@ impl ExitCode {
/// ///
/// Note that this has the same caveats as [`process::exit()`][exit], namely that this function /// Note that this has the same caveats as [`process::exit()`][exit], namely that this function
/// terminates the process immediately, so no destructors on the current stack or any other /// terminates the process immediately, so no destructors on the current stack or any other
/// thread's stack will be run. If a clean shutdown is needed, it is recommended to simply /// thread's stack will be run. Also see those docs for some important notes on interop with C
/// return this ExitCode from the `main` function, as demonstrated in the [type /// code. If a clean shutdown is needed, it is recommended to simply return this ExitCode from
/// documentation](#examples). /// the `main` function, as demonstrated in the [type documentation](#examples).
/// ///
/// # Differences from `process::exit()` /// # Differences from `process::exit()`
/// ///
@ -2326,6 +2326,33 @@ impl Child {
/// ///
/// process::exit(0x0100); /// process::exit(0x0100);
/// ``` /// ```
///
/// ### Safe interop with C code
///
/// On Unix, this function is currently implemented using the `exit` C function [`exit`][C-exit]. As
/// of C23, the C standard does not permit multiple threads to call `exit` concurrently. Rust
/// mitigates this with a lock, but if C code calls `exit`, that can still cause undefined behavior.
/// Note that returning from `main` is equivalent to calling `exit`.
///
/// Therefore, it is undefined behavior to have two concurrent threads perform the following
/// without synchronization:
/// - One thread calls Rust's `exit` function or returns from Rust's `main` function
/// - Another thread calls the C function `exit` or `quick_exit`, or returns from C's `main` function
///
/// Note that if a binary contains multiple copies of the Rust runtime (e.g., when combining
/// multiple `cdylib` or `staticlib`), they each have their own separate lock, so from the
/// perspective of code running in one of the Rust runtimes, the "outside" Rust code is basically C
/// code, and concurrent `exit` again causes undefined behavior.
///
/// Individual C implementations might provide more guarantees than the standard and permit concurrent
/// calls to `exit`; consult the documentation of your C implementation for details.
///
/// For some of the on-going discussion to make `exit` thread-safe in C, see:
/// - [Rust issue #126600](https://github.com/rust-lang/rust/issues/126600)
/// - [Austin Group Bugzilla (for POSIX)](https://austingroupbugs.net/view.php?id=1845)
/// - [GNU C library Bugzilla](https://sourceware.org/bugzilla/show_bug.cgi?id=31997)
///
/// [C-exit]: https://en.cppreference.com/w/c/program/exit
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "process_exit")] #[cfg_attr(not(test), rustc_diagnostic_item = "process_exit")]
pub fn exit(code: i32) -> ! { pub fn exit(code: i32) -> ! {