From 9a30e1b32d3c743b46c262f25b11d7e833f462c8 Mon Sep 17 00:00:00 2001 From: zachs18 <8355914+zachs18@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:02:36 -0500 Subject: [PATCH] Use a global lock for `exit` on unix. --- library/std/src/sys/pal/unix/os.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index 2e71ceceb58b1..1752fd78b322d 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -758,7 +758,13 @@ pub fn home_dir() -> Option { } pub fn exit(code: i32) -> ! { - unsafe { libc::exit(code as c_int) } + use crate::sync::OnceLock; + // https://github.com/rust-lang/rust/issues/126600 + // Ensure that only one Rust thread calls exit. + // Technically not enough to ensure soundness, since other code directly calling + // libc::exit will still race with this. + static EXIT_ONCE: OnceLock = OnceLock::new(); + *EXIT_ONCE.get_or_init(|| unsafe { libc::exit(code as c_int) }) } pub fn getpid() -> u32 {