Skip to content

ErrorBoundary fails to show error content when exception occurs inside @foreach - #67177

Closed
Vinoth2562000 wants to merge 11 commits into
dotnet:mainfrom
Vinoth2562000:56950-ErrorBoundary
Closed

Vinoth2562000 wants to merge 11 commits into
dotnet:mainfrom
Vinoth2562000:56950-ErrorBoundary

Conversation

@Vinoth2562000

@Vinoth2562000 Vinoth2562000 commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

ErrorBoundary fails to show error content when exception occurs inside @foreach.

Description

When multiple child components inside a parent (e.g., rendered via an @foreach loop) throw during the same render batch, the ErrorBoundary could incorrectly render blank content instead of its ErrorContent.

This occurs because the renderer queues an empty render to clear the failed subtree, which sets a component-local pending-render flag. The ErrorBoundary’s subsequent StateHasChanged() call was blocked by that flag, preventing the ErrorContent render from being queued.

This PR introduces a one-time permit mechanism that allows the boundary to bypass the pending-render guard and ensure ErrorContent is displayed.

Output gif:

msedge_sWfsSmfln3

Fixes #56950

@Vinoth2562000
Vinoth2562000 requested a review from a team as a code owner June 12, 2026 09:52
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 12, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@Vinoth2562000

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Syncfusion, Inc."

Copilot AI lite review requested due to automatic review settings August 20, 2026 11:30

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.

Pull request overview

Fixes a Blazor rendering edge case where ErrorBoundary could end up rendering blank output (instead of ErrorContent) when multiple child components throw during the same render batch (e.g., inside an @foreach). The change introduces a one-time “render permit” that lets an error boundary bypass ComponentBase’s pending-render coalescing guard so its HandleException-triggered re-render can still be queued after the renderer forces a subtree clear.

Changes:

  • Add a renderer-managed one-time bypass to allow an error boundary to queue an ErrorContent render even if _hasPendingQueuedRender is already set.
  • Update ComponentBase.StateHasChanged() to honor the one-time bypass and reset the pending-render flag appropriately.
  • Add regression tests covering single-child and multi-child render exceptions to ensure the boundary stays in an error-rendered state.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Components/Components/src/RenderTree/Renderer.cs Adds one-time render bypass permit and grants it when enqueueing the forced-empty render for error boundaries.
src/Components/Components/src/RenderHandle.cs Exposes an internal hook for ComponentBase to consume the renderer’s one-time permit.
src/Components/Components/src/ComponentBase.cs Updates StateHasChanged() guard logic to allow a one-time bypass when a permit is present.
src/Components/Components/test/ComponentBaseTest.cs Adds regression tests and a render-throw toggle for the test component.
Suppressed comments (2)

src/Components/Components/test/ComponentBaseTest.cs:477

  • This test indexes frames.Array[0] without checking frames.Count. If the boundary rendered an empty subtree, frames.Count could be 0 while frames.Array[0] contains stale data, leading to a false-positive. Assert frames.Count before indexing.
        frames = renderer.GetCurrentRenderTreeFrames(errorBoundaryId);
        Assert.Equal(RenderTree.RenderTreeFrameType.Element, frames.Array[0].FrameType);
        Assert.Equal("div", frames.Array[0].ElementName);

src/Components/Components/test/ComponentBaseTest.cs:517

  • This test indexes frames.Array[0] without checking frames.Count. Since ArrayRange<T> can retain stale values when Count == 0, the assertion could pass even if nothing was rendered. Assert frames.Count before indexing.
        var frames = renderer.GetCurrentRenderTreeFrames(errorBoundaryId);
        Assert.Equal(RenderTree.RenderTreeFrameType.Element, frames.Array[0].FrameType);
        Assert.Equal("div", frames.Array[0].ElementName);

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@dariatiurina

Copy link
Copy Markdown
Contributor

Hi @Vinoth2562000, thanks a lot for taking the time to look into this!

I did some work on this issue previously and actually explored a very similar approach, so I wanted to share what we learned. The concern with this direction is that it introduces coupling between ComponentBase and Renderer. The renderer ends up reaching into a component's internal render state to tell it that its queued render was superseded. We'd like to keep those two independent, with neither dictating the other's internal behavior, so unfortunately we don't want to take this particular approach.

Your work is appreciated, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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 ErrorBoundary error content not shown when exception occurs inside @foreach

6 participants