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

Any way to tap into OnDisconnectError?

Discussion in 'Multiplayer' started by shadiradio, Sep 16, 2016.

  1. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    This is in NetworkServerSimple. If you have two game instances connected to each other, and quit one of them, you have no way of handling this error (if using NetworkManager or NetworkServer, which in turn use NetworkServerSimple under the hood). Looking at the source, is there a reason why there are no handlers for these first three methods?

    Basically, if a player quits their game, this generates the OnDisconnectError below instead of a timeout error, so how can you handle this on the other side? Regular, intentional, disconnections can be handled just fine.

    Code (CSharp):
    1. // --------------------------- virtuals ---------------------------------------
    2.  
    3.         public virtual void OnConnectError(int connectionId, byte error)
    4.         {
    5.             Debug.LogError("OnConnectError error:" + error);
    6.         }
    7.  
    8.         public virtual void OnDataError(NetworkConnection conn, byte error)
    9.         {
    10.             Debug.LogError("OnDataError error:" + error);
    11.         }
    12.  
    13.         public virtual void OnDisconnectError(NetworkConnection conn, byte error)
    14.         {
    15.             Debug.LogError("OnDisconnectError error:" + error);
    16.         }
    17.  
    18.         public virtual void OnConnected(NetworkConnection conn)
    19.         {
    20.             conn.InvokeHandlerNoData(MsgType.Connect);
    21.         }
    22.  
    23.         public virtual void OnDisconnected(NetworkConnection conn)
    24.         {
    25.             conn.InvokeHandlerNoData(MsgType.Disconnect);
    26.         }
    27.  
    28.         public virtual void OnData(NetworkConnection conn, int receivedSize, int channelId)
    29.         {
    30.             conn.TransportRecieve(m_MsgBuffer, receivedSize, channelId);
    31.         }
     
  2. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    After more testing it seems that if you terminate a game, remote clients do get a timeout error (and can handle disconnects that way), but a client disconnecting from the host doesn't generate a timeout error for the host (you get the un-handleable OnDisconnectError above).

    Does anyone know if this is a bug? Surely almost everyone out there working on a network game is trying to gracefully handle sudden disconnects?
     
  3. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,256
    I have noticed this on the patch notes for 5.4.1p1:
    Not sure if it has something to do with what you are looking into though.
     
  4. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Yup, it's definitely from that release onwards (I'm using the latest 5.5 beta). The thing is, it seems to be generated on the client when a host times out, but not the other way around, and there's no way to intercept/handle it in that direction. Also, the source code for NetworkServerSimple.cs seems to imply that this shouldn't happen on timeout (but in other error cases), making that patch note more confusing. :(

    Code (CSharp):
    1.  
    2. // ...
    3. if ((NetworkError)error != NetworkError.Timeout)
    4. {
    5.     m_Connections[connectionId] = null;
    6.     if (LogFilter.logError) { Debug.LogError("Server client disconnect error:" + connectionId); }
    7.  
    8.     //NOTE: timeout should not generate a disconnect error
    9.     OnDisconnectError(conn, error);
    10.     return;
    11. }
    12. //...
    13.  
     
  5. diego-vieira

    diego-vieira

    Joined:
    Mar 27, 2015
    Posts:
    32
    Not sure if it is a bug, but I am facing the same "problem". Before the patch it was working fine. Now, when players quits the game, i get this same error and none of my override functions is fired on NetworkManager.
    If some knows a workaround or a solution, please let me know.
     
    shadiradio likes this.
  6. lumacode

    lumacode

    Joined:
    Mar 25, 2014
    Posts:
    8
    Argh! Just came across this thing too. Annoyingly 5.4.1p1 fixes a different UNET showstopper, but then introduces this one!

    Has anyone submitted a bug report on this, otherwise i'll add one.
     
  7. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,117
    Guys , this is a bug. I submitted one before for all Disconnects and that got fixed, 5.3ish times. This should not happen. Make a small reproducible and send it with the bug.
     
  8. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    There haven't been any networking fixes in the last few beta releases. I hope the next one will have a bunch.
     
  9. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    I can confirm the bug! Installed the 5.4.1p3 and getting errors. Probably all started in patch 1.
    Going to reinstall without patches to see what happens...
     
  10. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    I would really love to release the first online-enabled version of my game to my Kickstarter backers and alpha customers, but this is one issue that is holding me back at the moment. Anyone hosting can't be notified of a client that quit, aside from a downward spiral of Unity errors and network failures from that point on. :(
     
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Encountered this in uMMORPG too. Reported it as #838689.

    I created a test scene with just a NetworkManager and a player prefab for the bug report, it happens without any scripts or any fancy features.
     
    Last edited: Oct 6, 2016
    shadiradio likes this.
  12. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    From the advice of another Unet developer, I tried tapping into Application.logMessageReceived to capture the Debug error logs from NetworkServerSimple to catch this case (really hacky), but the log handler doesn't appear to be called for any Debug messages. Is this another 5.5 bug?
     
  13. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    We can literally see right where the problem is in the code on Github, but have no control over a fix. :(
     
  14. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Mind sharing the link?
     
  15. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Oh it's the same code I showed in the first post. NetworkServerSimple (used by NetworkServer) simply Debug.Logs an error instead of running it through a handler that you can catch. Here's all the source... but I don't know if it's the latest or not. I made a mistake, it's on Bitbucket:

    https://bitbucket.org/Unity-Technol...ab4e4c0f2a3d74bb3d005641e11201c7e6f3/Runtime/
     
    mischa2k likes this.
  16. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    Any time I disconnect a client the server dumps a
    Code (CSharp):
    1. Server client disconnect error:1
    followed by an endless stream of
    Code (CSharp):
    1. Send Error: WrongConnection channel:1 bytesToSend:1390
    2. ChannelBuffer SendBytes no space on unreliable channel 1
    3. SendBytesToReady failed for...
    4. Failed to send internal buffer channel:1 bytesToSend:1395
    and the client object never gets destroyed. Is this the same error you are all encountering? This does not happen in 5.4.0.
     
  17. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Yup, this is the new error, preventing a clean way of handling the case where the client disconnects. Another developer pointed me in a direction that lets you workaround this, though it's a pretty hacky bandaid. Basically, you can register a callback handler to listen for Debug.Log messages:

    Code (CSharp):
    1. Application.logMessageReceived += HandleLog;
    Then in your handler, you can catch that specific Unet error log and handle the disconnect:

    Code (CSharp):
    1. void HandleLog(string logString, string stackTrace, LogType type)
    2.     {
    3.         if (type != LogType.Error)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         string[] errorParts = logString.Split(':');
    9.         switch (errorParts[0])
    10.         {
    11.             case "Server client disconnect error, connectionId":
    12.                 // handle stuff
    13.                 break;
    14.             case "OnDisconnectError error":
    15.                 // handle stuff
    16.                 break;
    17.         }
    18.     }
    This is a pretty bad way to handle this, and I still don't know why this *super* common case is not passed through a proper handler in 5.5.
     
    slakdevelopacc likes this.
  18. Meltor

    Meltor

    Joined:
    Nov 4, 2014
    Posts:
    1
    Just checked it with todays 5.4.2f1 - it's still broken.

    @shadiradio: Thank you for the "hacky" workaround :D

    In my current app it's a common situation that server and/or client get disconnected from the network. The last version with functional disconnect behaviour seems to be 5.4.1f1.
     
  19. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Thanks for sharing.
     
  20. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    I found that this error only seems to occur on when the application quits without a clean networking disconnect. I added the following to my player script and the errors went away on the server:
    Code (CSharp):
    1. void OnApplicationQuit(){
    2.     if(isLocalPlayer) {
    3.         NetworkManager.singleton.StopClient ();
    4.     }
    5. }
    I also submitted a bug report (with example) so hopefully they'll get it up on the issue tracker shortly.
     
    Last edited: Oct 15, 2016
    JillHoff and shadiradio like this.
  21. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Thanks, that's good information. This probably wouldn't get caught if the client crashes though, right?
     
  22. darthtelle

    darthtelle

    Joined:
    Jan 7, 2014
    Posts:
    114
    I'm getting the same error ("Server client disconnect error" / "OnDisconnectError") and I'm not even using NetworkServerSimple. Unfortunately, I noticed the Unet source on bitbucket was last updated in August so if they've updated NetworkServer (which I am using) with a similar fail check I can't compare! My current version of Unity is 5.4.1p4 (I can't update to a newer version because of the platforms we're on), so I'm rolling back to 5.4.0.
     
  23. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    Can confirm this happened on my side too after moving from 5.4.1f1 to 5.4.2f1, so I submitted a bug report with a minimal NetworkServerSimple project showcasing the problem.
     
  24. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Yup, NetworkServer now wraps NetworkServerSimple, bugs and all.
     
  25. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    Correct =/
     
  26. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    @aabramychev do you know the status of this issue? I am getting the noted log spam when a client disconnects unexpectedly now that I've upgraded to 5.4.1p4.
     
  27. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    l3fty, mischa2k and Meltor like this.
  28. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    Voted, unfortunately it doesn't let me use my whole vote quota on this :p
     
  29. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @l3fty What do you mean?
    this one -- This is in NetworkServerSimple. If you have two game instances connected to each other, and quit one of them, you have no way of handling this error (if using NetworkManager or NetworkServer, which in turn use NetworkServerSimple under the hood). Looking at the source, is there a reason why there are no handlers for these first three methods?

    or this one --- https://issuetracker.unity3d.com/is...s-clients-player-and-disconnect-error-appears

    The first afaik one in work now, but the second looks quite fresh?
     
  30. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    @aabramychev Hi, sorry I thought they were related due to the OnDisconnectError being thrown (which was how I found this thread).

    But yes I am experiencing closer to the second issue. I am running two Unity editors (5.4.1p4); one server one client. When I stop the client I receive the following errors on the server:



    The disconnected player object remains on the server, and it spams about failing to send network data to the client.
     
  31. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    This is the exact bug I reported for 5.4.1+ and the response directed me to the above linked issue (even though the tracker lists this as a problem with 5.5.x).
     
  32. MichalBUnity

    MichalBUnity

    Unity Technologies

    Joined:
    May 9, 2016
    Posts:
    18
    There is a fix for this issue on its way out to 5.4 and 5.5. It fixes this issue among others. I'll update you when i have more information, i'm sorry for any inconvenience it may caused.
     
    Royall, GixG17, Meltor and 4 others like this.
  33. shadiradio

    shadiradio

    Joined:
    Jun 22, 2013
    Posts:
    83
    Thank you @MichalBUnity, that's good news! :)
     
  34. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    Will it be released as a 5.4.2 patch release first? Any news so far?
     
  35. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    86
    No network fixes in the latest patch release D:

    Edit, oh it's marked as fixed in 5.4.3 in the issue tracker, shouldn't be too long then :)
     
  36. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    Patch 5.4.2p3 fixed the error bug! :):cool:
     
    MichalBUnity likes this.
  37. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    Unity 5.4.3, which fixes this bug, is out now.
     
  38. zaraziq

    zaraziq

    Joined:
    May 22, 2021
    Posts:
    1
    Thank you very much for having this forum, I was helped