Search Unity

JobComponentSystem hangs when IJobForEach for an archetype with no matching entities is scheduled.

Discussion in 'Entity Component System' started by Latty, Jul 7, 2019.

  1. Latty

    Latty

    Joined:
    May 5, 2014
    Posts:
    16
    I had an odd situation where adding a job to the dependency chain in my JobComponentSystem was causing that system to hang (it worked up to that job then stopped executing—the rest of the game continued, but that system was hung).

    I eventually traced this down to the job being an IJobForEach with parameters for an archetype that didn't exist (yet). Stopping this task from executing until I knew the entity existed already "fixed" this, but that won't be possible in a real environment (I just hacked it in by checking the game time until a second has passed).

    If this is intended behaviour, it is not well documented (I can't find anything describing this behaviour), and seems extremely odd. I would assume that an IJobForEach with no matches would just do nothing in that case. Am I missing something here, or is this a bug?

    Simple example:
    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. namespace Test {
    4.  
    5. public struct Test : IComponentData {
    6.     public int A;
    7. }
    8.  
    9. }
    10.  
    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using UnityEngine;
    5.  
    6. namespace Test {
    7.  
    8. public class TestSystem : JobComponentSystem {
    9.     // protected override void OnCreate() {
    10.     //     Debug.Log("[TestSystem] OnCreate");
    11.     //     EntityManager.CreateEntity(typeof(Test));
    12.     // }
    13.  
    14.     private struct TestJob : IJobForEach<Test> {
    15.         public void Execute(
    16.             ref Test test
    17.         ) {
    18.             Debug.Log("[TestSystem] TestJob");
    19.         }
    20.     }
    21.  
    22.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    23.         Debug.Log("[TestSystem] OnUpdate");
    24.         var testJob = new TestJob().Schedule(this, inputDeps);
    25.         return testJob;
    26.     }
    27. }
    28.  
    29. }
    30.  
    This hangs (OnUpdate() only runs once), while uncommenting the OnCreate() method means it works as expected.
     
    Last edited: Jul 7, 2019
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Cause IJFE creates EntityQuery implicitly only on first Schedule call, not at system initialization as before and this described in release notes. If you declare EQ in OnCreate and pass as first argument to Schedule system not runs until EQ match. If you want system to run always - use AlwaysUpdateSystem attribute.
     
  3. Latty

    Latty

    Joined:
    May 5, 2014
    Posts:
    16
    Thanks for the solution. I understand the docs are in flux at the moment, but that would seem like something worth mentioning in the JobComponentSystem docs, as they currently don't mention that they won't execute `OnUpdate()` in that case, as far as I can see.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685