Skip to content

Commit 4f7b287

Browse files
committed
Document that init and cleanup are not guaranteed to run
1 parent d074abf commit 4f7b287

File tree

6 files changed

+13
-2
lines changed

6 files changed

+13
-2
lines changed

std/src/sys/hermit/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ pub extern "C" fn __rust_abort() {
9696
}
9797

9898
// SAFETY: must be called only once during runtime initialization.
99+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
99100
pub unsafe fn init(argc: isize, argv: *const *const u8) {
100101
let _ = net::init();
101102
args::init(argc, argv);
102103
}
103104

104105
// SAFETY: must be called only once during runtime cleanup.
106+
// NOTE: this is not guaranteed to run, for example when the program aborts.
105107
pub unsafe fn cleanup() {
106108
args::cleanup();
107109
}

std/src/sys/sgx/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ pub mod time;
4040
pub use crate::sys_common::os_str_bytes as os_str;
4141

4242
// SAFETY: must be called only once during runtime initialization.
43+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
4344
pub unsafe fn init(argc: isize, argv: *const *const u8) {
4445
unsafe {
4546
args::init(argc, argv);
4647
}
4748
}
4849

4950
// SAFETY: must be called only once during runtime cleanup.
51+
// NOTE: this is not guaranteed to run, for example when the program aborts.
5052
pub unsafe fn cleanup() {}
5153

5254
/// This function is used to implement functionality that simply doesn't exist.

std/src/sys/unix/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub mod time;
4545
pub use crate::sys_common::os_str_bytes as os_str;
4646

4747
// SAFETY: must be called only once during runtime initialization.
48+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
4849
pub unsafe fn init(argc: isize, argv: *const *const u8) {
4950
// The standard streams might be closed on application startup. To prevent
5051
// std::io::{stdin, stdout,stderr} objects from using other unrelated file
@@ -120,6 +121,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
120121
}
121122

122123
// SAFETY: must be called only once during runtime cleanup.
124+
// NOTE: this is not guaranteed to run, for example when the program aborts.
123125
pub unsafe fn cleanup() {
124126
args::cleanup();
125127
stack_overflow::cleanup();

std/src/sys/unsupported/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ pub use crate::sys_common::os_str_bytes as os_str;
1111
use crate::os::raw::c_char;
1212

1313
// SAFETY: must be called only once during runtime initialization.
14+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
1415
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
1516

1617
// SAFETY: must be called only once during runtime cleanup.
18+
// NOTE: this is not guaranteed to run, for example when the program aborts.
1719
pub unsafe fn cleanup() {}
1820

1921
pub fn unsupported<T>() -> std_io::Result<T> {

std/src/sys/windows/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ cfg_if::cfg_if! {
5050
}
5151

5252
// SAFETY: must be called only once during runtime initialization.
53+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
5354
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
5455
stack_overflow::init();
5556
}
5657

5758
// SAFETY: must be called only once during runtime cleanup.
59+
// NOTE: this is not guaranteed to run, for example when the program aborts.
5860
pub unsafe fn cleanup() {
5961
net::cleanup();
6062
}

std/src/sys_common/rt.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::thread::Thread;
55

66
// One-time runtime initialization.
77
// Runs before `main`.
8+
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
89
#[cfg_attr(test, allow(dead_code))]
910
pub fn init(argc: isize, argv: *const *const u8) {
1011
static INIT: Once = Once::new();
@@ -23,8 +24,8 @@ pub fn init(argc: isize, argv: *const *const u8) {
2324
}
2425

2526
// One-time runtime cleanup.
26-
// Runs after `main` or at program exit. Note however that this is not guaranteed to run,
27-
// for example when the program aborts.
27+
// Runs after `main` or at program exit.
28+
// NOTE: this is not guaranteed to run, for example when the program aborts.
2829
#[cfg_attr(test, allow(dead_code))]
2930
pub fn cleanup() {
3031
static CLEANUP: Once = Once::new();

0 commit comments

Comments
 (0)