Search Unity

Bug Server world allocations live too long in 0.50 preview 29

Discussion in 'NetCode for ECS' started by Occuros, Mar 17, 2022.

  1. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    By creating a fresh project and just importing the latest netcode package (0.50.0-preview.29) the leak detection system complains about deleting allocations that are too old.


    Code (CSharp):
    1. Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 6)
    2. UnityEngine.StackTraceUtility:ExtractStackTrace ()
    3. Unity.Collections.Memory/Unmanaged/Array:Resize
    this happens every frame. Apart from deactivating leak detection, is there currently another workaround?
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    This can happen for multiple reason. question: what is the current frame rate? (higher than 60FPS ?)
     
  3. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    Yes, the current framerate is 72FPS (potentially 90 FPS).

    But I could resolve it by enabling VSync in the GameView window. Are currently higher framerates than 60FPS not supported by netcode?
     
  4. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    They are supported and everything works fine. The problem is that when you use temp allocation, if the frame rate is higher then the server tick rate, the world is not updated every frame. As such, when the allocation get delete is considered too old.
    This problem has been always here, is not something new in 0.5
     
  5. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    But there is also a possibility that we are "leaking" something, but we I don't remember of any new leak issue recently in 0.5.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I'm seeing a single random leak even with vsync enabled right at the start of the game.

    My current setup is there is always a client world even in the menu and when you start game a server world is created.

    I think it's the point where I create the server world that it leaks.

    I'll do more debugging at some point when it bothers me enough, but I never saw this pre-update.
     
  7. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    I can confirm. Fresh 2020.3.30 project with the following packages: Entities, Physics, HybridRenderer has no warnings spam, but warnings appear as soon as Netcode package is added

    I don't think I was seeing this in previous versions
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Based off the change log this should actually be fixed

    https://docs.unity3d.com/Packages/com.unity.entities@0.50/changelog/CHANGELOG.html
    Except looking at the source code this change log is a lie

    Code (CSharp):
    1.         public EntityCommandBuffer CreateCommandBuffer()
    2.         {
    3.             var cmds = new EntityCommandBuffer(Allocator.TempJob, -1, PlaybackPolicy.SinglePlayback);
    4.             var world = World.Unmanaged;
    5.             var state = world.ResolveSystemState(world.ExecutingSystem);
    6.             cmds.SystemID = state != null ? state->m_SystemID : 0;
    7.             cmds.OriginSystemHandle = state != null ? state->m_Handle : default;
    8.  
    9.             m_PendingBuffers.Add(cmds);
    10.  
    11.             return cmds;
    12.         }
    It's still using TempJob not World.UpdateAllocator.
     
  9. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    900
    That should have been already addressed. I will double check. Thanks for reporting
     
  10. skiplist

    skiplist

    Joined:
    Nov 9, 2014
    Posts:
    47
    Does this mean that there now is a TempJob-like allocator that we can use which doesn't generate those warnings?
     
  11. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    The new World.UpdateAllocator is tied to World Update rate not frame rate and resets every 2 frames (because there are 2 of them that alternate.) You don't need to worry about disposing it either.

    You can use it like

    NativeArray<int> array = CollectionHelper.CreateNativeArray<int, RewindableAllocator>(1234, ref this.World.UpdateAllocator);
     
    bit-master and skiplist like this.
  12. crowder

    crowder

    Joined:
    Jul 17, 2015
    Posts:
    1
    What is the recommendation/fix here? Is there even a way to wallpaper over this until a pkg fix is released?
     
  13. gabriel_proxima

    gabriel_proxima

    Joined:
    Mar 14, 2022
    Posts:
    1
    For anyone looking to fix this in the meantime. You will need to:

    - Make com.unity.entities a local package. You can do this by going to the Library/PackageCache/ and copying com.unity.entities@0.50.1-preview.2 to a local path, and then adding a local package in the Package Manager

    - In EntityCommandBufferSystem, line 83, change Allocator.TempJob to World.UpdateAllocator.ToAllocator (see below)


    Code (CSharp):
    1.         public EntityCommandBuffer CreateCommandBuffer()
    2.         {
    3.             // var cmds = new EntityCommandBuffer(Allocator.TempJob, -1, PlaybackPolicy.SinglePlayback);
    4.             var cmds = new EntityCommandBuffer(World.UpdateAllocator.ToAllocator, -1, PlaybackPolicy.SinglePlayback);
    5.  
    6.             var world = World.Unmanaged;
    7.             var state = world.ResolveSystemState(world.ExecutingSystem);
    8.             cmds.SystemID = state != null ? state->m_SystemID : 0;
    9.             cmds.OriginSystemHandle = state != null ? state->m_Handle : default;
    10.  
    11.             m_PendingBuffers.Add(cmds);
    12.  
    13.             return cmds;
    14.         }
    15.  
     

    Attached Files:

    adammpolak likes this.
  14. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    883
    Is there any docs where I can read about this new allocator?