Unity3dAsyncAwaitUtil icon indicating copy to clipboard operation
Unity3dAsyncAwaitUtil copied to clipboard

Assert hit in UnityAsyncUtil package

Open JPerryOddGames opened this issue 2 years ago • 4 comments

We're experiencing the Assert failing in the SimpleCoroutineAwaiter implementation of INotifyCompletion.OnCompleted.

We've also seen this error pop up many times before in random places. This particular part of the code is just waiting on a WaitForUpdate instance. Unfortunately I don't have any other details, and I've not been able to reproduce this despite many attempts. As far as I can tell the generated task state machine code should only invoke the OnCompleted method once ever, so I really don't understand how this could happen.

The Stacktrace

System.Exception Assert hit in UnityAsyncUtil package! 0 IEnumeratorAwaitExtensions.Assert (System.Boolean condition) (<00000000000000000000000000000000>:0) 1 IEnumeratorAwaitExtensions+SimpleCoroutineAwaiter.System.Runtime.CompilerServices.INotifyCompletion.OnCompleted (System.Action continuation) (<00000000000000000000000000000000>:0) 2 System.Runtime.CompilerServices.AsyncVoidMethodBuilder.AwaitOnCompleted[TAwaiter,TStateMachine] (TAwaiter& awaiter, TStateMachine& stateMachine) (<00000000000000000000000000000000>:0) 3 ODDFramework.AdProviderBase+<AttemptLoadAsync>d__40.MoveNext () (<00000000000000000000000000000000>:0)

JPerryOddGames avatar Mar 30 '22 04:03 JPerryOddGames

Hi, I have the same exceptions on production. Android and iOS. Different system version numbers and different devices. No common features. I was unable to reproduce this during testing on my devices.

An example of an exception from Android. After receiving a callback from an admob native plugin, that is not invoked on the main thread, I want to wait for the main thread to change the icon state on unity UI.

Exception:

Exception : Assert hit in UnityAsyncUtil package!
       at IEnumeratorAwaitExtensions.Assert(IEnumeratorAwaitExtensions)
       at IEnumeratorAwaitExtensions+SimpleCoroutineAwaiter.System.Runtime.CompilerServices.INotifyCompletion.OnCompleted(IEnumeratorAwaitExtensions+SimpleCoroutineAwaiter.System.Runtime.CompilerServices.INotifyCompletion)
       at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult].AwaitOnCompleted[TAwaiter,TStateMachine](System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[TResult])
       at AdMobAdNetwork+<RewardedAdLoadedTask>d__16.MoveNext(AdMobAdNetwork+<RewardedAdLoadedTask>d__16)
       at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](System.Runtime.CompilerServices.AsyncTaskMethodBuilder)
       at AdMobAdNetwork.RewardedAdLoadedTask(AdMobAdNetwork)
       at AdMobAdNetwork+<HandleRewardedAdLoaded>d__15.MoveNext(AdMobAdNetwork+<HandleRewardedAdLoaded>d__15)
       at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[TStateMachine](System.Runtime.CompilerServices.AsyncVoidMethodBuilder)
       at AdMobAdNetwork.HandleRewardedAdLoaded(AdMobAdNetwork)
       at System.EventHandler`1[TEventArgs].Invoke(System.EventHandler`1[TEventArgs])
       at System.EventHandler`1[TEventArgs].Invoke(System.EventHandler`1[TEventArgs])
       at System.Reflection.MonoMethod.Invoke(System.Reflection.MonoMethod)
       at System.Reflection.MethodBase.Invoke(System.Reflection.MethodBase)
       at UnityEngine.AndroidJavaProxy.Invoke(UnityEngine.AndroidJavaProxy)
       at UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod(UnityEngine._AndroidJNIHelper)
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(System.Runtime.ExceptionServices.ExceptionDispatchInfo)
       at System.Threading.WaitCallback.Invoke(System.Threading.WaitCallback)
       at System.Threading.ContextCallback.Invoke(System.Threading.ContextCallback)
       at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem(System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem)
       at System.Threading.ThreadPoolWorkQueue.Dispatch(System.Threading.ThreadPoolWorkQueue)

Code:

private static async void HandleRewardedAdLoaded(object sender, EventArgs e) //callback invoked from admob plugin
{
	await RewardedAdLoadedTask();
}

private static async Task RewardedAdLoadedTask()
{
	await new WaitForUpdate();

	//some action to do on main thread
	...
}

AndrzejLorek avatar Apr 05 '22 15:04 AndrzejLorek

Just posting to add some additional info: I can fairly confidently confirm that this only happens when awaiting a WaitForUpdate from a task that isn't running on the main Unity thread. I can also see this primarily occurs after receiving an AdMob callback on a non-main thread, just as AndrzejLorek mentioned in the post above

JPerryOddGames avatar Apr 06 '22 08:04 JPerryOddGames

funny because I usually use WaitForUpdate to get back to the main thread. But I've forked this and heavily modified it from this source code. Not sure what the difference could be.

StephenHodgson avatar Apr 06 '22 16:04 StephenHodgson

We're experiencing the Assert failing in the SimpleCoroutineAwaiter implementation of INotifyCompletion.OnCompleted.

We've also seen this error pop up many times before in random places. This particular part of the code is just waiting on a WaitForUpdate instance. Unfortunately I don't have any other details, and I've not been able to reproduce this despite many attempts. As far as I can tell the generated task state machine code should only invoke the OnCompleted method once ever, so I really don't understand how this could happen.

The Stacktrace

hi, did you manage to solve the problem?

dethroneofall avatar Jun 08 '23 07:06 dethroneofall