Repository navigation
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved issues were identified, and unit and E2E coverage validate the fix.
Pull request overview
Hardens RendererSynchronizationContext restoration to prevent context leakage and dispatcher bypass when execution-context flow is suppressed.
Changes:
- Reorders queue-marker completion and context restoration.
- Adds deterministic unit regression coverage.
- Adds server component E2E coverage.
File summaries
| File | Description |
|---|---|
src/Components/test/testassets/BasicTestApp/DispatchingComponent.razor |
Adds the suppressed-flow regression scenario. |
src/Components/test/E2ETest/ServerExecutionTests/ServerComponentRenderingTest.cs |
Verifies context restoration and dispatcher serialization. |
src/Components/Components/test/Rendering/RendererSynchronizationContextTest.cs |
Adds focused unit regression coverage. |
src/Components/Components/src/Rendering/RendererSynchronizationContext.cs |
Safely orders queue completion and context restoration. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
| ThreadPool.UnsafeQueueUserWorkItem(_ => | ||
| { | ||
| var originalContext = SynchronizationContext.Current; | ||
| InvokeAsync(async () => |
There was a problem hiding this comment.
Non-blocking: this only reproduces the leak if the holder's InvokeAsync runs inline, which requires the dispatcher to be idle at exactly this point. If other circuit work holds the dispatcher here (a render batch, post-render work from the click, JS interop callbacks), the lambda gets queued instead. Then nothing leaks even without the fix, and the test still sees Context leaked: False; Dispatcher overlapped: False. So this E2E can pass without exercising the bug; the new RendererSynchronizationContextTest case is the real regression oracle.
A cheap way to make it self-checking is to record whether the lambda actually ran on the holder thread and include that in the result:
var holderThreadId = Environment.CurrentManagedThreadId;
InvokeAsync(async () =>
{
ranInline = Environment.CurrentManagedThreadId == holderThreadId;
rendererEntered.Set();
...and assert "Ran inline: True; Context leaked: False; Dispatcher overlapped: False".
Why
Fixes #69323.
When queued renderer work is submitted without a flowed
ExecutionContext, its continuation can inline onto the thread that releases the renderer queue.PostAsyncthen installs the renderer synchronization context on that caller-owned thread without an execution-context or thread-pool boundary to restore it. SubsequentCheckAccesscalls can incorrectly succeed and bypass dispatcher serialization.The reported scenario is not a valid Blazor component pattern. It suppresses execution-context flow while crossing the component dispatcher boundary. Although
ExecutionContext.SuppressFlow()andConfigureAwait(false)are different runtime mechanisms, the same ownership rule applies: component code that deliberately leaves Blazor's ambient execution context must not access component state until it returns through the dispatcher.Using
ConfigureAwait(false)inside a nested implementation method is fine when that method does not access component state after the context-free await and the component-facing caller awaits its returned task normally. The caller then resumes on Blazor's synchronization context. Similarly, a library can isolate its own background work, but should restore normal flow before invoking the component's dispatcher.This change does not make context-free component execution supported. It defensively prevents the renderer synchronization context from escaping onto a caller-owned thread when unsupported scheduling primitives are combined in this way.
What changed
RendererSynchronizationContextis still current, preventing queued continuations from inlining onto the caller's thread.How to verify
. .\activate.ps1; dotnet test src\Components\Components\test\Microsoft.AspNetCore.Components.Tests.csproj --no-restore --filter "FullyQualifiedName~RendererSynchronizationContextTest" -v:q -p:UseIisNativeAssets=false. .\activate.ps1; dotnet test src\Components\test\E2ETest\Microsoft.AspNetCore.Components.E2ETests.csproj --no-restore --filter "FullyQualifiedName~ServerComponentRenderingTest.RestoresContextWhenQueuedWorkDoesNotFlowExecutionContext" -v:q -p:UseIisNativeAssets=false -p:EnforceE2ETestPrerequisites=trueInteractiveServercomponent repro reportedContext leaked: FalseandDispatcher overlapped: Falsetwice with the change.Risks / follow-ups
Completing the queue marker while the renderer context is current can add an asynchronous scheduling hop where a continuation previously inlined. This aligns the affected async/
Sendpath with the synchronous dispatcher overloads, which already complete their builders before restoring the context.No follow-up is currently required.