Azure / azure-iot-sdk-csharp

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

Repeatedly throwing exceptions on internet reconnection #447

Closed crates-barrels closed 6 years ago

crates-barrels commented 6 years ago

Description of the issue:

I'm using the Device Management Client for Windows IoT Core and added HockeyApp to the application. When disconnecting the device from the internet and then reconnecting it back to the internet, I get a lot (~6 per minute) of these exceptions: "A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ()".

However, the application will not completely crash, just one Task. It seems to be the method that wants to close the (already closed) connection. I also posted the issue in the Device Management repo, but they redirected me this repo. You can find the issue here.

Steps to reproduce

  1. Use the DM Hello world sample code
  2. Add HockeyApp to the application by adding HockeyClient.Current.Configure("xxxxxxxxxxx"); to the App() method (details here)
  3. Run the application with internet connected
  4. Verify the device is connected by checking the connectionState = "connected" in the device twin in Azure Portal -> IoT Hub -> devices -> device twin
  5. Disconnect the device from the internet
  6. Wait for the device twin connectionState to become "disconnected"
  7. Reconnect the device to the internet
  8. HockeyApp will start receiving a lot of exceptions

Exception stack in HockeyApp of the issue:

unknown location Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.< CloseAsync>d__54.MoveNext() DotNetty.Transport.Channels.Sockets.SocketChannelAsyncOperation.Validate() DotNetty.Transport.Channels.Sockets.AbstractSocketByteChannel.SocketByteChannelUnsafe.FinishRead(SocketChannelAsyncOperation operation)

Code sample exhibiting the issue:

You can use the DM Hello world sample code

CIPop commented 6 years ago

This is a duplicate of #265.

CIPop commented 6 years ago

@crates-barrels is there an impact to the application? If you reconnect will the device eventually re-establish the IoT Hub connection?

crates-barrels commented 6 years ago

Yes @CIPop, the connection re-establishes when reconnected. This is handled by the Device Management Client:

this.deviceClient.SetConnectionStatusChangesHandler(async (ConnectionStatus status, ConnectionStatusChangeReason reason) =>
{
    string msg = "Connection changed: " + status.ToString() + " " + reason.ToString();
    System.Diagnostics.Debug.WriteLine(msg);
    logAsyncHandler?.Invoke(msg, LoggingLevel.Verbose);

    switch (reason)
    {
        case ConnectionStatusChangeReason.Connection_Ok:
            // No need to do anything, this is the expectation
            break;

        case ConnectionStatusChangeReason.Expired_SAS_Token:
        case ConnectionStatusChangeReason.Bad_Credential:
        case ConnectionStatusChangeReason.Retry_Expired:
        case ConnectionStatusChangeReason.No_Network:
            await InternalRefreshConnectionAsync();
            break;

        case ConnectionStatusChangeReason.Client_Close:
            // ignore this ... part of client shutting down.
            break;

        case ConnectionStatusChangeReason.Communication_Error:
        case ConnectionStatusChangeReason.Device_Disabled:
            // These are not implemented in the Azure SDK
            break;

        default:
            break;
    }
});

There is no real impact on the application, but it's rather annoying to have HockeyApp flooded with exceptions. Also, when the device is on a metered connection, there will be a lot of unnecessary data usage. Our devices will be operating most of the time on cellular.

CIPop commented 6 years ago

@crates-barrels the decision for #265 was to postpone the fix for a future version.

I would recommend catching and ignoring these exceptions using your own TaskScheduler.UnobservedTaskException handler before they are reported to the HockeyApp.