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

Commit 0a638a8

Browse files
author
Boqin Qin
authored
runtime: fix possible deadlock in accounts_db (#10469)
1 parent 75b8c2c commit 0a638a8

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

runtime/src/accounts_db.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::{
4242
ops::RangeBounds,
4343
path::{Path, PathBuf},
4444
sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
45-
sync::{Arc, Mutex, RwLock},
45+
sync::{Arc, Mutex, RwLock, RwLockReadGuard},
4646
time::Instant,
4747
};
4848
use tempfile::TempDir;
@@ -964,10 +964,21 @@ impl AccountsDB {
964964
F: Fn(&StoredAccount, AppendVecId, &mut B) + Send + Sync,
965965
B: Send + Default,
966966
{
967-
let storage_maps: Vec<Arc<AccountStorageEntry>> = self
968-
.storage
969-
.read()
970-
.unwrap()
967+
self.scan_account_storage_inner(slot, scan_func, &self.storage.read().unwrap())
968+
}
969+
970+
// The input storage must come from self.storage.read().unwrap()
971+
fn scan_account_storage_inner<F, B>(
972+
&self,
973+
slot: Slot,
974+
scan_func: F,
975+
storage: &RwLockReadGuard<AccountStorage>,
976+
) -> Vec<B>
977+
where
978+
F: Fn(&StoredAccount, AppendVecId, &mut B) + Send + Sync,
979+
B: Send + Default,
980+
{
981+
let storage_maps: Vec<Arc<AccountStorageEntry>> = storage
971982
.0
972983
.get(&slot)
973984
.unwrap_or(&HashMap::new())
@@ -1806,10 +1817,10 @@ impl AccountsDB {
18061817
}
18071818

18081819
pub fn generate_index(&self) {
1820+
let mut accounts_index = self.accounts_index.write().unwrap();
18091821
let storage = self.storage.read().unwrap();
18101822
let mut slots: Vec<Slot> = storage.0.keys().cloned().collect();
18111823
slots.sort();
1812-
let mut accounts_index = self.accounts_index.write().unwrap();
18131824

18141825
let mut last_log_update = Instant::now();
18151826
for (index, slot) in slots.iter().enumerate() {
@@ -1820,7 +1831,7 @@ impl AccountsDB {
18201831
}
18211832

18221833
let accumulator: Vec<HashMap<Pubkey, Vec<(u64, AccountInfo)>>> = self
1823-
.scan_account_storage(
1834+
.scan_account_storage_inner(
18241835
*slot,
18251836
|stored_account: &StoredAccount,
18261837
store_id: AppendVecId,
@@ -1835,6 +1846,7 @@ impl AccountsDB {
18351846
.or_insert_with(Vec::new);
18361847
entry.push((stored_account.meta.write_version, account_info));
18371848
},
1849+
&storage,
18381850
);
18391851

18401852
let mut accounts_map: HashMap<Pubkey, Vec<(u64, AccountInfo)>> = HashMap::new();

0 commit comments

Comments
 (0)