Skip to content

Commit 80bc769

Browse files
committed
Fix CompileAsync race condition in expression cache
Replace TryAdd with indexer assignment to prevent duplicate key exception when multiple threads compile the same expression.
1 parent 60b3eca commit 80bc769

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Algo/TraderHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,11 +1063,11 @@ public static async Task<ExpressionFormula<TResult>> CompileAsync<TResult>(this
10631063
{
10641064
var cache = CacheHolder<TResult>.Cache;
10651065

1066-
if (!cache.TryGetValue(expression, out var formula))
1067-
{
1068-
formula = await CodeExtensions.GetCSharpCompiler().Compile<TResult>(tracker, fileSystem, expression, ServicesRegistry.TryCompilerCache, cancellationToken);
1069-
cache.TryAdd(expression, formula);
1070-
}
1066+
if (cache.TryGetValue(expression, out var formula))
1067+
return formula;
1068+
1069+
formula = await CodeExtensions.GetCSharpCompiler().Compile<TResult>(tracker, fileSystem, expression, ServicesRegistry.TryCompilerCache, cancellationToken);
1070+
cache[expression] = formula;
10711071

10721072
return formula;
10731073
}

0 commit comments

Comments
 (0)