Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash on lazy_static init race #128

Merged
merged 3 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ impl<T: 'static> Lazy<T> {
Some(v) => v,
None => {
// Init the value out of the `rt::execution`
let mut sv = crate::rt::lazy_static::StaticValue::new((self.init)());
let sv = crate::rt::lazy_static::StaticValue::new((self.init)());

// While calling init, we may have yielded to the scheduler, in which case some
// _other_ thread may have initialized the static. The real lazy_static does not
// have this issue, since it takes a lock before initializing the new value, and
// readers wait on that lock if they encounter it. We could implement that here
// too, but for simplicity's sake, we just do another try_get here for now.
if let Some(v) = unsafe { self.try_get() } {
return v;
}

rt::execution(|execution| {
let sv = execution.lazy_statics.init_static(self, sv);

// lazy_static uses std::sync::Once, which does a swap(AcqRel) to set
sv.sync.sync_store(&mut execution.threads, Ordering::AcqRel);

execution.lazy_statics.init_static(self, sv);
});

unsafe { self.try_get() }.expect("bug")
Expand Down
13 changes: 9 additions & 4 deletions src/rt/lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ impl Set {
&mut self,
key: &'static crate::lazy_static::Lazy<T>,
value: StaticValue,
) {
assert!(self
) -> &mut StaticValue {
let v = self
.statics
.as_mut()
.expect("attempted to access lazy_static during shutdown")
.insert(StaticKeyId::new(key), value)
.is_none())
.entry(StaticKeyId::new(key));

if let std::collections::hash_map::Entry::Occupied(_) = v {
unreachable!("told to init static, but it was already init'd");
}

v.or_insert(value)
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@ use std::sync::Arc;
loom::lazy_static! {
static ref A: AtomicUsize = AtomicUsize::new(0);
static ref NO_LEAK: loom::sync::Arc<usize> = Default::default();
static ref ARC_WITH_SLOW_CONSTRUCTOR: loom::sync::Arc<usize> = { thread::yield_now(); Default::default() };
}

loom::thread_local! {
static B: usize = A.load(Relaxed);
}

#[test]
#[should_panic]
fn lazy_static_arc_shutdown() {
loom::model(|| {
// note that we are not waiting for this thread,
// so it may access the static during shutdown,
// which is not okay.
thread::spawn(|| {
assert_eq!(**NO_LEAK, 0);
});
});
}

#[test]
fn lazy_static_arc_race() {
loom::model(|| {
let jh = thread::spawn(|| {
assert_eq!(**ARC_WITH_SLOW_CONSTRUCTOR, 0);
});
assert_eq!(**ARC_WITH_SLOW_CONSTRUCTOR, 0);

jh.join().unwrap();
});
}

#[test]
fn lazy_static_arc_doesnt_leak() {
loom::model(|| {
Expand Down