From 261d39f32e81f1e21bf37a013e90d1e8df22bd57 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Mon, 7 Aug 2023 15:15:19 -0700 Subject: [PATCH 1/8] Update MqttTransportHandler.cs --- iothub/device/src/Pipeline/MqttTransportHandler.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/iothub/device/src/Pipeline/MqttTransportHandler.cs b/iothub/device/src/Pipeline/MqttTransportHandler.cs index e2eae3dcb4..c855700642 100644 --- a/iothub/device/src/Pipeline/MqttTransportHandler.cs +++ b/iothub/device/src/Pipeline/MqttTransportHandler.cs @@ -1389,6 +1389,9 @@ private void RemoveOldOperations(object state) }) .Count(); + _mqttClient.DisconnectedAsync -= HandleDisconnectionAsync; + _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; + if (Logging.IsEnabled) Logging.Error(this, $"Removed {canceledOperations} twin responses", nameof(RemoveOldOperations)); } From d97829ffbb4a7f5806433ba726a3ed92f477e4af Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Tue, 8 Aug 2023 10:19:16 -0700 Subject: [PATCH 2/8] Update MqttTransportHandler.cs --- iothub/device/src/Pipeline/MqttTransportHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iothub/device/src/Pipeline/MqttTransportHandler.cs b/iothub/device/src/Pipeline/MqttTransportHandler.cs index c855700642..f6ff2972a3 100644 --- a/iothub/device/src/Pipeline/MqttTransportHandler.cs +++ b/iothub/device/src/Pipeline/MqttTransportHandler.cs @@ -1076,6 +1076,9 @@ private Task HandleDisconnectionAsync(MqttClientDisconnectedEventArgs disconnect // During a disconnection, any pending twin updates won't be received, so we'll preemptively // cancel these operations so the client can retry once reconnected. RemoveOldOperations(TimeSpan.Zero); + + _mqttClient.DisconnectedAsync -= HandleDisconnectionAsync; + _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; } return Task.CompletedTask; @@ -1389,9 +1392,6 @@ private void RemoveOldOperations(object state) }) .Count(); - _mqttClient.DisconnectedAsync -= HandleDisconnectionAsync; - _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; - if (Logging.IsEnabled) Logging.Error(this, $"Removed {canceledOperations} twin responses", nameof(RemoveOldOperations)); } From c410abef3f3a7e1c6905a6b60efe2760a369bbba Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Tue, 8 Aug 2023 11:00:00 -0700 Subject: [PATCH 3/8] no need to unhook disconnect handler --- iothub/device/src/Pipeline/MqttTransportHandler.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/iothub/device/src/Pipeline/MqttTransportHandler.cs b/iothub/device/src/Pipeline/MqttTransportHandler.cs index f6ff2972a3..d8fc5d240a 100644 --- a/iothub/device/src/Pipeline/MqttTransportHandler.cs +++ b/iothub/device/src/Pipeline/MqttTransportHandler.cs @@ -1077,7 +1077,6 @@ private Task HandleDisconnectionAsync(MqttClientDisconnectedEventArgs disconnect // cancel these operations so the client can retry once reconnected. RemoveOldOperations(TimeSpan.Zero); - _mqttClient.DisconnectedAsync -= HandleDisconnectionAsync; _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; } From 7438ce7112456e9584028eaaf12dd45640b45421 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 9 Aug 2023 11:52:17 -0700 Subject: [PATCH 4/8] Added unit test, removed ClientWasConnected check as the disconnect handler is only being triggered after disconnection --- .../src/Pipeline/MqttTransportHandler.cs | 13 +++++-------- .../Mqtt/MqttTransportHandlerTests.cs | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/iothub/device/src/Pipeline/MqttTransportHandler.cs b/iothub/device/src/Pipeline/MqttTransportHandler.cs index d8fc5d240a..eda39dea6f 100644 --- a/iothub/device/src/Pipeline/MqttTransportHandler.cs +++ b/iothub/device/src/Pipeline/MqttTransportHandler.cs @@ -1069,16 +1069,13 @@ private Task HandleDisconnectionAsync(MqttClientDisconnectedEventArgs disconnect if (Logging.IsEnabled) Logging.Info(this, $"MQTT connection was lost {disconnectedEventArgs.Exception}", nameof(HandleDisconnectionAsync)); - if (disconnectedEventArgs.ClientWasConnected) - { - OnTransportDisconnected(); + OnTransportDisconnected(); - // During a disconnection, any pending twin updates won't be received, so we'll preemptively - // cancel these operations so the client can retry once reconnected. - RemoveOldOperations(TimeSpan.Zero); + // During a disconnection, any pending twin updates won't be received, so we'll preemptively + // cancel these operations so the client can retry once reconnected. + RemoveOldOperations(TimeSpan.Zero); - _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; - } + _mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync; return Task.CompletedTask; } diff --git a/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs b/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs index aba78eb678..485eca2e72 100644 --- a/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs +++ b/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System; +using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Devices.Client.Transport; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -7,6 +8,8 @@ namespace Microsoft.Azure.Devices.Client.Tests.Transport.Mqtt { + [TestClass] + [TestCategory("Unit")] public class MqttTransportHandlerTests { [TestMethod] @@ -24,6 +27,20 @@ public async Task MqttTransportHandler_OpenAsyncCallsConnectAsync() mockMqttClient.Verify(p => p.ConnectAsync(It.IsAny(), cancellationToken)); } + [TestMethod] + public async Task mqtt() + { + var cancellationToken = new CancellationToken(); + var options = new MqttClientOptions(); + + var mockMqttClient = new Mock(); + using MqttTransportHandler mqttTransportHandler = CreateTransportHandler(mockMqttClient.Object); + await mqttTransportHandler.OpenAsync(cancellationToken); + mockMqttClient.VerifyAdd(p => p.ApplicationMessageReceivedAsync += It.IsAny>()); + mockMqttClient.Raise(p => p.DisconnectedAsync += null, It.IsAny>()); + mockMqttClient.VerifyRemove(p => p.ApplicationMessageReceivedAsync -= It.IsAny>()); + } + internal static MqttTransportHandler CreateTransportHandler(IMqttClient mockMqttClient) { var pipelineContext = new PipelineContext(); From 94925ed2e23a022bce0801bd1c4fb7ac0584ab12 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 9 Aug 2023 13:47:52 -0700 Subject: [PATCH 5/8] Update MqttTransportHandlerTests.cs --- .../device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs b/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs index 485eca2e72..f0ac3e7d56 100644 --- a/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs +++ b/iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs @@ -9,7 +9,6 @@ namespace Microsoft.Azure.Devices.Client.Tests.Transport.Mqtt { [TestClass] - [TestCategory("Unit")] public class MqttTransportHandlerTests { [TestMethod] @@ -28,7 +27,7 @@ public async Task MqttTransportHandler_OpenAsyncCallsConnectAsync() } [TestMethod] - public async Task mqtt() + public async Task MqttTransportHandler_Disconnect_UnhooksMessageProcessorHandler() { var cancellationToken = new CancellationToken(); var options = new MqttClientOptions(); From 7bc8e68d5ab01551f2f678bd4f35df3f9c319681 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 9 Aug 2023 13:59:29 -0700 Subject: [PATCH 6/8] updated analyzer version --- iothub/device/src/Microsoft.Azure.Devices.Client.csproj | 2 +- iothub/service/src/Microsoft.Azure.Devices.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Client.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Service.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj index 9fc91bc3b6..93ab1341dc 100644 --- a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj +++ b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj @@ -60,7 +60,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/iothub/service/src/Microsoft.Azure.Devices.csproj b/iothub/service/src/Microsoft.Azure.Devices.csproj index fe4e87f71f..01e83a91e3 100644 --- a/iothub/service/src/Microsoft.Azure.Devices.csproj +++ b/iothub/service/src/Microsoft.Azure.Devices.csproj @@ -64,7 +64,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj index a55b720e59..1de58eb338 100644 --- a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj +++ b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj @@ -65,7 +65,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj index fb6720e8ce..93cebe8fcb 100644 --- a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj +++ b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj @@ -58,7 +58,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 1e957c01d332ad2ec09f8fd51f269a66eb366e85 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 9 Aug 2023 14:08:40 -0700 Subject: [PATCH 7/8] reverting version --- iothub/device/src/Microsoft.Azure.Devices.Client.csproj | 2 +- iothub/service/src/Microsoft.Azure.Devices.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Client.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Service.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj index 93ab1341dc..9fc91bc3b6 100644 --- a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj +++ b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj @@ -60,7 +60,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/iothub/service/src/Microsoft.Azure.Devices.csproj b/iothub/service/src/Microsoft.Azure.Devices.csproj index 01e83a91e3..fe4e87f71f 100644 --- a/iothub/service/src/Microsoft.Azure.Devices.csproj +++ b/iothub/service/src/Microsoft.Azure.Devices.csproj @@ -64,7 +64,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj index 1de58eb338..a55b720e59 100644 --- a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj +++ b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj @@ -65,7 +65,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj index 93cebe8fcb..fb6720e8ce 100644 --- a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj +++ b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj @@ -58,7 +58,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 25b3832d5716e08f48d3096f3302d145065590f9 Mon Sep 17 00:00:00 2001 From: tmahmood-microsoft Date: Wed, 9 Aug 2023 14:19:08 -0700 Subject: [PATCH 8/8] updated netanalyzer --- iothub/device/src/Microsoft.Azure.Devices.Client.csproj | 2 +- iothub/service/src/Microsoft.Azure.Devices.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Client.csproj | 2 +- .../src/Microsoft.Azure.Devices.Provisioning.Service.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj index 9fc91bc3b6..8aafd7ffa6 100644 --- a/iothub/device/src/Microsoft.Azure.Devices.Client.csproj +++ b/iothub/device/src/Microsoft.Azure.Devices.Client.csproj @@ -60,7 +60,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/iothub/service/src/Microsoft.Azure.Devices.csproj b/iothub/service/src/Microsoft.Azure.Devices.csproj index fe4e87f71f..33bc5f623c 100644 --- a/iothub/service/src/Microsoft.Azure.Devices.csproj +++ b/iothub/service/src/Microsoft.Azure.Devices.csproj @@ -64,7 +64,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj index a55b720e59..5a2f0dfc5b 100644 --- a/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj +++ b/provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj @@ -65,7 +65,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj index fb6720e8ce..e2449e65ec 100644 --- a/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj +++ b/provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj @@ -58,7 +58,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive