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

Client errors during networked scene loads. Key already exists in dictionary that maps server scene handles to client scene handles. #1102

Closed SubtleSanity closed 3 years ago

SubtleSanity commented 3 years ago

Describe the bug Exception thrown on client when a network scene load is performed

When the client loads the scene (after the server calls for a networked scene load) it adds an entry in a dictionary that links the scene instance handle at the client to the scene instance handle at the server. This throws an exception as the server scene handle is already present in the dictionary. It appears that when the server performs a scene load it is sending the wrong handle to connected clients (it might be sending the handle for the current scene instead of the newly loaded one?).

Some notes:

Stack trace of error:

Exception: Server Scene Handle (-74) already exist! Happened during scene load of SceneToLoad with Client Handle (-31058) Unity.Netcode.NetworkSceneManager.OnSceneLoaded (System.String sceneName) (at Library/PackageCache/com.unity.netcode.gameobjects@15d5bef049/Runtime/SceneManagement/NetworkSceneManager.cs:836) Unity.Netcode.NetworkSceneManager+<>c__DisplayClass39_0.b__0 (UnityEngine.AsyncOperation asyncOp2) (at Library/PackageCache/com.unity.netcode.gameobjects@15d5bef049/Runtime/SceneManagement/NetworkSceneManager.cs:776) UnityEngine.AsyncOperation.InvokeCompletionEvent () (at <4a29708cee654ac0aca16c9d5624806a>:0)

To Reproduce

See sample project attached at the end You need to run two copies, so either duplicate the project or build it.

Run the project Click "Start as Server"

Run the project Click "Connect as Client"

On the server project Click "Load additive scene"

Observe error thrown on client project

Note that the error does not occur if you start the server project as 'host' rather than 'server', and that it only happens for scene loads that are initiated while the client is connected and not for scene loads that were initiated before the client connects.

Environment (please complete the following information):

Additional context

TestProject.zip Made in 2021.1.16f but shouldn't be version dependent. Make sure you run from StartScene

NoelStephensUnity commented 3 years ago

SubtleSanity, This was a very good catch, and I have a PR coming up to fix this issue. Using the testproject you provide I did determine a flaw in my original design with the SceneEventData being a singular instance. The fix will include a SceneEventData pool that will avoid this type of issue. Basically, since your scene was so small the server was able to send an event back to the client before the client was finished loading and the SceneEventData (singular) instance was being overwritten.

Thank you for your submission and taking the time to put the project together, it helped me track this down very quickly! 🥇