Skip to content

Commit

Permalink
Fix deadlock on python new fundamentals (#7546)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero authored Oct 31, 2023
1 parent 43585ac commit b21b6b9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Common/Data/Fundamental/FundamentalInstanceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,24 @@ public class FundamentalInstanceProvider
/// <returns>The unique instance provider</returns>
public static FundamentalInstanceProvider Get(Symbol symbol)
{
lock(_cache)
FundamentalInstanceProvider result = null;
lock (_cache)
{
if (!_cache.TryGetValue(symbol.ID, out var result))
_cache.TryGetValue(symbol.ID, out result);
}

if (result == null)
{
// we create the fundamental instance provider without holding the cache lock, this is because it uses the pygil
// Deadlock case: if the main thread has PyGil and wants to take lock on cache (security.Fundamentals use case) and the data
// stack thread takes the lock on the cache (creating new fundamentals) and next wants the pygil deadlock!
result = new FundamentalInstanceProvider(symbol);
lock (_cache)
{
_cache[symbol.ID] = result = new FundamentalInstanceProvider(symbol);
_cache[symbol.ID] = result;
}
return result;
}
return result;
}

/// <summary>
Expand Down

0 comments on commit b21b6b9

Please sign in to comment.