Passed array of entities has content during job scheduling but gone in execution. 2019.4.3 and above

I already reported this behavior as a bug but might as well open a discussion here. Our jobified pathfinding code is suddenly not working when we upgraded to 2019.4.3 (and 2019.4.4) from 2019.4.1. Attached is the minimal project.

To replicate:

  • Open the project in Unity 2019.4.1.

  • Set Burst compilation as disabled (Jobs > Burst > Enable Compilation)

  • Run the scene Assets/AStarError/Scenes/AStarErrorTest.unity.

  • This should run fine without errors.

  • Open the project in Unity 2019.4.3.

  • Run the same scene.

  • There is now an error:

IndexOutOfRangeException: Index 0 is out of range of ‘0’ Length.
Unity.Collections.NativeArray1[T].FailOutOfRangeError (System.Int32 index) (at <a979f18d51af41179d12b797e8c5be14>:0) Unity.Collections.NativeArray1[T].CheckElementReadAccess (System.Int32 index) (at :0)
Unity.Collections.NativeArray1[T].get_Item (System.Int32 index) (at <a979f18d51af41179d12b797e8c5be14>:0) CommonEcs.AStarSearchParallelFor2[HeuristicCalculator,ReachabilityType].Execute (System.Int32 index) (at Assets/CommonEcs.AStar/Scripts/AStarSearchParallelFor.cs:41)
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at :0)

During debugging, I found that the NativeArray passed to AStarSearchParallelFor has a content but somehow those contents are lost when the job is executing. I don’t know what to look for as scheduling a job leads to native code outside of C#.

6103911–664146–AStarErrorReport.zip (365 KB)

1 Like

Issue tracker link.

Have admittedly not looked at attached ZIP, but I suspect the cause is likely the usual one: a structural change due to an entitymanager command (see doc, e.g. AddComponent) between creating the entity array, and execution. Either within the same system, or by the next system when that next System is not waiting (Check dependencies). Such structural changes invalidate all Entity IDs.

Completely random note, but from your code

// Unity says to Dispose() Temp collections:
// https://discussions.unity.com/t/781949

If you check the updated the thread Unity corrected their original statement

having just read the source, that doesnt look like it’s happening.

i haven’t run the sample scene to actually repo the bug though.

1 Like

OK i repoed this from your project, and I know what the issue is and I also have a workaround for you.

So the issue is the same as the issue discussed over here: Native plugins broken in jobs

Basically NativeDisableContainerSafetyRestriction is broken. It seems to invalidate any field before it filling it with garbage.

For your case this is an easy fix, just replace them all with NativeDisableParallelForRestriction and your code works fine.

1 Like

Thank you very much!