Skip to main content

kithara_platform/
env.rs

1/// Returns a process-wide mutex that serializes mutations of `std::env`
2/// across threads.
3///
4/// `std::env::set_var` and `remove_var` are not thread-safe in the
5/// presence of concurrent reads from other threads. Code that mutates
6/// the process environment (test setup, startup-time configuration,
7/// `#[kithara::test]` proc-macro env-guard) should hold this lock
8/// across the mutation to prevent races.
9#[must_use]
10pub fn env_mutation_lock() -> &'static std::sync::Mutex<()> {
11    static LOCK: std::sync::OnceLock<std::sync::Mutex<()>> = std::sync::OnceLock::new();
12    LOCK.get_or_init(|| std::sync::Mutex::new(()))
13}