Search Unity

Web Socket Client Disconnecting Exception Being Thrown in UWP Builds

Discussion in 'Windows' started by Th3A1chemist, Dec 5, 2018.

  1. Th3A1chemist

    Th3A1chemist

    Joined:
    Jul 30, 2015
    Posts:
    3
    I'm currently having an issue using Web Socket Client in .NET 4 in Unity in UWP builds using the IL2CPP backend. The Unity version I'm running is 2018.2.17f1. When I run my app without Visual Studio Debugging (so opening the solution in VS 2017 and hit Build > Start Debugging) it disconnects from the Web Socket Server fine and no error is thrown in the Unity logs.

    But when I build the project then open the generated solution file and click "Local Machine" or click Build > Start Debugging I get the following exception thrown in the output window: `Exception thrown at 0x00007FFAE19BDCEA (ntdll.dll) in Sensei.exe: 0xC0000008: An invalid handle was specified.` in the SocketImpl.cpp file on line 1810. But just in case it's different for other people, it's this block of code:
    Code (CPlusPlus):
    1. __try
    2. {
    3.     affected = select(0, &rfds, &wfds, &efds, timeoutPtr); // Breaks on this line
    4. }
    5. __except (SocketExceptionFilter(GetExceptionCode()))
    6. {
    7. }
    My Web Socket Client is encapsulated in a class so it's more portable in my code but the C# code for disconnecting in that class looks like this:
    Code (CSharp):
    1. public async void Disconnect(WebSocketCloseStatus status, string reason)
    2. {
    3.     await DisconnectAsync(status, reason);
    4. }
    5.  
    6. private async Task DisconnectAsync(WebSocketCloseStatus status, string reason)
    7. {
    8.     try
    9.     {
    10.         await _webSocket.CloseAsync(status, reason, CancellationToken.None).ConfigureAwait(false);
    11.     }
    12.     catch (Exception e)
    13.     {
    14.         Debug.LogWarning("Exception thrown when disconnecting to WebSocket. " + e.Message);
    15.     }
    16.     finally
    17.     {
    18.         _webSocket.Dispose();
    19.     }
    20. }
    I've been working on this for the last two days but haven't gotten a clue as to why it's doing this. Before I had implemented WebSocket-sharp but it gave me the same issue I'm having now. It broke at the same point in my code and gave the same exception. My other client (that I'm connecting to through a Web Socket Server) is on Android running IL2CPP with the same code and it doesn't seem to break. I also ran this on Unity Standalone for Windows x86_64 with the IL2CPP backend, it also doesn't seem to break. Is this the expected behaviour and that I shouldn't run Unity UWP in Visual studio or is there something I'm doing wrong here?
     
    Last edited: Dec 5, 2018
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    The exception that gets thrown is handled (as you've noticed, there's a try/catch statement). That's why it works just fine when Visual Studio is not attached. Visual Studio has a setting which causes it to break on any "thrown" exception, and you're running into it. When the exception gets thrown, you should see a checkbox that says "break on this exception type" (or something along those lines). If you uncheck it, it will not break on that exception anymore.
     
  3. Th3A1chemist

    Th3A1chemist

    Joined:
    Jul 30, 2015
    Posts:
    3
    Yeah, I had guessed that's why Visual Studio was breaking at the point. Is it an error/exception I should be worried about though?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    No, you should ignore it.
     
  5. Th3A1chemist

    Th3A1chemist

    Joined:
    Jul 30, 2015
    Posts:
    3
    Okay, thanks for the help! I really appreciate it.