com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

NullReferenceException during client Shutdown

Open Risord opened this issue 2 years ago • 4 comments

Description

I use heavily custom messages and time to time client shutdown crashes. Don't have reproduce steps but concept is pretty simple:

// At Unity.Netcode.NetworkManager.ShutdownInternal()

            ...
            CustomMessagingManager = null;  // <= !!!

            RpcTarget?.Dispose();
            RpcTarget = null;

            BehaviourUpdater?.Shutdown();
            BehaviourUpdater = null;

            // Shutdown connection manager last which shuts down transport
            ConnectionManager.Shutdown();  // <= !!!
            ...

Problems is that ConnectionManager.Shutdown() does execute received messages and deep in stack there is UnnamedMessage.Handle which access NetworkManager.CustomMessagingManager => bhoom.

On surface level it seems that NetworkManager.CustomMessagingManager should be nullified after ConnectionManager.Shutdown();. I tried that and so far things seems to work well but my knowledge from library internals are limited.

Environment

  • OS: Windows 11
  • Unity Version: 2022.3.24
  • Netcode Version: 1.8.1

Additional Context

Complete stacktrace:

NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.UnnamedMessage.Handle (Unity.Netcode.NetworkContext& context) (at ./Packages/[email protected]/Runtime/Messaging/Messages/UnnamedMessage.cs:23)
Unity.Netcode.NetworkMessageManager.ReceiveMessage[T] (Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkContext& context, Unity.Netcode.NetworkMessageManager manager) (at ./Packages/[email protected]/Runtime/Messaging/NetworkMessageManager.cs:582)
Unity.Netcode.NetworkMessageManager.HandleMessage (Unity.Netcode.NetworkMessageHeader& header, Unity.Netcode.FastBufferReader reader, System.UInt64 senderId, System.Single timestamp, System.Int32 serializedHeaderSize) (at ./Packages/[email protected]/Runtime/Messaging/NetworkMessageManager.cs:446)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.NetworkMessageManager:HandleMessage(NetworkMessageHeader&, FastBufferReader, UInt64, Single, Int32) (at ./Packages/[email protected]/Runtime/Messaging/NetworkMessageManager.cs:450)
Unity.Netcode.NetworkMessageManager:ProcessIncomingMessageQueue() (at ./Packages/[email protected]/Runtime/Messaging/NetworkMessageManager.cs:472)
Unity.Netcode.NetworkConnectionManager:DisconnectEventHandler(UInt64) (at ./Packages/[email protected]/Runtime/Connection/NetworkConnectionManager.cs:492)
Unity.Netcode.NetworkConnectionManager:HandleNetworkEvent(NetworkEvent, UInt64, ArraySegment`1, Single) (at ./Packages/[email protected]/Runtime/Connection/NetworkConnectionManager.cs:394)
Unity.Netcode.NetworkTransport:InvokeOnTransportEvent(NetworkEvent, UInt64, ArraySegment`1, Single) (at ./Packages/[email protected]/Runtime/Transports/NetworkTransport.cs:49)
Unity.Netcode.Transports.UTP.UnityTransport:DisconnectLocalClient() (at ./Packages/[email protected]/Runtime/Transports/UTP/UnityTransport.cs:1154)
Unity.Netcode.NetworkConnectionManager:Shutdown() (at ./Packages/[email protected]/Runtime/Connection/NetworkConnectionManager.cs:1145)
Unity.Netcode.NetworkManager:ShutdownInternal() (at ./Packages/[email protected]/Runtime/Core/NetworkManager.cs:1166)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at ./Packages/[email protected]/Runtime/Core/NetworkManager.cs:84)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at ./Packages/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:185)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at ./Packages/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:268)

Risord avatar Apr 11 '24 21:04 Risord

The issue you're encountering appears to be a race condition or order-of-operations problem in the shutdown process of the Unity.Netcode.NetworkManager. Specifically, the problem is that the ConnectionManager.Shutdown() call can trigger the processing of messages that still reference the CustomMessagingManager, which has already been set to null, leading to a NullReferenceException.

Proposed Solution: To resolve this issue, the order of shutting down components in NetworkManager.ShutdownInternal() should be adjusted so that CustomMessagingManager is not set to null until after ConnectionManager.Shutdown() is complete. This change ensures that any messages processed during the shutdown won't attempt to access a null CustomMessagingManager.

Explanation: ConnectionManager.Shutdown() First: By shutting down the ConnectionManager first, you ensure that any pending network events or messages are processed while the CustomMessagingManager is still intact and can handle any necessary cleanup or message handling.

CustomMessagingManager Nullification: Only after the ConnectionManager has been properly shut down should you nullify the CustomMessagingManager. This prevents any remaining or incoming messages from attempting to use a null reference, avoiding the NullReferenceException.

quantum-shubham avatar Aug 21 '24 14:08 quantum-shubham

It seems Issue was fixed, looking in code.

Corriscant avatar Oct 26 '24 10:10 Corriscant

Hey, I can see that it should be resolved in NGO v1.9.0, were you using that version @Corriscant?

michalChrobot avatar Nov 29 '24 13:11 michalChrobot

closing this issue as it has since been resolved.

NoelStephensUnity avatar Feb 24 '25 20:02 NoelStephensUnity