Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6ab1805

Browse files
authoredAug 27, 2024
Rollup merge of rust-lang#129581 - RalfJung:exit, r=joshtriplett
exit: explain our expectations for the exit handlers registered in a Rust program This documents the position of ``@Amanieu`` and others in rust-lang#126600: a library with an atexit handler that destroys state that other threads could still be working on is buggy. We do not consider it acceptable for a library to say "you must call the following cleanup function before exiting from `main` or calling `exit`". I don't know if this is established ``@rust-lang/libs-api`` consensus so I presume this will have to go through FCP. Given that Rust supports concurrency, I don't think there is any way to write a sound Rust wrapper around a library that has such a required cleanup function: even if we made `exit` unsafe, and the Rust wrapper used the scope-with-callback approach to ensure it can run cleanup code before returning from the wrapper (like `thread::scope`), one could still call this wrapper in a second thread and then return from `main` while the wrapper runs. Making this sound would require `std` to provide a way to "block" returning from `main`, so that while the wrapper runs returning from `main` waits until the wrapper is done... that just doesn't seem feasible. The `exit` docs do not seem like the best place to document this, but I also couldn't think of a better one.
2 parents 849c240 + 21dd88f commit 6ab1805

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed
 

‎library/std/src/process.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,15 @@ impl Child {
22962296
/// }
22972297
/// ```
22982298
///
2299+
/// In its current implementation, this function will execute exit handlers registered with `atexit`
2300+
/// as well as other platform-specific exit handlers (e.g. `fini` sections of ELF shared objects).
2301+
/// This means that Rust requires that all exit handlers are safe to execute at any time. In
2302+
/// particular, if an exit handler cleans up some state that might be concurrently accessed by other
2303+
/// threads, it is required that the exit handler performs suitable synchronization with those
2304+
/// threads. (The alternative to this requirement would be to not run exit handlers at all, which is
2305+
/// considered undesirable. Note that returning from `main` also calls `exit`, so making `exit` an
2306+
/// unsafe operation is not an option.)
2307+
///
22992308
/// ## Platform-specific behavior
23002309
///
23012310
/// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`

0 commit comments

Comments
 (0)
Failed to load comments.