Replicated Scene Components being duplicated on Clients

Hi, I’ve been running into a bug where we get duplicates of replicated Scene Components added to the AttachChildren array of their parents on Clients on cooked builds. This sometimes ends up hitting a check in USceneComponent::SendAllEndOfFrameUpdates in LevelTick.cpp because the component will get unmarked for updates the first time it gets processed.

After investigating the cause of this it seems like this block of code in USceneComponent::AttachToComponent is resulting in duplicates being created on clients.

		// Preserve order of previous attachment if valid (in case we're doing a reattach operation inside a loop that might assume the AttachChildren order won't change)
		// Don't do this if updating attachment from replication to avoid overwriting addresses in AttachChildren that may be unmapped
		if(LastAttachIndex != INDEX_NONE && !bNetUpdateAttachment)
		{
			Parent->AttachChildren.Insert(this, LastAttachIndex);
		}
		else
		{
			Parent->AttachChildren.Add(this);
		}

Both of these code paths can result in duplicates being added to the arrays. These will get cleaned up on the client at the beginning of USceneComponent::OnRep_AttachChildren() but only if the Child Component’s AttachParent property is replicated before the ParentComponents AttachChildren list.

Changing the second part to AttachChildren.AddUnique or gating it by checking if LastAttachIndex== INDEX_NONE fixes the bug, but I’m not sure why we would want to allow adding duplicates in the first place. It seems to me like removing this code block and replacing it with this:

if(LastAttachIndex == INDEX_NONE) { Parent->AttachChildren.Add(this);}

Would fix the issue and prevent duplicates from being added at all? Which would mean the de-duplication logic in USceneComponent::OnRep_AttachChildren() wouldn’t be necessary either.

Thanks!