Skip to content

Fix for Unobserved NavigationException in Blazor SSR #62554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ilonatommy
Copy link
Member

Prevent rethrowing NavigationException in finalizer thread

Description

When a NavigationException is thrown from an async component lifecycle method during Blazor server-side rendering, it can become an unobserved task exception if not properly handled. This happens specifically in circular redirection scenarios, where the exception is thrown and then the task is garbage collected before being properly observed.

The issue manifests as an unhandled exception in the finalizer thread with a stack trace like:

Unhandled caught: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.
 ---> Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
   at Microsoft.AspNetCore.Components.Endpoints.HttpNavigationManager.NavigateToCore(String uri, NavigationOptions options)
   ...
   at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.<WaitForNonStreamingPendingTasks>g__Execute|43_0()
   at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.WaitForResultReady(Boolean waitForQuiescence, PrerenderedComponentHtmlContent result)

Root Cause

The issue occurs in the WaitForNonStreamingPendingTasks method in EndpointHtmlRenderer, where we await a collection of tasks without properly handling NavigationExceptions that might be thrown from them. When the task is garbage collected before completing, the unobserved exception is rethrown from the finalizer thread.

Fix

We've added proper exception handling in WaitForNonStreamingPendingTasks to specifically catch, observe NavigationExceptions and handle navigation when possible.

Validation

We added NavigationException_InAsyncContext_DoesNotBecomeUnobservedTaskException test that verifies no unobserved exceptions occur when a component causes circular redirections, even with forced garbage collection. We set the AppContext switch to make sure navigation uses the exception-driven flow.

Fixes #62167

@ilonatommy ilonatommy added this to the 10.0-preview7 milestone Jul 3, 2025
@ilonatommy ilonatommy requested review from javiercn and oroztocil July 3, 2025 11:27
@ilonatommy ilonatommy self-assigned this Jul 3, 2025
@ilonatommy ilonatommy requested a review from a team as a code owner July 3, 2025 11:27
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Jul 3, 2025
@ilonatommy
Copy link
Member Author

/ba-g Unrelated build failure

Comment on lines +232 to +241
try
{
// new work might be added before we check again as a result of waiting for all
// the child components to finish executing SetParametersAsync
await pendingWork;
}
catch (NavigationException navigationException)
{
await HandleNavigationException(_httpContext, navigationException);
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this specific to the EndpointHtmlRenderer? Or could it happen in one of the interactive modes

Copy link
Member Author

Choose a reason for hiding this comment

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

Interactive is not throwing NavigationException to redirect. I don't know how it could happen there.

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 SSR Task throws NavigationException from finalizer thread
3 participants