-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
base: main
Are you sure you want to change the base?
Conversation
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Prerendering.cs
Outdated
Show resolved
Hide resolved
/ba-g Unrelated build failure |
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); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Prevent rethrowing
NavigationException
in finalizer threadDescription
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:
Root Cause
The issue occurs in the
WaitForNonStreamingPendingTasks
method inEndpointHtmlRenderer
, 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, observeNavigationExceptions
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