colyseus / colyseus-unity-sdk

⚔ Colyseus Multiplayer SDK for Unity
https://docs.colyseus.io/getting-started/unity-sdk/
MIT License
371 stars 102 forks source link

[Bug] `KeyNotFoundException` exception in garbage collector #175

Open rnd256 opened 2 years ago

rnd256 commented 2 years ago

Colyseus Unity3d version: 0.14.8 (latest). It also occurs with version 0.14.7. I haven't tested other versions. Colyseus server version: 0.14.23 (latest).

Occasionally, after removing a Schema object from a MapSchema, this line throws the error "KeyNotFoundException: The given key was not present in the dictionary": https://github.com/colyseus/colyseus-unity3d/blob/f80f7f402ad2e51c4ebad59db389c0b4d05ee1ad/Assets/Colyseus/Runtime/Scripts/Serializer/Schema/ColyseusReferenceTracker.cs#L85

If I continue to play my game after this error, I eventually hit a "refId not found" error as well: https://github.com/colyseus/colyseus-unity3d/blob/f80f7f402ad2e51c4ebad59db389c0b4d05ee1ad/Assets/Colyseus/Runtime/Scripts/Serializer/Schema/Schema.cs#L399

This makes me think that there's some step where a Schema within a MapSchema isn't properly getting added to refCounts and refs in the Unity3d client.

rnd256 commented 2 years ago

My temporary workaround is to completely disable the garbage collector by commenting out this line:

https://github.com/colyseus/colyseus-unity3d/blob/f80f7f402ad2e51c4ebad59db389c0b4d05ee1ad/Assets/Colyseus/Runtime/Scripts/Serializer/Schema/Schema.cs#L648

That seems to prevent both of the above errors.

endel commented 1 year ago

It seems this issue has some connection to this one, which has been (hopefully) fixed here: https://github.com/colyseus/colyseus/issues/495#issuecomment-1276545324