Skip to content
Closed
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions program-runtime/src/loaded_programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use {
fmt::{Debug, Formatter},
sync::{
atomic::{AtomicU64, Ordering},
Arc, Condvar, Mutex, RwLock,
Arc, Condvar, Mutex, RwLock, Weak,
},
},
};
Expand Down Expand Up @@ -549,7 +549,7 @@ pub struct LoadedPrograms<FG: ForkGraph> {
/// List of loaded programs which should be recompiled before the next epoch (but don't have to).
pub programs_to_recompile: Vec<(Pubkey, Arc<LoadedProgram>)>,
pub stats: Stats,
pub fork_graph: Option<Arc<RwLock<FG>>>,
pub fork_graph: Option<Weak<RwLock<FG>>>,
pub loading_task_waiter: Arc<LoadingTaskWaiter>,
}

Expand Down Expand Up @@ -691,7 +691,7 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
}
}

pub fn set_fork_graph(&mut self, fork_graph: Arc<RwLock<FG>>) {
pub fn set_fork_graph(&mut self, fork_graph: Weak<RwLock<FG>>) {
self.fork_graph = Some(fork_graph);
}

Expand Down Expand Up @@ -797,6 +797,7 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
error!("Program cache doesn't have fork graph.");
return;
};
let fork_graph = fork_graph.upgrade().unwrap();
let Ok(fork_graph) = fork_graph.read() else {
error!("Failed to lock fork graph for reading.");
return;
Expand Down Expand Up @@ -929,7 +930,8 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
is_first_round: bool,
) -> Option<(Pubkey, u64)> {
debug_assert!(self.fork_graph.is_some());
let locked_fork_graph = self.fork_graph.as_ref().unwrap().read().unwrap();
let fork_graph = self.fork_graph.as_ref().unwrap().upgrade().unwrap();
let locked_fork_graph = fork_graph.read().unwrap();
let mut cooperative_loading_task = None;
search_for.retain(|(key, (match_criteria, usage_count))| {
if let Some(second_level) = self.entries.get_mut(key) {
Expand Down Expand Up @@ -1031,6 +1033,8 @@ impl<FG: ForkGraph> LoadedPrograms<FG> {
self.fork_graph
.as_ref()
.unwrap()
.upgrade()
.unwrap()
.read()
.unwrap()
.relationship(loaded_program.deployment_slot, slot),
Expand Down
10 changes: 9 additions & 1 deletion runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl BankForks {
.loaded_programs_cache
.write()
.unwrap()
.set_fork_graph(bank_forks.clone());
.set_fork_graph(Arc::downgrade(&bank_forks));

bank_forks
}
Expand Down Expand Up @@ -734,6 +734,14 @@ mod tests {
std::{sync::atomic::Ordering::Relaxed, time::Duration},
};

#[test]
fn test_bank_forks_new_rw_arc_memory_leak() {
for _ in 0..1000 {
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
BankForks::new_rw_arc(Bank::new_for_tests(&genesis_config));
}
}

#[test]
fn test_bank_forks_new() {
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
Expand Down