fix: NetworkClient/Server: use the new custom NetworkLateUpdate instead of Unity's LateUpdate. fixes possible data races where other component's LateUpdate could be called before/after NetworkServer/Client LateUpdate causing non obvious data races. (#2604)

This commit is contained in:
vis2k 2021-03-02 15:28:30 +08:00 committed by GitHub
parent edea4089e4
commit 43e6fb4c53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 28 deletions

View File

@ -272,15 +272,7 @@ internal static void NetworkEarlyUpdate() {}
// NetworkLateUpdate called after any Update/FixedUpdate/LateUpdate
// (we add this to the UnityEngine in NetworkLoop)
internal static void NetworkLateUpdate() {}
// obsolete to not break people's projects. Update was public.
[Obsolete("NetworkClient.Update was renamed to LateUpdate because that's when it actually happens.")]
public static void Update() => LateUpdate();
// Called from NetworkManager in LateUpdate
// The user should never need to pump the update loop manually.
internal static void LateUpdate()
internal static void NetworkLateUpdate()
{
// local connection?
if (connection is LocalConnectionToServer localConnection)
@ -298,6 +290,10 @@ internal static void LateUpdate()
}
}
// obsolete to not break people's projects. Update was public.
[Obsolete("NetworkClient.Update is now called internally from our custom update loop. No need to call Update manually anymore.")]
public static void Update() => NetworkLateUpdate();
internal static void RegisterSystemHandlers(bool hostMode)
{
// host mode client / regular client react to some messages differently.

View File

@ -292,11 +292,6 @@ public virtual void Start()
/// </summary>
public virtual void LateUpdate()
{
// call it while the NetworkManager exists.
// -> we don't only call while Client/Server.Connected, because then we would stop if disconnected and the
// NetworkClient wouldn't receive the last Disconnect event, result in all kinds of issues
NetworkServer.LateUpdate();
NetworkClient.LateUpdate();
UpdateScene();
}

View File

@ -456,15 +456,7 @@ internal static void NetworkEarlyUpdate() {}
// NetworkLateUpdate called after any Update/FixedUpdate/LateUpdate
// (we add this to the UnityEngine in NetworkLoop)
internal static void NetworkLateUpdate() {}
// obsolete to not break people's projects. Update was public.
[Obsolete("NetworkServer.Update was renamed to LateUpdate because that's when it actually happens.")]
public static void Update() => LateUpdate();
// Called from NetworkManager in LateUpdate
// The user should never need to pump the update loop manually.
internal static void LateUpdate()
internal static void NetworkLateUpdate()
{
// don't need to update server if not active
if (!active) return;
@ -493,6 +485,10 @@ internal static void LateUpdate()
}
}
// obsolete to not break people's projects. Update was public.
[Obsolete("NetworkServer.Update is now called internally from our custom update loop. No need to call Update manually anymore.")]
public static void Update() => NetworkLateUpdate();
static void CheckForInactiveConnections()
{
if (!disconnectInactiveConnections)

View File

@ -1121,7 +1121,7 @@ public void UpdateDetectsNullEntryInSpawned()
// update
LogAssert.Expect(LogType.Warning, new Regex("Found 'null' entry in spawned list.*"));
NetworkServer.LateUpdate();
NetworkServer.NetworkLateUpdate();
// clean up
NetworkServer.Shutdown();
@ -1144,7 +1144,7 @@ public void UpdateDetectsDestroyedEntryInSpawned()
// update
LogAssert.Expect(LogType.Warning, new Regex("Found 'null' entry in spawned list.*"));
NetworkServer.LateUpdate();
NetworkServer.NetworkLateUpdate();
// clean up
NetworkServer.Shutdown();

View File

@ -82,8 +82,8 @@ protected T CreateHostObject<T>(bool spawnWithAuthority) where T : NetworkBehavi
protected static void ProcessMessages()
{
// run update so message are processed
NetworkServer.LateUpdate();
NetworkClient.LateUpdate();
NetworkServer.NetworkLateUpdate();
NetworkClient.NetworkLateUpdate();
}
}
}

View File

@ -69,7 +69,7 @@ public IEnumerator DisconnectTimeoutTest()
while (Time.time < endTime)
{
yield return null;
NetworkServer.LateUpdate();
NetworkServer.NetworkLateUpdate();
}
// host client connection should still be alive