UWP StreamSocket ConnectAsync exception 0xC00D36FF

0

I'm getting a very strange exception using a UWP StreamSocket, 99.9% of the time this code functions as expected and the communication with the device works properly, however I get the following exception every once in a while.

The exception message:

The operation failed because an invalid combination of workqueue ID and flags was specified. (Exception from HRESULT: 0xC00D36FF)

Sample code for the issue:

using (StreamSocket analyzerSocket = new StreamSocket())
{
    HostName hostName = new HostName(host);

    // Set NoDelay to false so that the Nagle algorithm is not disabled
    analyzerSocket.Control.NoDelay = false;

    try
    {
        // Connect to the server
        await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
    }
    catch (Exception e)
    {
        var x = e;
    }
}

Screenshot of exception in code: Screenshot

The Stack Trace:

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Analyzer.<SendMessageAsync>d__120.MoveNext() in zzz.cs:line 779

I've tried my GoogleFu and I was able to find issues with MediaPlayer or Linux kernels but nothing seemed to relate to this issue with StreamSockets.

While I can trap this error and work around the issue I would like to know what's going on in case it's a symptom of a bigger issue.

Thanks in advance.

Edit 1

I thought this might be related to http://referencesource.microsoft.com/#mscorlib/system/runtime/compilerservices/TaskAwaiter.cs,be57b6bc41e5c7e4 based on the code comment of "// And throw an exception if the task is faulted or canceled.".

However I still get this expcetion when I am not using the ".AsTask(new CancellationTokenSource(timeout.Value).Token)"

Edit 2

When I have this in a loop, to constantly send messages to our device, the messages are being received until this exception occurs. Once the exception occurs and I tell it to continue and try again, the exception re-occurs over and over in the loop and the device stops receiving messages.

Edit 3

So I've tried the following, to connect to a different device, with a different instance of the StreamSocket object... and it generates the same error!

using (StreamSocket analyzerSocket = new StreamSocket())
{
    HostName hostName = new HostName(host);

    // Set NoDelay to false so that the Nagle algorithm is not disabled
    analyzerSocket.Control.NoDelay = false;

    try
    {
        // Connect to the server
        await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
    }
    catch (Exception e)
    {
        using (StreamSocket analyzerSocket2 = new StreamSocket())
        {
            HostName hostName2 = new HostName("xxx.xxx.xxx.xxx");

            // Set NoDelay to false so that the Nagle algorithm is not disabled
            analyzerSocket.Control.NoDelay = false;

            // Connect to the server
            await analyzerSocket2.ConnectAsync(hostName2, port.ToString());
        }

        throw;
    }
}

It feels like some sort of cross threading type of issue... I'm grasping at straws right now as I cannot trap and bypass the error as once the error occurs I can no longer talk to the devices and I must exit the application to get it to work again.

Does anyone have any other ideas or confirmation that it looks like cross threading type of issue?

Thanks in advance.

c#
exception
stream
uwp
asked on Stack Overflow Sep 26, 2016 by Michael Woolsey • edited Jul 15, 2019 by Claudio

1 Answer

0

The only way I have found to prevent this issue is to stop using the "StreamSocket" altogether.

I switched my code to use the "System.Net.Sockets" namespace using the Socket object instead and with some modifications to my code I haven't encountered this issue since.

Code sample:

https://msdn.microsoft.com/en-us/library/windows/apps/hh202858%28v=vs.105%29.aspx?f=255&MSPPError=-2147217396

answered on Stack Overflow Sep 29, 2016 by Michael Woolsey

User contributions licensed under CC BY-SA 3.0