Skip to content

Commit 2e90f6f

Browse files
committed
Use libc::pause instead of std::thread::park in wait-for-exit loop
1 parent a272844 commit 2e90f6f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

std/src/sys/pal/common/exit_guard.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ cfg_if::cfg_if! {
1010
} else if #[cfg(target_os = "linux")] {
1111
/// Mitigation for <https://github.com/rust-lang/rust/issues/126600>
1212
///
13-
/// On `unix` (where `libc::exit` may not be thread-safe), ensure that only one Rust thread
14-
/// calls `libc::exit` (or returns from `main`) by calling this function before calling
15-
/// `libc::exit` (or returning from `main`).
13+
/// On UNIX-like platforms (where `libc::exit` may not be thread-safe), ensure that only one
14+
/// Rust thread calls `libc::exit` (or returns from `main`) by calling this function before
15+
/// calling `libc::exit` (or returning from `main`).
1616
///
1717
/// Technically not enough to ensure soundness, since other code directly calling
1818
/// libc::exit will still race with this.
@@ -23,7 +23,7 @@ cfg_if::cfg_if! {
2323
/// This function will return only the first time it is called in a process.
2424
///
2525
/// * If it is called again on the same thread as the first call, it will abort.
26-
/// * If it is called again on a different thread, it will `thread::park()` in a loop
26+
/// * If it is called again on a different thread, it will wait in a loop
2727
/// (waiting for the process to exit).
2828
pub(crate) fn unique_thread_exit() {
2929
let this_thread_id = unsafe { libc::gettid() };
@@ -52,9 +52,10 @@ cfg_if::cfg_if! {
5252
}
5353
Err(_) => {
5454
// This is not the first thread to call `unique_thread_exit`.
55-
// Park until the process exits.
55+
// Pause until the process exits.
5656
loop {
57-
crate::thread::park();
57+
// Safety: libc::pause is safe to call.
58+
unsafe { libc::pause(); }
5859
}
5960
}
6061
}
@@ -77,10 +78,12 @@ cfg_if::cfg_if! {
7778
core::panicking::panic_nounwind("std::process::exit called re-entrantly")
7879
} else {
7980
// This is not the first thread to call `unique_thread_exit`.
81+
// Pause until the process exits.
8082
// Park until the process exits.
8183
drop(exiting_thread_id);
8284
loop {
83-
crate::thread::park();
85+
// Safety: libc::pause is safe to call.
86+
unsafe { libc::pause(); }
8487
}
8588
}
8689
}

0 commit comments

Comments
 (0)