diff --git a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs index cf96674b..1e1f4910 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotLootCacheService.cs @@ -29,6 +29,7 @@ public class BotLootCacheService( private readonly Lock _grenadeLock = new(); private readonly Lock _specialLock = new(); private readonly Lock _healingLock = new(); + private readonly Lock _lootCacheGeneratedLock = new(); /// /// Remove cached bot loot data @@ -47,7 +48,7 @@ public class BotLootCacheService( /// Base json db file for the bot having its loot generated /// OPTIONAL - item price min and max value filter /// THIS IS NOT A THREAD SAFE METHOD - /// dictionary + /// dictionary, key = item tpl, value = weight public Dictionary GetLootFromCache( string botRole, bool isPmc, @@ -56,10 +57,14 @@ public class BotLootCacheService( MinMax? itemPriceMinMax = null ) { - if (!BotRoleExistsInCache(botRole)) + // Ensure cache is hydrated before use + lock (_lootCacheGeneratedLock) { - InitCacheForBotRole(botRole); - AddLootToCache(botRole, isPmc, botJsonTemplate); + if (!BotRoleExistsInCache(botRole)) + { + InitCacheForBotRole(botRole); + AddLootToCache(botRole, isPmc, botJsonTemplate); + } } if (!_lootCache.TryGetValue(botRole, out var botRoleCache))