Devices reconnect to Azure IoT Hub every 65 minutes

Viewed 1655

We have devices connected to IoT Hub via MQTT (default settings) and they all keep reconnecting precisely every 65 minutes (to the millisecond).

Device app details:

  • .net Core 2.1
  • Libraries:
    • Microsoft.Azure.Devices v1.17.0
    • Microsoft.Azure.Devices.Client v1.18.0
  • Connection Type: MQTT
  • OS: Windows 10 IoT Enterprise (Dell Industrial PC), Raspbian (Raspberry PI 3) and Windows IoT Core (Raspberry PI 3) - All behave the same.

We are using the free version/tier of IoT Hub.

When the reconnection occurs:

  • DeviceClient StatusChangesHandler reports ConnectionStatus: Disconnected_Retrying and Reason: No_Network
  • IoT Hub Diagnostics reports
    1. deviceDisconnect => 404104 DeviceConnectionClosedRemotely
    2. deviceConnect => 401003 IoTHubUnauthorized
    3. deviceConnect => No error, successful
  • DeviceClient StatusChangesHandler reports ConnectionStatus: Connected and Reason: Connection_Ok

This is not a highly critical issue for our operation since at this stage it is a PoC and each reconnect takes about 2 seconds but, we'd like to know the root cause of this behaviour before we go live.

2 Answers

Are you using SAS Token to authenticate with IoT Hub? If so, you are most likely encounter a disconnect when your token is refreshed. This is handled by the SDK and SDK uses retry policy to reconnect in this case. If the retry policy is not set explicitly, it is using the default (exponential backoff with 4 minutes timeout). If you set the default to no retry, the connection cannot be re-established without interference.

Reliability features in the SDKs

Security tokens in IoT Hub

DeviceClient.RetryPolicy = RetryPolicyType.No_Retry has been deprecated in Microsoft.Azure.Devices.Client v1.18.0. Please try to use Microsoft.Azure.Devices.Client.SetRetryPolicy(IRetryPolicy retryPolicy) instead, like following code.

deviceClient.SetRetryPolicy(new NoRetry());
Related