[0.50] DynamicBuffer out of range and overflow reading data from dynamic snapshot memory buffer

Recently I get IndexOutOfRangeException: Index x is out of range in DynamicBuffer of ‘x’ Length and InvalidOperationException: Overflow reading data from dynamic snapshot memory buffer. Is there any bug at dots netcode 0.50 currently trigger the following errors?

IndexOutOfRangeException: Index 53 is out of range in DynamicBuffer of ‘53’ Length.
Unity.Entities.DynamicBuffer1[T].CheckBounds (System.Int32 index) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/Iterators/DynamicBuffer.cs:145)
Unity.Entities.DynamicBuffer1[T].get_Item (System.Int32 index) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/Iterators/DynamicBuffer.cs:186)
Unity.NetCode.GhostUpdateSystem+UpdateJob.Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 chunkIndex, System.Int32 firstEntityIndex) (at Library/PackageCache/com.unity.netcode@0.50.0-preview.29/Runtime/Snapshot/GhostUpdateSystem.cs:342)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].ExecuteInternal (Unity.Entities.JobChunkExtensions+JobChunkWrapper1[T]& jobWrapper, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:401)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].Execute (Unity.Entities.JobChunkExtensions+JobChunkWrapper1[T]& jobWrapper, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:368)

InvalidOperationException: Overflow reading data from dynamic snapshot memory buffer
Unity.NetCode.GhostUpdateSystem+UpdateJob.SetupDynamicDataAtTick (Unity.NetCode.SnapshotData+DataAtTick& dataAtTick, System.Int32 snapshotOffset, System.Int32 snapshotSize, System.Int32 maskBits, Unity.Entities.DynamicBuffer1[Unity.NetCode.SnapshotDynamicDataBuffer]& ghostSnapshotDynamicBuffer, System.Int32& buffernLen) (at Library/PackageCache/com.unity.netcode@0.50.0-preview.29/Runtime/Snapshot/GhostUpdateSystem.cs:429)
Unity.NetCode.GhostUpdateSystem+UpdateJob.Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 chunkIndex, System.Int32 firstEntityIndex) (at Library/PackageCache/com.unity.netcode@0.50.0-preview.29/Runtime/Snapshot/GhostUpdateSystem.cs:385)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].ExecuteInternal (Unity.Entities.JobChunkExtensions+JobChunkWrapper1[T]& jobWrapper, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:401)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].Execute (Unity.Entities.JobChunkExtensions+JobChunkWrapper`1[T]& jobWrapper, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:368)

IndexOutOfRangeException: Index 50 is out of range in DynamicBuffer of ‘50’ Length.
Unity.Entities.DynamicBuffer1[T].CheckBounds (System.Int32 index) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/Iterators/DynamicBuffer.cs:145)
Unity.Entities.DynamicBuffer1[T].get_Item (System.Int32 index) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/Iterators/DynamicBuffer.cs:186)
Unity.NetCode.GhostUpdateSystem+UpdateJob.Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 chunkIndex, System.Int32 firstEntityIndex) (at Library/PackageCache/com.unity.netcode@0.50.0-preview.29/Runtime/Snapshot/GhostUpdateSystem.cs:342)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].ExecuteInternal (Unity.Entities.JobChunkExtensions+JobChunkWrapper1[T]& jobWrapper, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:401)
Unity.Entities.JobChunkExtensions+JobChunkProducer1[T].Execute (Unity.Entities.JobChunkExtensions+JobChunkWrapper1[T]& jobWrapper, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.entities@8e13422a93/Unity.Entities/IJobChunk.cs:368)

I think I know that one. It happens when you have ghosts that have the same archetype for their root entity but different child hierarchy layouts. If that is indeed the issue, you can add a shared component that uses the GhostTypeComponent to separate ghosts on the client.

I fixed it on my side by adding the following component to the NetCode package and attaching it to all ghosts through the GhostAuthoringConversion system.

   [GeneratePropertyBag]
   [DontSupportPrefabOverrides]
   public struct ClientGhostSeparationType : ISharedComponentData
   {
       public GhostTypeComponent SharedValue;
   }
    DstEntityManager.AddSharedComponentData(rootEntity, new ClientGhostSeparationType()             {SharedValue = ghostType});

@Lukas_Kastern Thanks for the reply. I will try your solution. Btw is there any special reason to put [GeneratePropertyBag] and [DontSupportPrefabOverrides] attribute? The errors from this thread looks like caused by the similar issue ( [0.50] NullReferenceException: Object reference not set to an instance of an object ) ?

One a ghost is spawned, their child entities should not be removed / destroyed in general. The LinkedEntityGroup must be kept consistent (adding new entities may still be valid, but removing entities it is not), because all the serialization code store rely on the archetype being consistent.
We have in many places checks that still allow missing component and or entities but it may we are missing some safety check in the GhostUpdateSystem.
Thanks for reporting the issue. If someone find an way to repro the issue consistently, would be amazing.

@CMarastoni I guess I know why it become like that. It’s current limitation of dots netcode. When ur parent entities all are at the same archetype but different child entities with different archetypes, dots netcode will fail to sync the data across network. For example, it will wrongly sync parent entity A data to parent entity B then the error will start spamming. Not sure how u plan to fix it but from what I know there’s 2 ways. First solution is add validation at authoring stage to ghost to check whether the ghost need to split to another new archetype then maybe add SharedGhostTypeComponent. Second solution is further improve the dots netcode to make this sync data possible.

@timjohansson @CMarastoni I think you really need to make sure this issue really getting proper fix as soon as possible. If I remember correctly more than 1 year ago this issue is ady there but I just making every entity to different chunk to hack fix it. But this solution is not really nice. I really want same parent entity can stay at the same chunk to have quick iteration to get best possible performance whenever possible. I think it need dots netcode team to figure out better solution to fix this issue.

Hi @CMarastoni . Have u look at this issue?