Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug InvalidOperationException: GetSingleton<Unity.NetCode.NetworkId>() GhostUpdateSystem.cs:1139

Discussion in 'NetCode for ECS' started by kirkgamesllc, Nov 15, 2023.

  1. kirkgamesllc

    kirkgamesllc

    Joined:
    Jan 7, 2023
    Posts:
    23
    Unity Editor 2022.3.13f1
    macOS 14.0 (23A344)
    dots 1.2.0-pre.3

    I'm receiving these 3 warnings/errors when I disconnect client using:
    Code (CSharp):
    1. ecb.AddComponent(entity, new NetworkStreamRequestDisconnect());
    CommandSendPacket BeginSend failed with errorCode: -3!
    UnityEngine.Debug:LogWarning (object)
    Unity.NetCode.NetDebug:LogWarning (Unity.Collections.FixedString512Bytes&) (at ./Library/PackageCache/com.unity.netcode@1.2.0-exp.3/Runtime/Debug/NetDebug.cs:270)
    Unity.NetCode.CommandSendPacketSystem/CommandSendPacket:Execute (Unity.Entities.DynamicBuffer`1<Unity.NetCode.OutgoingCommandDataStreamBuffer>,Unity.NetCode.NetworkStreamConnection&,Unity.NetCode.NetworkSnapshotAck&) (at ./Library/PackageCache/com.unity.netcode@1.2.0-exp.3/Runtime/Command/CommandSendSystem.cs:186)

    InvalidOperationException: GetSingleton<Unity.NetCode.NetworkId>() requires that exactly one entity exists that match this query, but there are none. Are you missing a call to RequireForUpdate<T>()? You could also use TryGetSingleton<T>()
    Unity.Entities.EntityQueryImpl.GetSingleton[T] () (at ./Library/PackageCache/com.unity.entities@1.2.0-exp.3/Unity.Entities/Iterators/EntityQuery.cs:1485)
    Unity.Entities.EntityQuery.GetSingleton[T] () (at ./Library/PackageCache/com.unity.entities@1.2.0-exp.3/Unity.Entities/Iterators/EntityQuery.cs:2785)
    Unity.NetCode.GhostUpdateSystem.OnUpdate (Unity.Entities.SystemState& systemState) (at ./Library/PackageCache/com.unity.netcode@1.2.0-exp.3/Runtime/Snapshot/GhostUpdateSystem.cs:1139)

    IndexOutOfRangeException: Index 0 is out of range of '0' Length.
    Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:325)
    Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:166)
    Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:189)
    Unity.NetCode.CommandSendSystem`2+SendJobData[TCommandDataSerializer,TCommandData].Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 orderIndex) (at ./Library/PackageCache/com.unity.netcode@1.2.0-exp.3/Runtime/Command/CommandSendSystem.cs:432)

    GhostUpdateSystem line 1139
    Code (CSharp):
    1. var localNetworkId = SystemAPI.GetSingleton<NetworkId>().Value;
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    873
    These are no warnings: these are bad and real errors.

    The first strange thing is the first error: CommandSendPacket that fail with -3 (NetworkStateMismatch) that suggest the connection was somewhat closed or in strange state.
    Indeed when we process the NetworkStreamRequestDisconnect request we disconnect the connection first (that is then in Disconnecting state) and at the beginning of the next frame we dispose the connection entity.
    But for the whole frame that connection should still be in a usable state. None touches the NetworkId or something like that.

    To which ECB you added the NetworkStreamRequestDisconnect ? Begin frame, end frame, none of these ?
    Where the system that does that run ?
    Just to understand the timing in order to repro the problem,

    Then, in general,.. the GhostUpdateSystem only runs if there are connection that has NetworkStreamInGame. So even if you add the NetworkStreamRequestDisconnect it runs and that it is fine.
    What I find controversial here is that the system is running (so all the RequireForUpdate condition passes) but there are no connection with NetworkId component.. that is really strange.

    Are you sure aren't you doing something else to the entity that has the NetworkStreamConnection component, or in general to the connection (like: calling Disconnect() on the NetworkStreamDriver) ?
     
  3. kirkgamesllc

    kirkgamesllc

    Joined:
    Jan 7, 2023
    Posts:
    23

    The problem was that NetworkStreamInGame wasn't being removed (line of code was probably removed in the refactor), now that I'm doing that along with the NetworkStreamRequestDisconnect everything is fine.

    Thank you for your response.
     
  4. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    873
    Yeah, but that highlighted a bug anyway.. so we will make this a little more robust