Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    5,801
    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:
    5,801
    No, you should ignore it.
     
  5. Th3A1chemist

    Th3A1chemist

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