dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.34k stars 9.97k forks source link

System.InvalidOperationException: Error: No element is currently associated with component 0 #51393

Closed XorZy closed 10 months ago

XorZy commented 12 months ago

Is there an existing issue for this?

Describe the bug

I have been getting a lot of circuit crashes with "System.InvalidOperationException: Error: No element is currently associated with component X' when navigating away from a specific server interactive page. I've had a hard time finding the cause of the issue but I've narrowed it down to a component that takes too long to render synchronously.

EDIT: I was not able to reproduce the issue on 7.0.11

Expected Behavior

The circuit should not crash and the app should continue to run as normal. It's easy to avoid the exception by making all work asynchronous, but it may be difficult to find the source of the issue given the vagueness of the exception. I should note that I have been able to reproduce the issue with timeouts as low as 12ms. I think I see why this is happening but imo if the component has already been disposed then the framework should not try to render it and no exception should be raised.

Steps To Reproduce

Here is a minimal example that will raise the exception every single time on my machine: (Obviously this is a simplified example that serves no purpose but in my case the component was calling a synchronous method that, under specific circumstances, could take a little while to return).

@page "/counter"
@attribute [RenderModeInteractiveServer]
@inject NavigationManager NavigationManager;

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

@{
    if (currentCount > 0)
    {
        Thread.Sleep(500);
    }

}

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
        NavigationManager.NavigateTo("/");
    }
}

When you click on the button, you will be redirected to the home page and the circuit will crash.

Exceptions (if any)

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit 'w85YXLa05o0keP-wQRM_Bve_qfw2WV04E30hNBMooXs'. System.AggregateException: One or more errors occurred. (Error: No element is currently associated with component 0) ---> System.InvalidOperationException: Error: No element is currently associated with component 0 at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCallsAfterUpdateDisplayTask(Task updateDisplayTask, Int32[] updatedComponents) --- End of inner exception stack trace ---

.NET Version

8.0.100-rc.2.23502.2

Anything else?

.NET SDK: Version: 8.0.100-rc.2.23502.2 Commit: 0abacfc2b6

Runtime Environment: OS Name: endeavouros OS Version:
OS Platform: Linux RID: linux-x64 Base Path: /usr/share/dotnet/sdk/8.0.100-rc.2.23502.2/

.NET workloads installed: There are no installed workloads to display.

Host: Version: 8.0.0-rc.2.23479.6 Architecture: x64 Commit: 0b25e38ad3

.NET SDKs installed: 8.0.100-rc.1.23463.5 [/usr/share/dotnet/sdk] 8.0.100-rc.2.23502.2 [/usr/share/dotnet/sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 8.0.0-rc.1.23421.29 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0-rc.2.23480.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.0-rc.1.23419.4 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0-rc.2.23479.6 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

msschl commented 11 months ago

I get the same error. But I narrowed it down to webpack. If I use npm / webpack with the Microsoft.AspNetCore.ClientAssets build tool and load the build module, even if it is empty, it will interfere with the blazor.js file and produces this error.

javiercn commented 10 months ago

@XorZy can you try with RTM? I am unable to reproduce this based on your minimal repro. I tried increasing the time up to 10s, but it doesn't seem to be an issue. I think we address a similar issue recently. I suspect it has to do with the component being disposed.

XorZy commented 10 months ago

@XorZy can you try with RTM? I am unable to reproduce this based on your minimal repro. I tried increasing the time up to 10s, but it doesn't seem to be an issue. I think we address a similar issue recently. I suspect it has to do with the component being disposed.

I have tried again with RC2, which still throws the exception 100% of the time, but I could not reproduce it with RTM, even with long timeouts. I think it's safe to say the issue has been resolved.

javiercn commented 10 months ago

@XorZy thanks for the additional details.

We are going to consider this addressed then until we receive new information about it.