Disabling system running CommandBuffer job causes JobTempAlloc leak spam

I’m trying to implement pausing in my game, I do so by doing

foreach (var system in World.Active.Systems)
system.Enabled = false;

It works great but I started getting “Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak” spam in my console when pausing. After a lot of debugging I figured out it’s because I was disabling a system in the same frame which was scheduling a job where an EntityCommandBuffer.Concurrent was trying to remove a component. Here’s a pared down example:

public class RemoveComponentSystemTest : JobComponentSystem
{
    BeginInitializationEntityCommandBufferSystem initBufferSystem_;
 
    [RequireComponentTag(typeof(Test))]
    struct RemoveComponentSystemJob : IJobForEachWithEntity<Translation>
    {
        public EntityCommandBuffer.Concurrent commandBuffer;
     
        public void Execute(Entity entity, int index, ref Translation c0)
        {
            commandBuffer.RemoveComponent<Test>(index, entity);
        }
    }

    protected override void OnCreate()
    {
        initBufferSystem_ = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    }

    protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        var job = inputDependencies;

        if(Input.GetButtonDown("Jump") )
        {
            job = new RemoveComponentSystemJob
            {
                commandBuffer = initBufferSystem_.CreateCommandBuffer().ToConcurrent(),
            }.Schedule(this, job);

            foreach (var system in World.Systems)
                system.Enabled = false;
        }
     
        return job;
    }
}

Is this a bug or just bad design on my part? (Or possibly both)

Overriding OnStopRunning in your system to call JobHandle.Complete on your job might do the trick.

Thanks for the suggestion - unfortunately it didn’t fix it.

On further testing it seems just scheduling a job with an EntityCommandBuffer.Concurrent and then disabling the system before ECS has had a chance to resolve that frame will cause the editor spam. I’ve submitted a bug report - though I’m not sure it’s actually a bug or if I’m just being dumb.

For now I was able to work around it by just deferring the “pausing” bit (toggling the “.Enabled” for all systems) to a coroutine, and that seems to avoid the spam.