Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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

Discussion in 'Entity Component System' started by davenirline, Jul 18, 2020.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    951
    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.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at <a979f18d51af41179d12b797e8c5be14>:0)
    Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at <a979f18d51af41179d12b797e8c5be14>:0)
    Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at <a979f18d51af41179d12b797e8c5be14>:0)
    CommonEcs.AStarSearchParallelFor`2[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 <a979f18d51af41179d12b797e8c5be14>:0)

    During debugging, I found that the NativeArray<Entity> 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#.
     

    Attached Files:

    Egad_McDad likes this.
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    951
    Issue tracker link.
     
  3. Fribur

    Fribur

    Joined:
    Jan 5, 2019
    Posts:
    127
    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.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,674
    Completely random note, but from your code

    // Unity says to Dispose() Temp collections:
    // https://forum.unity.com/threads/allocator-temp-container-need-dispose.852082/

    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.
     
    davenirline likes this.
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,674
    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: https://forum.unity.com/threads/native-plugins-broken-in-jobs.932961/#post-6105531

    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.
     
    davenirline likes this.
  6. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    951
    Thank you very much!