diff --git a/accounts-db/src/accounts_index/in_mem_accounts_index.rs b/accounts-db/src/accounts_index/in_mem_accounts_index.rs index 7d7a810afb06d8..5bd40a695ff327 100644 --- a/accounts-db/src/accounts_index/in_mem_accounts_index.rs +++ b/accounts-db/src/accounts_index/in_mem_accounts_index.rs @@ -769,13 +769,25 @@ impl + Into> InMemAccountsIndex::lock_and_update_slot_list( + let updated_slot_list_len = InMemAccountsIndex::::lock_and_update_slot_list( occupied.get(), (slot, account_info), None, // should be None because we don't expect a different slot # during index generation &mut Vec::default(), UpsertReclaim::IgnoreReclaims, ); + + // In case of a race condition, multiple threads try to insert + // to the same pubkey with different slots. We only need to + // record `other_slot` once. If the slot list length after + // update is not 2, it means that someone else has already + // recorded `other_slot` before us. Therefore, We don't need to + // record it again. + if updated_slot_list_len != 2 { + // clear `other_slot` if we don't win the race. + other_slot = None; + } + ( true, /* found in mem */ true, /* already existed */