RoyNexus
RoyNexus3d ago

RealtimeView.RequestOwnershipOfSelfAndChildren() causes error: Collection was modified

Any ideas how to fix this? InvalidOperationException: Collection was modified; enumeration operation may not execute. System.Collections.Generic.List1+Enumerator[T].MoveNextRare () (at <b4bf55bf6893420ebddeeda99394cb6d>:0) System.Collections.Generic.List1+Enumerator[T].MoveNext () (at <b4bf55bf6893420ebddeeda99394cb6d>:0) Normal.Realtime.RealtimeView.RequestOwnershipOfSelfAndChildren () (at <b5c6492a452a4d33b2ddfc50f28da200>:0)
22 Replies
kapa.ai
kapa.ai3d ago
Hi @RoyNexus. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but the knowledge sources do not contain any information about the internal implementation or known issues with RealtimeView.RequestOwnershipOfSelfAndChildren() causing a "Collection was modified" error. There is no documentation or troubleshooting guidance for this specific exception in the provided materials. If you are encountering this error, it may be due to modifying a collection (such as adding or removing child views or components) while iterating over it inside the RequestOwnershipOfSelfAndChildren() method. However, without explicit documentation or guidance from the knowledge sources, I cannot provide a definitive solution or workaround. For further assistance, you may want to consult the official Normcore support channels or community forums, as this issue is not covered in the current documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3d ago
<@362795742198235136> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
RoyNexus
RoyNexusOP3d ago
for more context I have a single scene entity of the RealtimeView, even if I trigger RequestOwnershipOfSelfAndChildren() via script or through the editor UI I get the same error
NormalMark
NormalMark2d ago
Hey, trying to repro this bug on our side Is this on Normcore 2.15.4?
NormalMark
NormalMark2d ago
I made a quick setup where I parented multiple RealtimeViews & RealtimeTransforms in the scene and clicked "Request Ownership of Self and Children" on the root:
No description
NormalMark
NormalMark2d ago
but I wasn't able to get the error
RoyNexus
RoyNexusOP2d ago
2.15.1 This particular RealtimeView only has a custom model attached to it. But it only has a custom component, but no children
RoyNexus
RoyNexusOP2d ago
No description
RoyNexus
RoyNexusOP2d ago
maybe it makes no sense to call RequestOwnershipOfSelfAndChildren(), but surprised that error happens anyway I take that back, I do want the ownership of HostSync as well
NormalMark
NormalMark2d ago
Downgraded to 2.15.1 but not able to trigger it. Lmk if this setup is different from yours:
No description
NormalMark
NormalMark2d ago
Test A is:
[RealtimeModel(true)]
public partial class TestAModel {

}

public class TestA : RealtimeComponent<TestAModel> {

}
[RealtimeModel(true)]
public partial class TestAModel {

}

public class TestA : RealtimeComponent<TestAModel> {

}
RoyNexus
RoyNexusOP2d ago
yeah, I'm guessing I'm trying to claim ownership of the model while is still not ready?
RoyNexus
RoyNexusOP2d ago
No description
NormalMark
NormalMark2d ago
Hmm this one works fine too:
using Normal.Realtime;

[RealtimeModel(true)]
public partial class TestAModel {
[RealtimeProperty(1, true)]
private int _a;

[RealtimeProperty(2, false)]
private int _b;
}

public class TestA : RealtimeComponent<TestAModel> {
public Realtime targetRealtime;

private void Start() {
targetRealtime.didConnectToRoom += x => {
realtimeView.RequestOwnershipOfSelfAndChildren();
};
}

protected override void OnRealtimeModelReplaced(TestAModel previousModel, TestAModel currentModel) {
base.OnRealtimeModelReplaced(previousModel, currentModel);

if (currentModel != null) {
currentModel.a = 10;
currentModel.b = 10;
}
}
}
using Normal.Realtime;

[RealtimeModel(true)]
public partial class TestAModel {
[RealtimeProperty(1, true)]
private int _a;

[RealtimeProperty(2, false)]
private int _b;
}

public class TestA : RealtimeComponent<TestAModel> {
public Realtime targetRealtime;

private void Start() {
targetRealtime.didConnectToRoom += x => {
realtimeView.RequestOwnershipOfSelfAndChildren();
};
}

protected override void OnRealtimeModelReplaced(TestAModel previousModel, TestAModel currentModel) {
base.OnRealtimeModelReplaced(previousModel, currentModel);

if (currentModel != null) {
currentModel.a = 10;
currentModel.b = 10;
}
}
}
If you can put together a minimal repro project I'd be glad to take a look
RoyNexus
RoyNexusOP2d ago
yeah, actually I think I found what triggers it! I have this other realtime view (separate in the hierarchy) that as soon as I do _realtimeView.RequestOwnershipOfSelfAnChildren() on it, it triggers the Collection error on the HostSync.cs. I'll clarify here:
RoyNexus
RoyNexusOP2d ago
This is the code on PlayerSpawnPoseSync
No description
RoyNexus
RoyNexusOP2d ago
The views for HostSync and PlayerSpawnPoseSync are independent of each other on the hierarchy. And they are both in the scene
RoyNexus
RoyNexusOP2d ago
No description
RoyNexus
RoyNexusOP2d ago
as soon as I do RequestOwnershipOfSelfAndChildren() on PLayerSpawnPoseSync's RealtimeView. For some reason HostSync is affected???? I guess a better question is. Is the RealtimeView ready for claiming ownership of it and its children as soon as I'm connected to realtime room? or should I wait for RealtimeView.didReplaceAllComponentModels event to then claim ownership?
NormalMark
NormalMark2d ago
We only fire Realtime.didConnectToRoom after we set the models on all the scene views, so they should be ready for ownership changes by then
RoyNexus
RoyNexusOP2d ago
thanks, that's very helpful. Ok so the issue is in my code, and it works as follows: HostSync's RealtimeView.RequestOwnershipOfSelfAndChildren() => HostSync's OnOwnerIDSelfDidChange => hostSelected?.Invoke(newHostId) => PoseSelection listening to hostSelected event => Call to PlayerSpawnPose's Occupy => call to PlayerSpawnPose's RealtimeView.RequestOwnershipOfSelfAndChildren() All of this happens on the same frame, and is the same call trace. It seems that Normcore doesn't like this "nested" call to RequestOwnershipOfSelfAndChildren(), and it's why I'm seing it complain on HostSync, and not on PlayerSpawnPoses the way I got rid of the error, was to not "nest" the calls to different realtimeView's RequestOwnershipOfSelfAndChildren()
NormalMark
NormalMark2d ago
Glad you found a workaround. But yeah seems like a bug in Normcore, I'll log it internally

Did you find this page helpful?