Cysharp/UniTask

Android IL2CPP Null Reference Exception

abdelfattahradwan opened this issue · 1 comments

Calling UniTask.RunOnThreadPool on Android when using IL2CPP causes a null reference exception to be thrown.

Stack trace:

Autoconnected Player "Autoconnected Player" NullReferenceException: Object reference not set to an instance of an object.

	at Cysharp.Threading.Tasks.PlayerLoopHelper.AddContinuation (Cysharp.Threading.Tasks.PlayerLoopTiming timing, System.Action continuation) [0x00000] in <00000000000000000000000000000000>:0 
	at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`1[TStateMachine].GetResult (System.Int16 token) [0x0001b] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\CompilerServices\StateMachineRunner.cs:224 
	at Cysharp.Threading.Tasks.UniTask+Awaiter.GetResult () [0x0002b] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\UniTask.cs:312 
	at MultiThreadedChunk.GenerateMeshAsync (System.Boolean generateVertices, System.Boolean generateTriangles, System.Boolean generateUVs) [0x000e6] in Project\Assets\Scripts\MultiThreadedChunk.cs:308 
	at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.InvokeMoveNext (System.Object stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
	at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
	at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
	at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
	at Cysharp.Threading.Tasks.AwaiterActions.Continuation (System.Object state) [0x00007] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\UniTask.cs:21 
	at Cysharp.Threading.Tasks.UniTaskCompletionSourceCore`1[TResult].TrySetException (System.Exception error) [0x00076] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\UniTaskCompletionSource.cs:167 
	at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`1[TStateMachine].SetException (System.Exception exception) [0x00008] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\CompilerServices\StateMachineRunner.cs:210 
	at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTaskMethodBuilder.SetException (System.Exception exception) [0x00021] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\CompilerServices\AsyncUniTaskMethodBuilder.cs:59 
	at Cysharp.Threading.Tasks.UniTask.RunOnThreadPool (System.Action action, System.Boolean configureAwait, System.Threading.CancellationToken cancellationToken) [0x0019e] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\UniTask.Run.cs:87 
	at Cysharp.Threading.Tasks.CompilerServices.AsyncUniTask`1[TStateMachine].Run () [0x0000d] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\CompilerServices\StateMachineRunner.cs:189 
	at Cysharp.Threading.Tasks.SwitchToThreadPoolAwaitable+Awaiter.Callback (System.Object state) [0x00009] in .\Library\PackageCache\com.cysharp.unitask@305695ad5d\Runtime\UniTask.Threading.cs:218 
	at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] in <00000000000000000000000000000000>:0 
	at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <00000000000000000000000000000000>:0 
	at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <00000000000000000000000000000000>:0 

--- End of stack trace from previous location where exception was thrown ---

	at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 
	at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () [0x0000e] in \home\bokken\build\output\unity\unity\Runtime\Export\Scripting\UnitySynchronizationContext.cs:153 
	at UnityEngine.UnitySynchronizationContext.Exec () [0x0005f] in \home\bokken\build\output\unity\unity\Runtime\Export\Scripting\UnitySynchronizationContext.cs:83 
	at UnityEngine.UnitySynchronizationContext.ExecuteTasks () [0x00015] in \home\bokken\build\output\unity\unity\Runtime\Export\Scripting\UnitySynchronizationContext.cs:107 
Player Settings Player Settings
Build Settings Build Settings

RunOnThreadPool itself works on Android, too.

From the stack trace, perhaps it is using UniTask before PlayerLoopHelper is initialized?
We can only start using UniTask at the BeforeSceneLoad timing.
See also #483 on the topic of initialization timing.