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

Break lock reentrancy in StateKey registry #67

Merged
Merged
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
24 changes: 16 additions & 8 deletions types/src/state_store/state_key/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use move_core_types::{
};
use once_cell::sync::Lazy;
use std::{
mem,
borrow::Borrow,
hash::{Hash, Hasher},
sync::{Arc, Weak},
Expand Down Expand Up @@ -149,17 +150,24 @@ where
}

fn maybe_remove(&self, key1: &Key1, key2: &Key2) {
let mut locked = self.inner.write();
if let Some(map2) = locked.get_mut(key1) {
if let Some(entry) = map2.get(key2) {
if entry.upgrade().is_none() {
map2.remove(key2);
// If the entry is removed, it must be dropped outside of the lock
// to prevent lock reentrancy
let mut maybe_entry = None;
{
let mut locked = self.inner.write();
if let Some(map2) = locked.get_mut(key1) {
if let Some(entry) = map2.get(key2) {
maybe_entry = entry.upgrade();
if maybe_entry.is_none() {
map2.remove(key2);
}
}
if map2.is_empty() {
locked.remove(key1);
}
}
if map2.is_empty() {
locked.remove(key1);
}
}
mem::drop(maybe_entry);
}

pub fn get_or_add<Ref1, Ref2, Gen>(
Expand Down