From f8a2f31ae48fbcf8b71684851b3652007a7094d7 Mon Sep 17 00:00:00 2001 From: ShE3py <52315535+she3py@users.noreply.github.com> Date: Sun, 20 Aug 2023 21:50:45 +0200 Subject: [PATCH] Add data race test to `std::env::{get, set}` --- library/std/tests/env.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/library/std/tests/env.rs b/library/std/tests/env.rs index 96b4f372b8b10..a1ca85c2145f5 100644 --- a/library/std/tests/env.rs +++ b/library/std/tests/env.rs @@ -5,6 +5,7 @@ use rand::distributions::{Alphanumeric, DistString}; mod common; use common::test_rng; +use std::thread; #[track_caller] fn make_rand_name() -> OsString { @@ -140,3 +141,22 @@ fn env_home_dir() { } } } + +#[test] // miri shouldn't detect any data race in this fn +#[cfg_attr(any(not(miri), target_os = "emscripten"), ignore)] +fn test_env_get_set_multithreaded() { + let getter = thread::spawn(|| { + for _ in 0..100 { + let _ = var_os("foo"); + } + }); + + let setter = thread::spawn(|| { + for _ in 0..100 { + set_var("foo", "bar"); + } + }); + + let _ = getter.join(); + let _ = setter.join(); +}