Crash in Unity.NetCode.GhostSendSystem+GhostDespawnParallelJob

Recently I have updated netcode to 1.4.0, and start use distance based importance. After that I get crash

Obtained 14 stack frames.
#0  0x007ff85936d8ab in PlayerMain(int, char**)
#1  0x007ff8593613a7 in (Unknown)
#2  0x007ff858430420 in funlockfile
#3  0x007ff7c7bacd4f in Unity.NetCode.GhostSendSystem+GhostDespawnParallelJob, Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.Execute(Unity.NetCode.GhostSendSystem+GhostDespawnParallelJob*, Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null this, Unity.Entities.ArchetypeChunk&, Unity.Entities, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null chunk, System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 chunkIndexInQuery, System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 useEnabledMask, Unity.Burst.Intrinsics.v128&, Unity.Burst, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null chunkEnabledMask) -> System.Void, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089_5097331dcc4b65c96a908e5e07a8434a from Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
#4  0x007ff7c7baccf0 in Unity.Entities.JobChunkExtensions+JobChunkProducer`1[[Unity.NetCode.GhostSendSystem+GhostDespawnParallelJob, Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], Unity.Entities, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null.ExecuteInternal(Unity.Entities.JobChunkExtensions+JobChunkWrapper`1[[Unity.NetCode.GhostSendSystem+GhostDespawnParallelJob, Unity.NetCode, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]&, Unity.Entities, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null jobWrapper, System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null ranges, System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 jobIndex) -> System.Void, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089_b105acc6cbaa86f06bee0e0804e569d8 from Unity.Entities, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
#5  0x007ff7c7f5b5ea in a86a1ae1668d7c060f2d355b13a3dcf4
#6  0x007ff8590c44a9 in (Unknown)
#7  0x007ff8590c488f in (Unknown)
#8  0x007ff8590c113f in (Unknown)
#9  0x007ff8590c16ee in (Unknown)
#10 0x007ff8590c2455 in (Unknown)
#11 0x007ff8592089e5 in (Unknown)
#12 0x007ff858424609 in start_thread
#13 0x007ff858349133 in clone

Do you have any idea what happend inside GhostDespawnParallelJob

Hello, AlexandrKvitka. What is the context of the crash? Do you build the project, launch the executable, and experience the crash straight away or after some specific steps?

The crash happened on the server build. Release build. It happened when ghost entities despawned. I don’t have stable repro case.

We had another similar bug reported. I’m trying to understand the reason for it, because it is not immediately obvious. But at a first glance seems like we are accessing some wrong memory address.

If you have a project to share or open and open a ticket for this would help us to track and try to repro it.

I will try to reproduce it in an empty project. But currently, I can’t reproduce.

For more context:

  • we updated the net code library for 1.4.0
  • we added “distance base importance”
  • we set up a Max send rate for predicted ghosts.
  • we don’t have a stable repro case for our project.

I could collect more helpful information, but I don’t know what it can be.

I think you given me some material to check at least.
One more question: was this in a build ? What kind? IL2PP or Mono ?
Also I suppose burst enabled right ?

Linux server release build with il2cpp and burst

When I use “Distance base importance”, this crash happens

I try this default function:
In first configuration I use: GhostDistanceImportance.BatchScaleFunctionPointer,
In second: GhostDistanceImportance.ScaleFunctionPointer,

Both functions cause a crash.
When I turn off distance base importance, the crash does not reproduce.

I’m also seeing a similar crash caused by GhostSendSystem+GhostDespawnParallelJob when I use DistanceBasedImportance, but have slightly more specific repro info. I have a simple scene where I spawn 1000 NPCs, and have the ability to fire projectiles. I only get the crash when both of the following are true:

  • I have DistanceBasedImportance configured
  • I fire projectiles (the projectiles despawning seems to be a requirement for the crash)

Using Unity 6.2.10f1, NfE 1.9.3

Do you mind creating a bug for this and link your project in it so we can repro? Using the Help/Report a Bug menu.

I’ve reproed the root cause, and am working with @LujainAl-Eryani & Entities folks on a comprehensive fix. There are two root cause repros, plus the fact that N4E code can run into one of them when the Ghost Distance Importance feature is enabled:

One:

Destroying an entity that carries cleanup components (i.e. every ghost instance) keeps its handle and loses every other component (expected). A command recorded for it earlier in the frame (for example; the netcode chunk partitioner’s SetSharedComponent, or the GhostDespawnParallelJob) then plays back against an entity that exists, but lacks said component. Checked builds throw; unchecked builds have those asserts compiled out, resolve the type to index -1, and either memcpy through Offsets[-1] (SIGSEGV in ProcessChain), or write the shared-component filter at a negative offset, and move the wrong chunk.

& Two:

Fully destroyed handles hit the same paths through a null archetype lookup.

Enabling Ghost Distance Importance triggers case 2 via the following repro:

  1. A ghost exists. It is destroyed. The destroyed ghost is kept alive by GhostCleanup and moved into the cleanup archetype, so existence checks still pass. Release builds strip AssertEntityHasComponent, so GetIndexInTypeArray returns -1.
  2. ChunkDataUtility.GetComponentDataWithTypeRW calls archetype->Chunks.SetChangeVersion(-1, chunk.ListIndex, GlobalSystemVersion) before it writes any data. In ArchetypeChunkData, ChunkIndices[Capacity] (4-byte ChunkIndex) sits directly before ChangeVersions[ComponentCount * Capacity]. So ChangeVersions[-Capacity + ListIndex] is exactly ChunkIndices[ListIndex]. The ghost’s chunk entry in the cleanup archetype’s chunk list gets overwritten with the current GlobalSystemVersion.
  3. The data write. The component value is then copied to chunk.Buffer + Offsets[-1] + SizeOfs[-1] * index. That either crashes right there in ECB playback (the SR-9609 signature) or silently scribbles memory.
  4. The crash. GhostSendSystem runs right after EndSimulationEntityCommandBufferSystem, so GhostDespawnParallelJob is the first job to walk the cleanup archetype once its chunk cache is rebuilt. That archetype changes constantly as ghosts are freed, so the rebuild comes quickly. The job then reads ChunkIndex = GlobalSystemVersion. The limit is kMaximumChunkCount = 2^20 (1,048,576) and the version passes that after a short uptime, so PerChunkArray.ChunkData[chunk] reads unmapped memory and segfaults.

So. Next steps:

  1. We could have added a mitigation to the N4E Ghost Distance Importance repro, but it’ll introduce main thread CPU overhead (to fully correctly track these destroyed entities), and lead to other correctness bugs, so we’ve opted out.
  2. If you’re not using the Ghost Distance Importance feature, your user projects are likely trying to operate on an entity that has been destroyed, or no longer has the component you’re setting. ECB playback is not guarded against this either. Thus, the most likely cause is: timing issues between creating an ECB command for an Entity, and said command being played back. Run a build with UNITY_DOTS_DEBUG in the scripting defines. Look for an ArgumentException saying GhostDistancePartitionShared has not been added to the entity. If you see it, you’re hitting the case we reproduced.
  3. Instead, we feel that Entities should not crash if you call SetComponentData or SetSharedComponentData invalidly, in prod, nor should it for an invalid chunk. It should no-op. That’s the fix we’re currently targeting.