Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UNET "ReadBytes out of Range" and "ReadString too long" Errors

Discussion in 'UNet' started by mischa2k, Jan 14, 2016.

  1. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Hello everyone,
    I sometimes randomly get the following errors on Unity 5.3.1f1:

    ReadBytes out of Range (happens in generated Code, I did not write the Serialization myself):

    Code (CSharp):
    1.   IndexOutOfRangeException: NetworkReader:ReadBytes out of range: (25928) NetBuf sz:754 pos:41
    2.   UnityEngine.Networking.NetBuffer.ReadBytes (System.Byte[] buffer, UInt32 count) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkBuffer.cs:45)
    3.   UnityEngine.Networking.NetworkReader.ReadString () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkReader.cs:302)
    4.   Unity.GeneratedNetworkCode._ReadItem_None (UnityEngine.Networking.NetworkReader reader)
    5.   Unity.GeneratedNetworkCode._ReadArrayItem_None (UnityEngine.Networking.NetworkReader reader)
    6.   Unity.GeneratedNetworkCode._ReadCharacterData_None (UnityEngine.Networking.NetworkReader reader)
    7.   Unity.GeneratedNetworkCode._ReadArrayCharacterData_None (UnityEngine.Networking.NetworkReader reader)
    8.   CharactersAvailableMsg.Deserialize (UnityEngine.Networking.NetworkReader reader)
    9.   UnityEngine.Networking.NetworkMessage.ReadMessage[CharactersAvailableMsg] () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/UNetwork.cs:161)
    10.   NetworkManagerCustom.OnClientCharactersAvailable (UnityEngine.Networking.NetworkMessage netMsg) (at Assets/uMMORPG/Scripts/NetworkManagerCustom.cs:179)
    11.   UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:430)
    12.   UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:386)
    13.   UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:536)
    14.   UnityEngine.Networking.NetworkClient.Update () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:648)
    15.   UnityEngine.Networking.NetworkClient.UpdateClients () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:861)
    16.   UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1044)
    17.  
    ReadString too long (happens in generated Code, I did not write the Serialization myself):
    Code (CSharp):
    1.   IndexOutOfRangeException: ReadString() too long: 52800
    2. UnityEngine.Networking.NetworkReader.ReadString () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkReader.cs:294)
    3.   Entity.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState)
    4.   Monster.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState)
    5.   UnityEngine.Networking.NetworkIdentity.OnUpdateVars (UnityEngine.Networking.NetworkReader reader, Boolean initialState) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:742)
    6.   UnityEngine.Networking.ClientScene.ApplySpawnPayload (UnityEngine.Networking.NetworkIdentity uv, Vector3 position, System.Byte[] payload, NetworkInstanceId netId, UnityEngine.GameObject newGameObject) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:478)
    7.   UnityEngine.Networking.ClientScene.OnObjectSpawnScene (UnityEngine.Networking.NetworkMessage netMsg) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:589)
    8.   UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:430)
    9.   UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:386)
    10.   UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:536)
    11.   UnityEngine.Networking.NetworkClient.Update () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:648)
    12.   UnityEngine.Networking.NetworkClient.UpdateClients () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:861)
    13.   UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at /home/builduser/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1044)
    14.  
    I am still trying to track them down further, but it's not that easy because they don't appear all the time. I remember that they randomly appeared when Network Simulation was on, and right now they appear if I try to connect to a VPS server of someone who purchased the package. They also appear when a player joins a scene with something like 500 Monsters around him. Note that I use the "Reliable Fragmented" packet format, so it shouldn't really be a packet length issue.

    The errors happen randomly, I still didn't find a way to make them reproducible easily.

    If anyone has any idea why that happens, or if it's a Unity bug, or if I can do anything to avoid that, then please let me know. From what I see, both errors occur in or after the ReadString function. I also know that no strings with the length of 52800 are happening in my project.
     
    Last edited: Jan 14, 2016
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    These messages are symptoms of a disrupted or corrupted byte stream from the network. This is most likely caused by network packets being truncated because they exceeded some maximum packet size. It could also be bugs in the server with the combining and queuing of user-level messages into protocol level packets. It could also possibly be just too much data is being sent - but in that case there would probably be other error messages as well.

    I would suggest checking the server logs at the time this occurs, and look for any sending errors.

    I believe the transport layer network simulator is not perfectly stable and errors from it dont happen when it is not being used.

    A VPS could have different maximum packet size rules than the regular internet..? You can reduce the trasnport layer maximum packet size in the ConnectionConfig class.

    The second error is on a spawn payload. When the spawn payload for a single entity becomes larger than the maximum packet size (about 1400 bytes), then a failure will occur. To avoid this, you could make channel zero a fragmented channel.
     
  3. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Hey, thanks for the quick reply. Channel zero was 'Reliable Fragmented' in all cases. I tried to track down the error a bit more by creating a Scene with 500 monsters. Then it happened once when logging in with a client, but then never again for the rest of today.

    I will try the things that you mentioned and let you know if I find anything more, thanks again!
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    The number of monsters should not matter, as the state update for each monster can be a moved to a new protocol level packet when the current one is full. It would be the size of the state update for a single monster being too large that would trigger this..
     
  5. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Hi team, I have similar error. I check the solutions suggested by seamr:
    1 - Reduce the trasnport layer maximum packet size in the ConnectionConfig class.
    2 - Make channel zero a fragmented channel.

    But I'm not sure how/where to apply those changes. Could it be done via the NetworkManager inspector - advanced configuration (screenshot attached)?

    Thanks!
     

    Attached Files:

  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I found the reason a while ago and reported it again. Still not fixed though:
    http://forum.unity3d.com/threads/bu...ease-show-the-variable-that-caused-it.395692/

    It's also in the issue tracker as the highest voted UNET bug: https://issuetracker.unity3d.com/is...lash-readbytes-out-of-range-errors-in-clients
     
    Last edited: May 9, 2016
  7. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    Thanks vis2k for starting this post and following it! Yeah I voted for it!
    In the meantime, have you found a work around? Or maybe a way to catch the issue before it crash the app?

    Please let me know what you think.

    I'll let you know if I find something...
     
    Last edited: May 10, 2016
    mischa2k likes this.
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    A part of your code in OnSerialize/OnDeserialize is buggy, but you don't see the error. You can only try to remove one component after another until it stops.
     
  9. JoeTheGG

    JoeTheGG

    Joined:
    Aug 18, 2013
    Posts:
    42
    Hey guys, I've been experiencing this issue as well. One of the only workaround I have found, and it only works some of the time, is building the game twice. On the second build for some reason it sometimes gets things right, just don't change anything in between builds.
     
  10. samjoly

    samjoly

    Joined:
    Apr 23, 2013
    Posts:
    30
    So the issue was that I modify the "NetworkTransform.transformSyncMode" at runtime; and I believe that the networkTransform was trying to sync the client before the transformSyncMode was identical between client.
    So for testing the theory, I turn the Send Rate to 0, wait a bit, change the transformSyncMode, wait a bit, and then change back the Send Rate. It seems to work!
    My next step would be to replace those unreliable delay (wait a bit) with some kind of callback system that can confirm that every clients have been updated, before moving to the next step - (by chance do you know if there is a built in system for that kind of task?)

    Thanks again vis2k! You rock!
     
  11. Vinny-the-true

    Vinny-the-true

    Joined:
    Nov 28, 2016
    Posts:
    10
    For some reason, I need to cheat with my game objects, meaning I would like, in some precise circumstances, to disable completely the syncing performed by the NetworkTransform on the client side (the one with no authority).
    At first, I tried, just like samjoly, changing the SyncMode to SyncNone. This obviously gave the same results as exposed in previous posts...
    So, is there a simple way to disable the effect of Networktransform syncing on my object ?