Search Unity

Sockets, multithreading, and not seeing Exceptions

Discussion in 'Connected Games' started by robscherer123, Sep 6, 2019.

  1. robscherer123

    robscherer123

    Joined:
    Jan 30, 2019
    Posts:
    13
    Hello folks, I'm working on writing a multiplayer api to communicate with my game server. I'm noticing an issue with exceptions not being displayed in Unity when inside an AsyncCallback function.

    So for example the below error will not be displayed in Unity at all, so I'll have a tough time knowing something went wrong.

    Code (CSharp):
    1.  
    2. Socket clientSocket = new Socket(......);
    3. clientSocket.BeginConnect(IPAddress.Loopback, 8184, onConnected, clientSocket);
    4.  
    5. void onConnected(IAsyncResult _result){
    6.      print("Can you see me?"); //Will be displayed.
    7.      throw new Exception("testing"); //Will NOT be displayed in Unity.
    8. }
    9.  
    Any idea why this is, or how I can fix it? I'm not super familiar with multithreading in Unity just yet. I've used it in .Net applications a bit, but not in Unity. From what I read, Unity is not multithread friendly in some aspects. Could this have to do why I'm not seeing the errors? Additionally, is it safe to manipulate game objects inside of ASyncCallbacks for messages from the server? Or do I need to go about that a different way?
     
  2. robscherer123

    robscherer123

    Joined:
    Jan 30, 2019
    Posts:
    13
    So after some digging, it appears that exceptions just aren't displayed when raised from threads that aren't the main thread. I guess that's just how it is. Looks like I have to wrap more sensitive code in a try/catch and manually print to the console window. A little ugly, but I guess it'll do.

    My same question still stands regarding modifying game objects in the asynccallback for incoming socket data. I imagine that is not thread safe then? If so, what would be the best approach? To queue the data in some thread safe collection and execute the message in an Update function?
     
  3. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,245
    You could wrap your whole thread function try/catch(Exception) to catch anything that might happen in there. Then use Debug.LogError to log it.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,340
    I thought such errors getting caught in other threads was added to Unity in 2018.3. Maybe I read the release notes wrong.
     
  5. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,245
    I wasn't aware of that. Perhaps they do get caught now.
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,140
    AppDomain.CurrentDomain.UnhandledException += (sender, e) => Debug.LogError(e.ExceptionObject);
     
    tobiass likes this.
  7. robscherer123

    robscherer123

    Joined:
    Jan 30, 2019
    Posts:
    13
    Thanks for the suggestion. I currently wrapped them in try/catch, I'll have to give the AppDomain suggestion a try and see if it works.