Skip to content
Merged
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
23 changes: 19 additions & 4 deletions runtime/src/bank_forks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,12 +727,27 @@ mod tests {
solana_vote_program::vote_state::BlockTimestamp,
};

// This test verifies that BankForks::new_rw_arc() doesn't create a reference cycle.
//
// Before PR #1893, there was a cycle:
// Arc<RwLock<BankForks>> → Bank → ProgramCache → Arc<RwLock<BankForks>>
//
// This happened because new_rw_arc() called:
// root_bank.set_fork_graph_in_program_cache(bank_forks.clone())
//
// The fix changed it to use a Weak reference:
// root_bank.set_fork_graph_in_program_cache(Arc::downgrade(&bank_forks))
//
// Breaking the cycle:
// Arc<RwLock<BankForks>> → Bank → ProgramCache → Weak<RwLock<BankForks>>
//
// Without the fix: strong_count == 2 (the clone creates an extra strong ref)
// With the fix: strong_count == 1 (only the returned Arc holds a strong ref)
#[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));
}
let GenesisConfigInfo { genesis_config, .. } = create_genesis_config(10_000);
let bank_forks = BankForks::new_rw_arc(Bank::new_for_tests(&genesis_config));
assert_eq!(Arc::strong_count(&bank_forks), 1);
}

#[test]
Expand Down
Loading