Unity-Technologies / multiplayer-community-contributions

Community contributions to Unity Multiplayer Networking products and services.
MIT License
429 stars 161 forks source link

[Photon] `KeyNotFoundException: The given key '2' was not present in the dictionary` when collecting a client #119

Closed brogan89 closed 2 years ago

brogan89 commented 2 years ago

I'm getting a KeyNotFoundException on the client-side shortly after running StartClient(). The exception is not happening with UNetTransport so I'm suspecting something to do with the PhotonRealtimeTransport.

Reproduction steps:

  1. Create an empty scene and add NetworkManager with PhotonRealtimeTransport.
  2. Add empty gameObject with a NetworkObject component and add as PlayerPrefab in NetworkManager
  3. Either create a build for the client or use ParrelSync.
  4. Start a Host then start a Client
  5. See the exception in the console. I've posted the full stack from below as well.

Details: Unity Version: 2021.2.1f1 Netcode: 1.0.0-pre.3 Photon transport: 2.0.0 - commit hash: b32ffdd4648a5f0359f3c9e4ffabaa6e7c91d8aa

[Exception] KeyNotFoundException: The given key '2' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item() at <c183552e59b24622ab6b4273bc2ea328>:0

NetworkManager.TransportIdToClientId() at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1244
1242:   private ulong TransportIdToClientId(ulong transportId)
1243:   {
-->1244:       return transportId == m_ServerTransportId ? ServerClientId : m_TransportIdToClientIdMap[transportId];
1245:   }

NetworkManager.HandleRawTransportPoll() at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1304
1302:   }
-->1304:   clientId = TransportIdToClientId(clientId);
1306:   HandleIncomingData(clientId, payload, receiveTime);

NetworkTransport.InvokeOnTransportEvent() at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Transports/NetworkTransport.cs:42
40:   protected void InvokeOnTransportEvent(NetworkEvent eventType, ulong clientId, ArraySegment<byte> payload, float receiveTime)
41:   {
-->42:       OnTransportEvent?.Invoke(eventType, clientId, payload, receiveTime);
43:   }

PhotonRealtimeTransport.InvokeTransportEvent() at Library/PackageCache/com.community.netcode.transport.photon-realtime@b32ffdd464/Runtime/PhotonRealtimeTransport.cs:407
405:           goto default;
406:       default:
-->407:           InvokeOnTransportEvent(networkEvent, senderId, payload, Time.realtimeSinceStartup);
408:           break;
409:   }

PhotonRealtimeTransport.OnEvent() at Library/PackageCache/com.community.netcode.transport.photon-realtime@b32ffdd464/Runtime/PhotonRealtimeTransport.cs:371
369:           reader.ReadBytesSafe(ref dataArray, length);
-->371:           InvokeTransportEvent(NetworkEvent.Data, senderId, new ArraySegment<byte>(dataArray, 0, dataArray.Length));
372:       }
373:   }

LoadBalancingClient.OnEvent() at Library/PackageCache/com.community.netcode.transport.photon-realtime@b32ffdd464/Runtime/Photon/PhotonRealtime/Code/LoadBalancingClient.cs:3335
3334:       this.UpdateCallbackTargets();
-->3335:       if (this.EventReceived != null) this.EventReceived(photonEvent);
3336:   }

PeerBase.DeserializeMessageAndCallback() at <1681fcfb2a274234971e611aa510f646>:0

EnetPeer.DispatchIncomingCommands() at <1681fcfb2a274234971e611aa510f646>:0

PhotonPeer.DispatchIncomingCommands() at <1681fcfb2a274234971e611aa510f646>:0

PhotonRealtimeTransport.Update() at Library/PackageCache/com.community.netcode.transport.photon-realtime@b32ffdd464/Runtime/PhotonRealtimeTransport.cs:105
103:       if (m_Client != null)
104:       {
-->105:           do { } while (m_Client.LoadBalancingPeer.DispatchIncomingCommands());
106:       }
107:   }
brogan89 commented 2 years ago

Cool, #124 seems to have fixed that issue. Thanks :)