Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
466 stars 493 forks source link

C# UWP device client on IoT Core issue #71

Closed helenhllo closed 7 years ago

helenhllo commented 7 years ago

I ran the UWP Sample (https://github.com/Azure/azure-iot-sdk-csharp/tree/master/device/samples/UWPSample) on the IoT Core device. I can repro the issue 100%:

  1. selecting AMQP from protocol dropdown
  2. start receiving thread (receiving message from IoT Hub)
  3. disconnecting the internet for couple of minutes (will see some caught exception in debug output panel)
    • Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll
    • Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in Microsoft.Azure.Devices.Client.UWP.dll
    • Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll
  4. then reconnect the internet and wait a few minute. There will be no messages coming in then the exception will happen. I will get an uncaught exception then the code will crash in another thread which I cannot access.

[In debug output log] Exception thrown: 'System.OperationCanceledException' in mscorlib.ni.dll Exception thrown: 'System.OperationCanceledException' in Microsoft.Azure.Devices.Client.UWP.dll Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in Microsoft.Azure.Devices.Client.UWP.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'System.AggregateException' in mscorlib.ni.dll Receive exception:Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException: Transient error occured, please retry. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.d24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.d201.MoveNext() --- End of inner exception stack trace --- at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__201.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.<ReceiveAsync>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.TaskHelpers.d171.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Samples.IoTClient.d15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.Azure.Devices.Client.Samples.MainPage.d__7.MoveNext()

After this exception, Visual Studio will stop with an exception that is outside the code (attached screenshot) exception

arturl commented 7 years ago

Fix checked in, see https://github.com/Azure/azure-iot-sdk-csharp/commit/0f8eb3f512cf5cf0cb875a2f31cd2280e128698c.

helenhllo commented 7 years ago

The issue still exists. If I catch the exception, the app won't crash but it doesn't reconnect the listener. After downloading the new code, I ran the same repro steps as above:

The same exceptions occur due to network disconnect:

Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll
Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in Microsoft.Azure.Devices.Client.UWP.dll
Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll
'uwpsample.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\UWP-IoT-a1429fd3-3896-4330-8ba8-2c762acb3cb9VS.Debug_x64.v-hello\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x6d0 has exited with code 0 (0x0).
Exception thrown: 'System.OperationCanceledException' in mscorlib.ni.dll
Exception thrown: 'System.OperationCanceledException' in Microsoft.Azure.Devices.Client.UWP.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll
Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in Microsoft.Azure.Devices.Client.UWP.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
Exception thrown: 'System.AggregateException' in mscorlib.ni.dll
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll
The thread 0x910 has exited with code 0 (0x0).
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll

Then the Visual Studio will show uncaught exception: image Exception detail:

Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException was unhandled by user code
  HResult=-2146233088
  IsTransient=true
  Message=Transient error occured, please retry.
  Source=Microsoft.Azure.Devices.Client.UWP
  TrackingId=""
  StackTrace:
       at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__20`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.<ReceiveAsync>d__6.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.Azure.Devices.Client.TaskHelpers.<WithTimeout>d__17`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.Azure.Devices.Client.Samples.IoTClient.<ReceiveCommand>d__15.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.Azure.Devices.Client.Samples.MainPage.<receiveButton_Click>d__7.MoveNext()
  InnerException: 
       HResult=-2146233029
       Message=A task was canceled.
       Source=mscorlib
       StackTrace:
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.<ReceiveAsync>d__24.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
            at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__20`1.MoveNext()
       InnerException: 

Which will go all the way up toreceiveButton_Click(object sender, RoutedEventArgs e)in MainPage.cs in Microsoft.Azure.Devices.Client.Sample.MainPage. If I catch exception fromawait this.client.ReceiveCommand(); the app doesn't crash but the receiving thread stopped working (no messages coming in).

``

arturl commented 7 years ago

The sample doesn't reconnect automatically when an exception happens during receive. Try catching the exception in the code, then switch the protocol (for example, to http and back tp mqtt). That should reset the DeviceClient.

Artur

From: helenlo319 Sent: Tuesday, March 14, 19:58 Subject: Re: [Azure/azure-iot-sdk-csharp] C# UWP device client on IoT Core issue (#71) To: Azure/azure-iot-sdk-csharp Cc: Artur Laksberg, Assign

The issue still exists. If I catch the exception, the app won't crash but it doesn't reconnect the listener. After downloading the new code, I ran the same repro steps as above:

The same exceptions occur due to network disconnect:

Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in Microsoft.Azure.Devices.Client.UWP.dll Exception thrown: 'DotNetty.Transport.Channels.ClosedChannelException' in mscorlib.ni.dll 'uwpsample.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\UWP-IoT-a1429fd3-3896-4330-8ba8-2c762acb3cb9VS.Debug_x64.v-hello\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The thread 0x6d0 has exited with code 0 (0x0). Exception thrown: 'System.OperationCanceledException' in mscorlib.ni.dll Exception thrown: 'System.OperationCanceledException' in Microsoft.Azure.Devices.Client.UWP.dll Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll Exception thrown: 'System.Threading.Tasks.TaskCanceledException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in Microsoft.Azure.Devices.Client.UWP.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll Exception thrown: 'System.AggregateException' in mscorlib.ni.dll Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll The thread 0x910 has exited with code 0 (0x0). Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException' in mscorlib.ni.dll

Then the Visual Studio will show uncaught exception:

Exception detail:

Microsoft.Azure.Devices.Client.Exceptions.IotHubClientTransientException was unhandled by user code HResult=-2146233088 IsTransient=true Message=Transient error occured, please retry. Source=Microsoft.Azure.Devices.Client.UWP TrackingId="" StackTrace: at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.d201.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.d6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.TaskHelpers.<WithTimeout>d__171.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Samples.IoTClient.<ReceiveCommand>d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Samples.MainPage.d7.MoveNext() InnerException: HResult=-2146233029 Message=A task was canceled. Source=mscorlib StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.d24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__201.MoveNext() InnerException:

Which will go all the way up toreceiveButton_Click(object sender, RoutedEventArgs e)in MainPage.cs in Microsoft.Azure.Devices.Client.Sample.MainPage. If I catch exception fromawait this.client.ReceiveCommand(); the app doesn't crash but the receiving thread stopped working (no messages coming in).

``

— You are receiving this because you were assigned. Reply to this email directly, view https://github.com/Azure/azure-iot-sdk-csharp/issues/71#issuecomment-286626753 ithttps://github.com/Azure/azure-iot-sdk-csharp/issues/71#issuecomment-286626753 on GitHubhttps://github.com/Azure/azure-iot-sdk-csharp/issues/71#issuecomment-286626753, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AByfFi9OZxpFiq6Kl4AVAVLcttojAzfNks5rl1PAgaJpZM4MXnoC.