Repository navigation
ErrorBoundary fails to show error content when exception occurs inside @foreach - #67177
Vinoth2562000 wants to merge 11 commits into
Conversation
… exceptions within foreach loops propagate correctly to display error UI.
|
Thanks for your PR, @Vinoth2562000. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
@dotnet-policy-service agree company="Syncfusion, Inc." |
There was a problem hiding this comment.
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
ErrorContentrender even if_hasPendingQueuedRenderis 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 checkingframes.Count. If the boundary rendered an empty subtree,frames.Countcould be 0 whileframes.Array[0]contains stale data, leading to a false-positive. Assertframes.Countbefore 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 checkingframes.Count. SinceArrayRange<T>can retain stale values whenCount == 0, the assertion could pass even if nothing was rendered. Assertframes.Countbefore 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.
|
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 Your work is appreciated, thank you! |
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:
Fixes #56950