Skip to content

Conversation

@dariatiurina
Copy link
Contributor

Fix empty render of the ErrorBoundary

Description

This PR fixes a bug where ErrorBoundary renders empty content instead of ErrorContent when multiple child components throw errors in quick succession (e.g., errors thrown inside a foreach loop like in the test example).

How it works normally (single error):

The Renderer queues an empty render to clear the subtree, then calls HandleException, which queues the ErrorContent render. This works correctly.

The bug (multiple errors):

When a second error is thrown:

  1. Renderer queues another empty render - this sets _hasPendingQueuedRender = true
  2. Renderer calls HandleException to queue the ErrorContent render
  3. StateHasChanged() returns early because _hasPendingQueuedRender is still true from step 1
  4. The ErrorContent render is never queued

The queue ends up with: [empty, ErrorContent, empty] instead of [empty, ErrorContent, empty, ErrorContent]. After the first ErrorContent renders, only the empty render remains in the queue, clearing the error UI.

Solution:

After queuing the empty render but before calling HandleException, manually reset _hasPendingQueuedRender to false using the new internal HasPendingQueuedRender property. This ensures StateHasChanged() successfully queues the ErrorContent render for each error.

Fixes #56950

@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Nov 25, 2025
@dariatiurina dariatiurina self-assigned this Nov 25, 2025
/// <summary>
/// Gets or sets a value that indicates whether there is a pending queued render.
/// </summary>
internal bool HasPendingQueuedRender
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way how we could implemement this fix for ErrorBoundary components without reducing encapsulation for every component? E.g. by overriding StateHasChanged for ErrorBoundary.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor ErrorBoundary error content not shown when exception occurs inside @foreach

2 participants