diff --git a/types/src/state_store/state_key/registry.rs b/types/src/state_store/state_key/registry.rs index 222664b2b1b01..c56577eda6653 100644 --- a/types/src/state_store/state_key/registry.rs +++ b/types/src/state_store/state_key/registry.rs @@ -151,9 +151,13 @@ where fn maybe_remove(&self, key1: &Key1, key2: &Key2) { let mut locked = self.inner.write(); if let Some(map2) = locked.get_mut(key1) { - map2.remove(key2); - if map2.is_empty() { - locked.remove(key1); + if let Some(entry) = map2.get(key2) { + if entry.strong_count() == 0 { + map2.remove(key2); + if map2.is_empty() { + locked.remove(key1); + } + } } } }