Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upRegion-is-free check is unsound due to a race condition #2
Comments
This comment has been minimized.
This comment has been minimized.
dtolnay
commented
Dec 9, 2018
|
Here is code to trigger the race condition. If you run this a few times with use region_buffer::region_buffer;
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;
fn main() {
let rb = Arc::new(RwLock::new(region_buffer![0]));
let latch = rb.write().unwrap();
let mut threads = Vec::new();
for _ in 0..10 {
let rb = rb.clone();
threads.push(thread::spawn(move || {
let rb = rb.read().unwrap();
rb.get_mut(0)
}));
}
thread::sleep(Duration::from_millis(100));
drop(latch);
let mut count = 0;
for thread in threads {
if thread.join().is_ok() {
count += 1;
}
}
println!("COUNT = {}", count);
} |
This comment has been minimized.
This comment has been minimized.
|
fixed by #5 |
Aaronepower
closed this
Dec 10, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dtolnay commentedDec 9, 2018
region_buffer/src/lib.rs
Lines 197 to 199 in 45d6c7b
This check is not thread safe. RegionBuffer implements Sync so some other thread may have mutably claimed the same region in between those two lines. This is unsound because you would end up having handed out multiple mutable borrows of the same data.