Skip to content

Blazor: NavigationManager.NavigateTo external URL sometimes throws ObjectDisposedException on HttpResponseStreamWriter #54997

@jessicah

Description

@jessicah

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When trying to redirect to external OAuth endpoint (could probably be any external URI) from OnAfterRenderAsync, which redirects back to the app, occasionally an AggregateException of ObjectDisposedException on HttpResponseStreamWriter is thrown.

The navigation is done in OnAfterRenderAsync in an attempt to avoid the exception, which did make it better, but it still happens from time to time.

Have tried using a CancellationTokenSource on Blazor components to tell them to stop updating their UI as well, but that doesn't seem to be helping either.

Expected Behavior

Expect the navigation to succeed without throwing an exception.

Steps To Reproduce

I have components that render on a timer, and then a component that performs external redirect in its OnAfterRenderAsync.

Base razor page is set with @attribute [StreamRendering] and @rendermode InteractiveServer.

@inject NavigationManager Manager

@code {
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender) {
            return;
        }

        var expiration = await GetValue<DateTime?>(ExpirationKey);
        var accessToken = await GetValue<string>(AccessTokenKey);

        if (string.IsNullOrEmpty(accessToken) || expiration.HasValue == false || expiration.Value < DateTime.UtcNow)
        {
            Manager.NavigateTo("https://external-service", true);
            return;
        }
        else
        {
            Console.WriteLine("have a valid token: {0}", accessToken);
        }
    }
}

Exceptions (if any)

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (Cannot access a disposed object.
ObjectDisposed_ObjectName_Name)
  Source=Microsoft.AspNetCore.Components
  StackTrace:
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCalls(ArrayRange`1 updatedComponents, Task updateDisplayTask)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<InvokeAsync>g__Execute|8_0(ValueTuple`3 state)
--- End of stack trace from previous location ---
   at BlazorSSR.Components.Pages.MainMetrics.<<OnInitialized>b__12_0>d.MoveNext() in C:\Users\jessi\source\repos\KittyhawkRacingOverlay\BlazorSSR\Components\Pages\MainMetrics.razor:line 92
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.QueueUserWorkItemCallback.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
ObjectDisposedException: Cannot access a disposed object.
ObjectDisposed_ObjectName_Name

- ObjectName: HttpResponseStreamWriter
- Source: Microsoft.AspNetCore.Components.Endpoints
- StackTrace:
    at Microsoft.AspNetCore.Components.Endpoints.Rendering.TextChunkListBuilder.<WriteToAsync>d__5.MoveNext()
   at Microsoft.AspNetCore.Components.Endpoints.Rendering.BufferedTextWriter.<FlushAsyncCore>d__15.MoveNext()
   at Microsoft.AspNetCore.Components.Endpoints.Rendering.BufferedTextWriter.<FlushAsyncCore>d__15.MoveNext()
   at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.<<UpdateDisplayAsync>g__FlushThenComplete|13_0>d.MoveNext()

.NET Version

8.0.200

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Needs: ReproIndicates that the team needs a repro project to continue the investigation on this issueStatus: No Recent Activityarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions