Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

runtime: fix possible deadlock in in_mem_accounts_index #26046

Merged
merged 1 commit into from
Jun 23, 2022

Conversation

BurtonQin
Copy link
Contributor

Problem

There is a possible deadlock in in_mem_accounts_index.

self.map() returns &RwLock<...>.

The first Read lock in fn items:

let map = self.map().read().unwrap();
let mut result = Vec::with_capacity(map.len());
map.iter().for_each(|(k, v)| {
if range.contains(k) {
result.push((*k, Arc::clone(v)));
}
});
self.hold_range_in_memory(range, false);

pub fn hold_range_in_memory<R>(&self, range: &R, start_holding: bool)
where
R: RangeBounds<Pubkey> + Debug,
{
let evictions_guard = EvictionsGuard::lock(self);
if !start_holding || !self.add_hold_range_in_memory_if_already_held(range, &evictions_guard)
{
if start_holding {
// put everything in the cache and it will be held there
self.put_range_in_cache(&Some(range), &evictions_guard);

The second Write lock in fn put_range_in_cache:

fn put_range_in_cache<R>(&self, range: &Option<&R>, _evictions_guard: &EvictionsGuard)
where
R: RangeBounds<Pubkey>,
{
assert!(self.get_stop_evictions()); // caller should be controlling the lifetime of how long this needs to be present
let m = Measure::start("range");
let mut added_to_mem = 0;
// load from disk
if let Some(disk) = self.bucket.as_ref() {
let mut map = self.map().write().unwrap();

Summary of Changes

Add drop(map) before calling fn hold_range_in_memory

Fixes #

@mergify mergify bot added the community Community contribution label Jun 18, 2022
@mergify mergify bot requested a review from a team June 18, 2022 03:15
@jstarry jstarry requested review from brooksprumo and removed request for a team June 23, 2022 15:57
Copy link
Contributor

@brooksprumo brooksprumo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thank you for fixing!

@brooksprumo brooksprumo merged commit 355e09e into solana-labs:master Jun 23, 2022
mergify bot pushed a commit that referenced this pull request Jun 23, 2022
mergify bot added a commit that referenced this pull request Jun 23, 2022
)

(cherry picked from commit 355e09e)

Co-authored-by: Boqin Qin(秦 伯钦) <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community Community contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants