System.InvalidOperationException errors possibly caused by Sentry.
Environment
Unity: 6000.0.45f1 Sentry: Unity package 3.2.3
Steps to Reproduce
NA
Expected Result
No errors to be raised.
Actual Result
The app runs into System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed. errors
Any logs or screenshots
The following errors are reported to Sentry with little context. Each show an action taken by Sentry when creating and sending events.
System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetException() in Generics__24.cpp:line 7307
at Sentry.Protocol.Envelopes.EnvelopeItem.SerializeAsync() in Sentry__3.cpp:line 20735
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 30488
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 28945
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() in mscorlib__19.cpp:line 23443
System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetResult() in Generics__24.cpp:line 7169
at System.Text.Json.Utf8JsonWriter.DisposeAsync() in Sentry.System.Text.Json__3.cpp:line 19595
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 30488
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 28945
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() in mscorlib__19.cpp:line 23443
System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetException() in Generics__24.cpp:line 7307
at System.Text.Json.Utf8JsonWriter.FlushAsync() in Sentry.System.Text.Json__3.cpp:line 19926
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 30488
at System.Threading.ExecutionContext.RunInternal() in mscorlib__9.cpp:line 28945
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run() in mscorlib__19.cpp:line 23443
Original report: Discord
Looks like a runtime or Unity error. We need more details. Are there some logs?
It's probably something to run through Unity's support
Oh wait I see this happens when we set the exception to the task. Gotta check if do we that by hand if so, and we do it on a completed task, that'd explain
Tiny update: We've been trying to reproduce this but no luck so far:
- Is this an issue from the underlying .NET code of the async-statemachine, it the IL2CPP implementation?
- Is this an issue where we fail to serialize something in general?
cc @Flash0ver
More context:
- we don't think it's our usage of
TCSorTCS<TResult>- we generally use
Try*-methods to avoid completing a Task that has already completed - no indication in the stack trace
- we generally use
- we don't think it's a double-consumption of a
ValueTaskorValueTask<TResult>- we just
awaitValueTask-returning methods - no
AsyncValueTaskMethodBuilderin stack trace - UPDATE: see below
- we just
- we think there may be an issue with either an implementation of "Async-Method-Builder" from Unity's fork of Mono, or that fork's "Async-Rewriter" (the compiler-generated Async State Machine), or an issue in the IL2CPP-ification of .NET
async/awaitcode- considering the AsyncTaskMethodBuilder`1 in the stack trace
- see also How Async/Await Really Works in C#
UPDATE
We actually did find a bad usage pattern in our sentry-dotnet code base:
Although we await a ValueTask or ValueTask<TResult> neither multiple times nor concurrently, we do invoke a blocking call (when not yet completed):
https://github.com/getsentry/sentry-dotnet/blob/aba82c2858ace9eb552e0d44f9b254e85f96358b/src/Sentry/Internal/Http/CachingTransport.cs#L570
But considering our current code flow as well as the implementation of the ValueTask-returning method in question, this seems to be a just a theoretical problem. And also unrelated to this issue.
See also Understanding the Whys, Whats, and Whens of ValueTask.
The only other piece of information that I can think of that could potentially be related is that we use UniTask for async handling in our game, so it's possible there's some kind of conflict going on here? Obviously we aren't interacting with anything async related to sentry, but perhaps it is overwriting something core that causes this conflict
I wish there was more info I could provide, but I'm also at a loss for what is causing this and haven't reproed either. I'm just seeing it happening for some people
I see ... thanks for the info ... hmmm ... UniTask could play a part in this issue ... but not necessarily ... I'm uncertain 🤔
I'm afraid this is going to be quite difficult to debug. And even more so without a minimal reproduction ... which will also be quite difficult to isolate.
I am wondering how to reproduce this issue, and also whether updating Unity and/or Sentry and/or UniTask could help.
We are also seeing frequent InvalidOperationException error when using Sentry Unity SDK 3.0.0. We also use UniTask in the project, if that is any clue.
Upgrading to 3.2.2 and higher results in frequent crashes, which is even worse: https://github.com/getsentry/sentry-unity/issues/2423
Thanks for the update. Unfortunately, we still have not been able to reproduce the issue locally.