Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

Clients are receiving data from objects not visible to them. #915

Closed Sphisis closed 3 years ago

Sphisis commented 3 years ago

Hi! I have a setup where I have clients and a headless server. Each client owns one object on the server and they are not destroyed on disconnect. Object ownerships are reclaimed by the clients on reconnection. In addition each client observes other players' objects only based on the distance from his own object. Player objects too far are invisible and unaware clients should not receive any traffic concerning them.

Describe the bug If object is set invisible to a client A, but visible for client B, client A still receives message sync messages when server changes the object state via NetworkVariable:

[MLAPI] NETWORK_VARIABLE_DELTA message received for a non-existent object with networkObjectId: 2. This delta will be buffered and might be recovered.

When client A owned object turns invisible, it causes client B report an error:

Trying to destroy object 2 but it doesn't seem to exist anymore!

Finally if hidden object is destroyed, unaware clients report:

KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary2[TKey,TValue].get_Item (TKey key) [0x0001e] in <695d1cc93cca45069c528c15c9fdd749>:0 at MLAPI.Messaging.InternalMessageHandler.HandleChangeOwner (System.UInt64 clientId, System.IO.Stream stream) [0x00015] in <beeac23026c94eb3810572809f7f06dc>:0 at MLAPI.NetworkManager.HandleIncomingData (System.UInt64 clientId, MLAPI.Transports.NetworkChannel networkChannel, System.ArraySegment1[T] data, System.Single receiveTime, System.Boolean allowBuffer) [0x0020d] in :0 at MLAPI.NetworkManager.HandleRawTransportPoll (MLAPI.Transports.NetworkEvent networkEvent, System.UInt64 clientId, MLAPI.Transports.NetworkChannel networkChannel, System.ArraySegment`1[T] payload, System.Single receiveTime) [0x000cd] in :0 at MLAPI.NetworkManager.OnNetworkEarlyUpdate () [0x0007e] in :0 at MLAPI.NetworkManager.NetworkUpdate (MLAPI.NetworkUpdateStage updateStage) [0x00009] in :0 at MLAPI.NetworkUpdateLoop.RunNetworkUpdateStage (MLAPI.NetworkUpdateStage updateStage) [0x00021] in :0 at MLAPI.NetworkUpdateLoop+NetworkEarlyUpdate+<>c.b__0_0 () [0x00000] in :0

Expected outcome I would expect that clients unaware of a given object do not receive any information sent by it.

Environment (please complete the following information):

will-mearns commented 3 years ago

Committing to within MTT-857 backlog item

mraue commented 3 years ago

Has this been fixed?

I was searching for the commit, but could not find it. I reported this issue some time ago in discord and also found the code location causing it:

Link to discord

jeffreyrainy commented 3 years ago

Hi @Sphisis ,

Thanks for reporting this, and sorry for the long time to address. With the work we've been doing, most of the issues were already fixed, but there remained:

The PR https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/pull/1099 addresses these two. This is a PR towards the develop branch, for now.

If you are able to try develop, you can check it, once merged. If not, it should make it into a release eventually.

Any comments around the fix are welcome!

Thanks