Why am I getting InvalidOperationException when trying to use unity physics

I am getting this exception : InvalidOperationException: The previously scheduled job NativeStream:ConstructJob writes to the Unity.Collections.NativeStream ConstructJob.Container. You must call JobHandle.Complete() on the job NativeStream:ConstructJob, before you can read from the Unity.Collections.NativeStream safely.
This is the code I have :

[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    [UpdateAfter(typeof(PhysicsSystemGroup))]
    public partial struct NpcKillSystem : ISystem
    {
        public void OnUpdate(ref SystemState state)
        {
            Entity playerEntity = SystemAPI.GetSingletonEntity<PlayerTag>();
            List<Entity> entitiesToDestroy = new List<Entity>();
            TriggerEvents triggerEvents = SystemAPI.GetSingleton<SimulationSingleton>().AsSimulation().TriggerEvents;
            foreach (var trigger in triggerEvents)
            {
                Entity npcEntity = trigger.EntityA;
                if (trigger.EntityA == playerEntity)
                {
                    npcEntity = trigger.EntityB;
                }
                entitiesToDestroy.Add(npcEntity);
            }
            foreach (var entity in entitiesToDestroy)
            {
                state.EntityManager.DestroyEntity(entity);
            }
        }
    }

The exception happens on the foreach line.

Apparently I just needed to change the UpdateInGroup to [UpdateInGroup(typeof(SimulationSystemGroup),OrderLast =true)]

Update: No it does not work all the time, sometimes the error does not appear and sometimes it appears. Regardless of the error though , with this change the triggers do seem to get parsed and the system works.
I would like to get an answer though to why this error happens in the first place. I think it’s got something to do with when the triggers get read but I tried different UpdateInGroup values and I have not found one that gives no errors consistently.

PS: I also tried command buffers for the destruction of the entity, no change.

Entity playerEntity = SystemAPI.GetSingletonEntity<PlayerTag>();
            TriggerEvents triggerEvents = SystemAPI.GetSingleton<SimulationSingleton>().AsSimulation().TriggerEvents;
            var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
            EntityCommandBuffer cb = ecbSingleton.CreateCommandBuffer(World.Unmanaged);
            foreach (var trigger in triggerEvents)
            {
                Entity npcEntity = trigger.EntityA;
                if (trigger.EntityA == playerEntity)
                {
                    npcEntity = trigger.EntityB;
                }
                cb.DestroyEntity(npcEntity);
            }

Update 2 :
I think I figured it out (again) it seems that the physics systems work on the data returned in triggerEvents in other threads while my code is also running. This cannot be mitigated by simply reordering the system’s execution. Instead what works is either:

  1. using the PhysicsStep component, remove multi-threading
  2. use jobs instead of SystemAPI.GetSingleton<SimulationSingleton>().AsSimulation().TriggerEvents as mentioned in the documentation here: Simulation results | Unity Physics | 1.0.16 The documentation should be updated though to explain this better