Skip to content

Commit d740f9b

Browse files
authored
Fix atomicity violations in concurrent cache operations (#1559)
1 parent b4fe7fe commit d740f9b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/command_helpers.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ impl CargoOutput {
6666
}
6767

6868
pub(crate) fn print_debug(&self, arg: &dyn Display) {
69-
if self.metadata && !self.checked_dbg_var.load(Ordering::Relaxed) {
70-
self.checked_dbg_var.store(true, Ordering::Relaxed);
69+
if self.metadata
70+
&& self
71+
.checked_dbg_var
72+
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
73+
.is_ok()
74+
{
7175
println!("cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT");
7276
}
7377
if self.debug {

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,9 +4326,8 @@ fn is_disabled() -> bool {
43264326
0 => {
43274327
let truth = compute_is_disabled();
43284328
let encoded_truth = if truth { 2u8 } else { 1 };
4329-
// Might race against another thread, but we'd both be setting the
4330-
// same value so it should be fine.
4331-
CACHE.store(encoded_truth, Relaxed);
4329+
// Use compare_exchange to avoid race condition
4330+
let _ = CACHE.compare_exchange(0, encoded_truth, Relaxed, Relaxed);
43324331
truth
43334332
}
43344333
_ => unreachable!(),

0 commit comments

Comments
 (0)