Skip to content

Unregistering a named message handler in its callback delegate throws an exception #2394

@TheCaveOfWonders

Description

@TheCaveOfWonders

If we unregister a named message handler during its callback execution, we get the following error.

KeyNotFoundException: The given key '1791774354' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <0da48681ced7494d83ae6a612d2206fc>:0)
Unity.Netcode.CustomMessagingManager.InvokeNamedMessage (System.UInt64 hash, System.UInt64 sender, Unity.Netcode.FastBufferReader reader, System.Int32 serializedHeaderSize) (at Library/PackageCache/[email protected]/Runtime/Messaging/CustomMessageManager.cs:173)
Unity.Netcode.NamedMessage.Handle (Unity.Netcode.NetworkContext& context) (at Library/PackageCache/[email protected]/Runtime/Messaging/Messages/NamedMessage.cs:27)
Unity.Netcode.MessagingSystem.ReceiveMessage[T] (Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkContext& context, Unity.Netcode.MessagingSystem system) (at Library/PackageCache/[email protected]/Runtime/Messaging/MessagingSystem.cs:511)
Unity.Netcode.MessagingSystem.HandleMessage (Unity.Netcode.MessageHeader& header, Unity.Netcode.FastBufferReader reader, System.UInt64 senderId, System.Single timestamp, System.Int32 serializedHeaderSize) (at Library/PackageCache/[email protected]/Runtime/Messaging/MessagingSystem.cs:384)
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.MessagingSystem:HandleMessage(MessageHeader&, FastBufferReader, UInt64, Single, Int32) (at Library/PackageCache/[email protected]/Runtime/Messaging/MessagingSystem.cs:388)
Unity.Netcode.MessagingSystem:ProcessIncomingMessageQueue() (at Library/PackageCache/[email protected]/Runtime/Messaging/MessagingSystem.cs:404)
Unity.Netcode.NetworkManager:OnNetworkEarlyUpdate() (at Library/PackageCache/[email protected]/Runtime/Core/NetworkManager.cs:1600)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library/PackageCache/[email protected]/Runtime/Core/NetworkManager.cs:1532)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at Library/PackageCache/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:185)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at Library/PackageCache/[email protected]/Runtime/Core/NetworkUpdateLoop.cs:208)

Reproduce Steps

NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("Hello", (senderClientId, stream) =>
            {
                NetworkManager.Singleton.CustomMessagingManager.UnregisterNamedMessageHandler("Hello");
                // do work.
            });

Actual Outcome

No error.

Expected Outcome

Error is thrown.

Context

Why would we want to do this? Because in some cases we no longer need to listen to such a message as soon as we receive it.

Environment

  • OS: Win10
  • Unity Version: 2022.2.3
  • Netcode Version: 1.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumThis issue has medium priority and may take some time to be resolvedstat:importedStatus - Issue is tracked internally at Unitytype:bugBug Report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions