Skip to content

Commit d9e5f7e

Browse files
authored
Merge pull request #794 from leancodepl/fix/flaky-razor-test-Stress_test_the_multithreading_part
Attempt to fix flakiness of `CompiledViewsCacheTests.Stress_test_the_multithreading_part()`
2 parents 9fcef58 + 53073ee commit d9e5f7e

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/Infrastructure/LeanCode.ViewRenderer.Razor/CompiledViewsCache.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ public ValueTask<CompiledView> GetOrCompileAsync(string viewName)
3939

4040
if (tcs == newTcs)
4141
{
42+
// Double-check the main cache. This handles the race condition where:
43+
// 1. We checked cache above → miss
44+
// 2. Another thread finished compilation, added to cache, and removed from buildCache
45+
// 3. We added our TCS to buildCache (which was now empty)
46+
// Without this check, we'd start a redundant compilation.
47+
if (cache.TryGetValue(viewName, out compiled))
48+
{
49+
logger.Verbose(
50+
"View type for {ViewName} was added to cache while we were setting up compilation",
51+
viewName
52+
);
53+
// Complete the TCS before removing - other threads may be waiting on it
54+
tcs.SetResult(compiled);
55+
buildCache.TryRemove(viewName, out _);
56+
return new ValueTask<CompiledView>(compiled);
57+
}
58+
4259
return new ValueTask<CompiledView>(WrappedCompileAsync(viewName, tcs));
4360
}
4461

0 commit comments

Comments
 (0)