3

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.

erichste
  • 759
  • 4
  • 16
  • What retry policy did you set, a custom retry policy or default retry policy(ExponentialBackoff)? In addition, how did you cause the disconnection? – Michael Xu Aug 28 '18 at 03:11
  • @MichaelXu-MSFT, the retry policy is set to no_retry - `DeviceClient.RetryPolicy = RetryPolicyType.No_Retry` but, it is retrying so, that's a strange behavior by itself. - The cause of reconnection is what I am investigating, so, I don't know it. - Also, to clarify, I don't have any custom logic to trigger a reconnect when the disconnect occurs – erichste Aug 28 '18 at 06:11

2 Answers2

3

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

Yi Zhong - MSFT
  • 306
  • 2
  • 7
  • Yi Zhong, thanks for your answer. I read the articles you suggested and looked into the source code of the c# device client sdk. All implementations of IAuthenticationMethod have a token expiry date of 1 hour and give us no control over the expiry / TTL. As you pointed out all reconnects seem to be originating from the refresh logic indeed. The only way I could have more control on the refresh logic and expiry / TTL was to use DeviceAuthenticationWithToken by generating SAS tokens with the SharedAccessSignatureBuilder class using my own TTL timespan. I'll accept your response as an answer. – erichste Sep 03 '18 at 16:37
2

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());
Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • The retry policy is not set explicitly, we are using the default which is No_Retry. What do you think the relevance of retry policy setting is with regards to the reconnects that happen every 65 minutes? – erichste Aug 28 '18 at 08:00
  • I mean that the method `DeviceClient.RetryPolicy = RetryPolicyType.No_Retry` is obsolete. Even though you set the retry policy with this method,it is no effort for actual running, there was also a retry policy created by default. If you use SetRetryPolicy method with NoRetry, you will find that the app will not retry to connect the IoT Hub when disconnected. About keep reconnecting precisely every 65 minutes, you should check what caused the disconnection in this frequency, such as network block. – Michael Xu Aug 28 '18 at 09:55