File tree Expand file tree Collapse file tree
src/Infrastructure/LeanCode.ViewRenderer.Razor Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments