1

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."

1
  • "exception don't broke the application" points to an async void. Avoid them as much as possible. And wrap their insides in a try/catch. Commented Feb 8, 2022 at 21:46

1 Answer 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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have multiples IJSRuntime executing some UI updates like await jsRuntime.InvokeVoidAsync("ToggleSoundIcon", TimeSpan.FromSeconds(2), value); but neither is using a .Result. Also I tried changing every .Wait()` with `await and the problem persist. Now i'm thinking is a IJSRuntime issue on my implementation. In my code exists events that call IJSRuntime methods.
Every IJSRuntime is called and wrapped inside a trycatch so I think the exception couldn't show in the UI or can't stop the application because isn't a unhandled exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.