Describe the bug
DeviceClient SetMethodDefaultHandlerAsync handler is not triggered on internet disconnection instantly. It triggers after 15 to 20 minutes. Below are the logs
IoT Hub connection status Changed Status: Connected Reason: Connection_Ok Time: 3:09:15 PM +02 IoT Hub connection status Changed Status: Disconnected_Retrying Reason: Communication_Error Time: 3:26:29 PM +02
I disconnected the internet at 3:10:00 PM +02 and communication error was thrown after 16 minutes. I have created a sample code which reproduces the issue
Steps to reproduce
using Microsoft.Azure.Amqp.Transport;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Client.Transport.Mqtt;
using Microsoft.Azure.Devices.Shared;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace IOTClientTest
{
class Program
{
private DeviceClient _client;
static async Task Main(string[] args)
{
await new Program().RunAsync(collection =>
{
return null;
}, (new CancellationTokenSource()).Token);
Console.Read();
}
public async Task RunAsync(Func<TwinCollection, Task<TwinCollection>> twinUpdateHandler, CancellationToken cancellationToken)
{
var settings = new ITransportSettings[]
{
new MqttTransportSettings(TransportType.Mqtt_Tcp_Only)
{
KeepAliveInSeconds = 10,
}
};
_client = DeviceClient.CreateFromConnectionString(
"HostName=XXXX;SharedAccessKey=XXXXX",
"XXXX", settings);
var retryPolicy = new ExponentialBackoff(5,
TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(120),
TimeSpan.FromSeconds(5));
_client.SetRetryPolicy(retryPolicy);
_client.SetConnectionStatusChangesHandler((status, reason) =>
{
Console.WriteLine($"IoT Hub connection status Changed Status: {status} Reason: {reason} Time: {DateTime.Now.ToString("h:mm:ss tt zz")}");
});
await _client.OpenAsync();
await _client.SetMethodHandlerAsync("executeShell", async (req, context) =>
{
await Task.Delay(0);
return new MethodResponse(500);
},null);
await _client.SetMethodDefaultHandlerAsync(MethodHandler, null);
await _client.SetDesiredPropertyUpdateCallbackAsync(async (collection, context) =>
{
var updated = await twinUpdateHandler(collection);
await _client.UpdateReportedPropertiesAsync(updated);
}
, null);
await ReceiveMessagesAsync(cancellationToken);
}
private async Task<MethodResponse> MethodHandler(MethodRequest methodRequest, object parameter)
{
await Task.Delay(0);
return new MethodResponse(500);
}
private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var message = await _client.ReceiveAsync(TimeSpan.FromSeconds(10));
if (message != null)
{
//Do something with received message...
}
}
}
}
}
Expected behavior
On internet disconnection, device client should change its status in not more than 10 seconds
Actual behavior
Device client throws status change after 16 minutes
Versions used
Add following information:
dotnet --info
.NET Core SDK (reflecting any global.json): Version: 3.1.201 Commit: b1768b4ae7
Runtime Environment: OS Name: zorin OS Version: 15 OS Platform: Linux RID: linux-x64 Base Path: /usr/share/dotnet/sdk/3.1.201/
Host (useful for support): Version: 3.1.3 Commit: 4a9f85e9f8
.NET Core SDKs installed: 2.2.402 [/usr/share/dotnet/sdk] 3.1.201 [/usr/share/dotnet/sdk]
.NET Core runtimes installed: Microsoft.AspNetCore.All 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.3 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.3 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
Repository is available here: https://github.com/raza707mirza/iottest
I have reported a bug here too: https://github.com/Azure/azure-iot-sdk-csharp/issues/1409