Skip to content

Commit

Permalink
Fix potential deadlock in state_key::Entry::drop
Browse files Browse the repository at this point in the history
If before Entry::drop() calls Registry::maybe_remove, the weak ref under
the same key1 and key2 is replaced, Entry::drop can be called
recursively resulting in a deadlock. The crux is we try to see if the
entry has been replaced by trying to upgrading it to a strong ref, and
it possible that the upgrade is successful and the result becomes a only
ref to the new entry.

This tries to fix it by determining the same thing by seeing if
Weak::strong_count() is 0 instead of upgrading the weak ref.
  • Loading branch information
msmouse committed Sep 18, 2024
1 parent 09ce976 commit 91caac6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions types/src/state_store/state_key/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ where
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() {
if entry.strong_count() == 0 {
map2.remove(key2);
if map2.is_empty() {
locked.remove(key1);
}
}
}
if map2.is_empty() {
locked.remove(key1);
}
}
}

Expand Down

0 comments on commit 91caac6

Please sign in to comment.