GeometryCache: data race between FGeometryCacheSceneProxy::CreateHitProxies and the render-thread FrameUpdate enqueued by the proxy constructor

Hello

We are experimenting intermittent ensures during PIE start / actor registration when a level contains a UGeometryCacheComponent:

ensureMsgf(CurrentNum == InitialNum, TEXT(“Array has changed during ranged-for iteration!”))

Callstack is on the game thread (or a parallel-game-thread registration task) inside

FGeometryCacheSceneProxy::CreateHitProxies, iterating Track->MeshData->BatchesInfo.

Root cause

1. FGeometryCacheSceneProxy::FGeometryCacheSceneProxy (GeometryCacheSceneProxy.cpp ~L162-180) decodes the

current frame into NewSection->MeshData on the creating thread, then immediately enqueues

ENQUEUE_RENDER_COMMAND(FGeometryCacheUpdateAnimation) { SceneProxy->FrameUpdate(RHICmdList); }.

2. After CreateSceneProxy() returns, FScene::BatchAddPrimitivesInternal constructs FPrimitiveSceneInfo,

which (GIsEditor) calls Proxy->CreateHitProxies (PrimitiveSceneInfo.cpp ~L240). That function

ranged-fors over Track->MeshData->BatchesInfo (GeometryCacheSceneProxy.cpp ~L354).

3. The render thread may already be executing the FrameUpdate from step 1. For a new track proxy

FrameIndex == NextFrameIndex == -1, so FrameUpdate takes the “seek” branch

(GeometryCacheSceneProxy.cpp ~L734-736):

if (TrackProxy->GetMeshData(FrameIndex, *TrackProxy->MeshData))

which ends in FCodecV1Decoder::DecodeFrameData (CodecV1.cpp ~L1110):

Reader << OutMeshData.BatchesInfo;

FGeometryCacheMeshBatchInfo has a custom operator<<, so TArray loading uses the per-item path:

A.Empty(SerializeNum) followed by AddDefaulted_GetRef per element. Num transiently goes to 0 and

back up while the other thread is iterating the same array, and the ranged-for check fires.

The final count is usually identical, which is why it is intermittent.

Suggested fix

Do not hand the proxy to the render thread from the constructor. Move the two constructor enqueues

(FGeometryCacheUpdateAnimation / FrameUpdate and FGeometryCacheInitRayTracingGeometry) into an

override of FPrimitiveSceneProxy::CreateRenderThreadResources, which runs inside the scene’s

AddPrimitiveCommand, i.e. after the FPrimitiveSceneInfo and its hit proxies exist.

Thanks

[Attachment Removed]

Steps to Reproduce
Engine version: 5.6, 5.7 and 5.8

Platform: Win64, editor (PIE). Not reproducible in cooked builds because the reader is editor-only.

Use the geometry cache plugin in the editor and go to PIE

[Attachment Removed]

Hi Sergi,

Thanks for reporting this issue, it does look like it still may occur in latest and I’m attempting to set up a repro scene to validate that or if some other code change in latest prevents it from happening.

[Attachment Removed]

I went ahead and created a new issue which should be visible publicly soon here https://issues.unrealengine.com/issue/UE\-402663 for tracking. We’ll likely implement a similar fix to the one proposed.

[Attachment Removed]