Skip to content

Commit 5b45a59

Browse files
authored
Fix atomicity violation in AtomicSamplingReservoir::consume (#606)
Signed-off-by: quantpoet <[email protected]>
1 parent 13cbf24 commit 5b45a59

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

metrics-util/src/storage/reservoir.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
sync::{
77
atomic::{
88
AtomicBool, AtomicUsize,
9-
Ordering::{Acquire, Relaxed, Release},
9+
Ordering::{AcqRel, Acquire, Relaxed, Release},
1010
},
1111
Mutex,
1212
},
@@ -176,9 +176,8 @@ impl AtomicSamplingReservoir {
176176
{
177177
let _guard = self.swap.lock().unwrap();
178178

179-
// Swap the active reservoir.
180-
let use_primary = self.use_primary.load(Acquire);
181-
self.use_primary.store(!use_primary, Release);
179+
// Swap the active reservoir atomically.
180+
let use_primary = self.use_primary.fetch_xor(true, AcqRel);
182181

183182
// Consume the previous reservoir.
184183
let drain = if use_primary { self.primary.drain() } else { self.secondary.drain() };

0 commit comments

Comments
 (0)