Skip to content

[Blazor Hybrid] Guard HandleComponentException after renderer disposal - #70304

Merged
kotlarmilos merged 13 commits into
dotnet:mainfrom
irfanajaffer:HandleComponentException
Nov 16, 2026
Merged

kotlarmilos merged 13 commits into
dotnet:mainfrom
irfanajaffer:HandleComponentException

Conversation

@irfanajaffer

Copy link
Copy Markdown
Contributor

Description

Added a disposal guard in Renderer.HandleComponentException to prevent exception routing after the renderer has already been disposed.

The reported scenario involves DispatchExceptionAsync being invoked after component or renderer teardown in Blazor Hybrid applications. Previously, HandleComponentException could attempt to resolve component state after disposal, leading to failures when the renderer no longer contained the target component. This change ensures such late exception dispatches are safely ignored once renderer disposal has begun or completed.

Changes Made

  • Added a _rendererIsDisposed guard in Renderer.HandleComponentException.
  • Return early when exception dispatch occurs after renderer disposal.
  • Preserved existing behavior for active components and active renderers.
  • Added comprehensive regression coverage for exception dispatch scenarios around renderer disposal, component removal, and dispatcher ordering.

Testing

Added/updated unit tests covering:

  • DispatchExceptionAsync after renderer disposal does not throw.
  • DispatchExceptionAsync during renderer disposal does not throw.
  • DispatchExceptionAsync while async disposal is pending does not throw.
  • Dispatcher queue ordering between disposal and exception dispatch is preserved.
  • Active components continue to route exceptions through the nearest error boundary.
  • Active components without an error boundary continue to propagate exceptions.
  • Dispatching after component removal continues to throw as expected.
  • Dispatching from a live component after unrelated component removal continues to use the fatal exception handler.
image

Before and After Fix Evidences

Before Fix
After.Fix.mp4

Issue

Fixes #66322

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Nov 4, 2026
@irfanajaffer irfanajaffer changed the title Handle component exception [Blazor Hybrid] Guard HandleComponentException after renderer disposal Nov 4, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 4, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @irfanajaffer. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@irfanajaffer
irfanajaffer marked this pull request as ready for review November 5, 2026 03:23
@irfanajaffer
irfanajaffer requested a review from a team as a code owner November 5, 2026 03:23
Copilot AI balanced review requested due to automatic review settings November 5, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Independent review checks cannot be completed because the required frozen head/merge-base/base-tip bundle and existing-feedback snapshot are unavailable.

No open issues 🎉

What changed in this PR

Addresses #66322 by ignoring late component exception dispatches once renderer disposal has begun, preventing missing-component failures reported in Blazor Hybrid.

Changes:

  • Adds a disposal guard before component-state lookup and exception routing.
  • Adds regression tests for completed and pending disposal, dispatcher ordering, component removal, and unchanged exception routing for live components.
File Description
src/​Components/​Components/​test/​RendererTest.cs Adds disposal and exception-routing regression coverage.
src/​Components/​Components/​src/​RenderTree/​Renderer.cs Returns early from HandleComponentException when the renderer is disposed.

Review effort: Balanced

@PureWeen

PureWeen commented Nov 5, 2026

Copy link
Copy Markdown
Member

/review

@PureWeen

PureWeen commented Nov 6, 2026

Copy link
Copy Markdown
Member

Copilot's local review panel found no issues to raise at this commit.

@kotlarmilos kotlarmilos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Please reduce the testing coverage

@irfanajaffer

Copy link
Copy Markdown
Contributor Author

@kotlarmilos ,

Addressed. I reduced the test coverage to focus only on the scenarios directly validating the fix, removing the broader coverage that was not required for this change. The remaining tests verify the intended behavior while keeping the test impact minimal and aligned with the scope of the fix.

@kotlarmilos

Copy link
Copy Markdown
Member

@irfanajaffer Please take a look at the failing CI

@kotlarmilos
kotlarmilos self-requested a review November 10, 2026 09:19
@kotlarmilos
kotlarmilos merged commit 0443efd into dotnet:main Nov 16, 2026
32 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 8%

No docs needed: a bug-fix guard against exception dispatch after renderer disposal, with tests only.

Documentation surfaces considered

  • Conceptual article: Not required — Internal bug fix with regression tests; no public API/config/default change; signals: no_signal.
  • Migration guidance: Not required — No upgrade action needed.
  • Breaking change: Not required — No compatibility impact; late dispatches after disposal are ignored.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 7a7b5613 Deployed Nov 16, 2026 by kotlarmilos via conclusion #332
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-reviewed-approved area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor Hybrid] Consider guarding HandleComponentException when Renderer is Disposed

4 participants