Skip to content

Commit cfcc10b

Browse files
Bug fix: Direct method subscription not removed after disconnect (#3359)
* Update MqttTransportHandler.cs * Update MqttTransportHandler.cs * no need to unhook disconnect handler * Added unit test, removed ClientWasConnected check as the disconnect handler is only being triggered after disconnection * Update MqttTransportHandlerTests.cs * updated analyzer version * reverting version * updated netanalyzer
1 parent 987e0a2 commit cfcc10b

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

iothub/device/src/Microsoft.Azure.Devices.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
<PackageReference Include="Azure.Core" Version="1.31.0" />
6161
<PackageReference Include="MQTTnet" Version="4.1.4.563" />
6262
<PackageReference Include="Microsoft.Azure.Amqp" Version="2.6.2" />
63-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3">
63+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4-preview1.23354.4">
6464
<PrivateAssets>all</PrivateAssets>
6565
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
6666
</PackageReference>

iothub/device/src/Pipeline/MqttTransportHandler.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,14 +1069,13 @@ private Task HandleDisconnectionAsync(MqttClientDisconnectedEventArgs disconnect
10691069
if (Logging.IsEnabled)
10701070
Logging.Info(this, $"MQTT connection was lost {disconnectedEventArgs.Exception}", nameof(HandleDisconnectionAsync));
10711071

1072-
if (disconnectedEventArgs.ClientWasConnected)
1073-
{
1074-
OnTransportDisconnected();
1072+
OnTransportDisconnected();
10751073

1076-
// During a disconnection, any pending twin updates won't be received, so we'll preemptively
1077-
// cancel these operations so the client can retry once reconnected.
1078-
RemoveOldOperations(TimeSpan.Zero);
1079-
}
1074+
// During a disconnection, any pending twin updates won't be received, so we'll preemptively
1075+
// cancel these operations so the client can retry once reconnected.
1076+
RemoveOldOperations(TimeSpan.Zero);
1077+
1078+
_mqttClient.ApplicationMessageReceivedAsync -= HandleReceivedMessageAsync;
10801079

10811080
return Task.CompletedTask;
10821081
}

iothub/device/tests/Transport/Mqtt/MqttTransportHandlerTests.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using Microsoft.Azure.Devices.Client.Transport;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -7,6 +8,7 @@
78

89
namespace Microsoft.Azure.Devices.Client.Tests.Transport.Mqtt
910
{
11+
[TestClass]
1012
public class MqttTransportHandlerTests
1113
{
1214
[TestMethod]
@@ -24,6 +26,20 @@ public async Task MqttTransportHandler_OpenAsyncCallsConnectAsync()
2426
mockMqttClient.Verify(p => p.ConnectAsync(It.IsAny<MqttClientOptions>(), cancellationToken));
2527
}
2628

29+
[TestMethod]
30+
public async Task MqttTransportHandler_Disconnect_UnhooksMessageProcessorHandler()
31+
{
32+
var cancellationToken = new CancellationToken();
33+
var options = new MqttClientOptions();
34+
35+
var mockMqttClient = new Mock<IMqttClient>();
36+
using MqttTransportHandler mqttTransportHandler = CreateTransportHandler(mockMqttClient.Object);
37+
await mqttTransportHandler.OpenAsync(cancellationToken);
38+
mockMqttClient.VerifyAdd(p => p.ApplicationMessageReceivedAsync += It.IsAny<Func<MqttApplicationMessageReceivedEventArgs, Task>>());
39+
mockMqttClient.Raise(p => p.DisconnectedAsync += null, It.IsAny<Func<MqttClientDisconnectedEventArgs, Task>>());
40+
mockMqttClient.VerifyRemove(p => p.ApplicationMessageReceivedAsync -= It.IsAny<Func<MqttApplicationMessageReceivedEventArgs, Task>>());
41+
}
42+
2743
internal static MqttTransportHandler CreateTransportHandler(IMqttClient mockMqttClient)
2844
{
2945
var pipelineContext = new PipelineContext();

iothub/service/src/Microsoft.Azure.Devices.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
6565
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
6666
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
67-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3">
67+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4-preview1.23354.4">
6868
<PrivateAssets>all</PrivateAssets>
6969
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
7070
</PackageReference>

provisioning/device/src/Microsoft.Azure.Devices.Provisioning.Client.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<PackageReference Include="Microsoft.Azure.Amqp" Version="2.6.2" />
6666
<PackageReference Include="MQTTnet" Version="4.1.4.563" />
6767
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
68-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3">
68+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4-preview1.23354.4">
6969
<PrivateAssets>all</PrivateAssets>
7070
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
7171
</PackageReference>

provisioning/service/src/Microsoft.Azure.Devices.Provisioning.Service.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<ItemGroup>
5959
<PackageReference Include="Azure.Core" Version="1.31.0" />
6060
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
61-
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3">
61+
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4-preview1.23354.4">
6262
<PrivateAssets>all</PrivateAssets>
6363
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
6464
</PackageReference>

0 commit comments

Comments
 (0)