'Blazor Server - System.InvalidOperationException is throwing randomly

I'm developing a application on Blazor Server (.NET 6). This exception occur a lot of times. But this exception occur in each page don't matter if the page render many times or not. For example, in a Login page (don't need to render more than once) this exception occur. I checked every event subscribe and all are unsubscribe when the dispose occur (so I think the event subscription is not a problem). I want to put my code as example but has more than 50k lines and you wouldn't want to read all the code.

I notice the application is a bit slow and would be by some Tasks or Threads running. If it is the problem what shall I do?

I read this exception occur when a Task ends more than once and I think is not the case.

If the StateHasChanged method is throwing this exception I have many InvokeAsync(StateHasChanged).Wait() and await InvokeAsync(StateHasChanged) on the code. No too of these but some ones.

What I tried? I tried remove all the asynchronous methods that I could (removing the await and using a .Result). Also when dispose all Callback += Method is unsubscribed Callback -= Method. The application performance is very good less than 3% of all cores available is used.

Error: System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
       at System.Threading.Tasks.TaskCompletionSource`1.SetException(Exception exception)
       at Microsoft.JSInterop.Infrastructure.TaskGenericsUtil.TcsResultSetter`1.SetException(Object tcs, Exception exception)
       at Microsoft.JSInterop.Infrastructure.TaskGenericsUtil.SetTaskCompletionSourceException(Object taskCompletionSource, Exception exception)
       at Microsoft.JSInterop.JSRuntime.EndInvokeJS(Int64 taskId, Boolean succeeded, Utf8JsonReader& jsonReader)
       at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.EndInvokeJS(JSRuntime jsRuntime, String arguments)
       at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<InvokeAsync>b__8_0(Object state)
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost.EndInvokeJSFromDotNet(Int64 asyncCall, Boolean succeeded, String arguments)

This exception don't broke the application. Only appear in the console and show the typical "An unhandled exception has occurred. See browser dev tools for details."



Solution 1:[1]

It looks like you are trying to be too complicated in your async code. My guess is either:

  1. You're using a TaskCompletionSource and setting it to complete when it is already set to complete.
  2. Doing some JsInterop calls on completed tasks.

By complicated I mean:

InvokeAsync(StateHasChanged).Wait()

Why .Wait()? rather than:

await InvokeAsync(StateHasChanged);

It's hard to tell as there's no code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrC aka Shaun Curtis