Skip to content

Commit 087d7b5

Browse files
authored
Improving dispose in DeviceClient and ProvisioningDeviceClient (#304)
1 parent 41d021b commit 087d7b5

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

nanoFramework.Azure.Devices.Client/Azure.Devices.Client.nfproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,12 @@
6969
</Reference>
7070
<Reference Include="nanoFramework.Json, Version=2.2.101.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
7171
<HintPath>..\packages\nanoFramework.Json.2.2.101\lib\nanoFramework.Json.dll</HintPath>
72-
<Private>True</Private>
7372
</Reference>
7473
<Reference Include="nanoFramework.M2Mqtt, Version=5.1.96.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
7574
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.dll</HintPath>
76-
<Private>True</Private>
7775
</Reference>
7876
<Reference Include="nanoFramework.M2Mqtt.Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
7977
<HintPath>..\packages\nanoFramework.M2Mqtt.5.1.96\lib\nanoFramework.M2Mqtt.Core.dll</HintPath>
80-
<Private>True</Private>
8178
</Reference>
8279
<Reference Include="nanoFramework.Runtime.Events">
8380
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.6\lib\nanoFramework.Runtime.Events.dll</HintPath>

nanoFramework.Azure.Devices.Client/DeviceClient.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public class DeviceClient : IDisposable
5050
private readonly object _lock = new object();
5151
private Timer _timerTokenRenew;
5252
private bool _hasClientCertificate;
53+
private bool _isDisposed = false;
54+
private bool _isClosed = true;
5355

5456
/// <summary>
5557
/// Device twin updated event.
@@ -359,6 +361,7 @@ public bool Open()
359361
_timerTokenRenew = new Timer(TimerCallbackReconnect, null, new TimeSpan(23, 50, 0), TimeSpan.MaxValue);
360362
}
361363

364+
_isClosed = !_mqttc.IsConnected;
362365
return _mqttc.IsConnected;
363366
}
364367

@@ -382,6 +385,11 @@ private void TimerCallbackReconnect(object state)
382385
/// </summary>
383386
public void Close()
384387
{
388+
if (_isClosed)
389+
{
390+
return;
391+
}
392+
385393
if (_mqttc != null)
386394
{
387395
if (_mqttc.IsConnected)
@@ -394,13 +402,21 @@ public void Close()
394402
}
395403

396404
_mqttc.Disconnect();
397-
_mqttc.Close();
405+
try
406+
{
407+
_mqttc.Close();
408+
}
409+
catch
410+
{
411+
// Nothing on purpose, we want to make sure we can continue
412+
}
398413

399414
// Make sure all get disconnected, cleared
400415
Thread.Sleep(1000);
401416
}
402417

403418
_timerTokenRenew?.Dispose();
419+
_isClosed = true;
404420
}
405421

406422
/// <summary>
@@ -804,6 +820,12 @@ private void ClientConnectionClosed(object sender, EventArgs e)
804820
/// <inheritdoc/>
805821
public void Dispose()
806822
{
823+
if (_isDisposed)
824+
{
825+
return;
826+
}
827+
828+
_isDisposed = true;
807829
if (_mqttc != null)
808830
{
809831
// Making sure we unregister the registered events
@@ -815,7 +837,6 @@ public void Dispose()
815837
while (_mqttc.IsConnected)
816838
{
817839
Thread.Sleep(100);
818-
819840
}
820841

821842
// Cleaning

nanoFramework.Azure.Devices.Client/ProvisioningDeviceClient.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ProvisioningDeviceClient : IDisposable
3434
private ProvisioningRegistrationStatusType _status = ProvisioningRegistrationStatusType.Unassigned;
3535
private DeviceRegistrationResult _result = null;
3636
private bool _isMessageProcessed = false;
37+
private bool _isDisposed = false;
3738

3839
/// <summary>
3940
/// Creates an instance of the Device Provisioning Client.
@@ -228,9 +229,17 @@ private void CleanAll()
228229
{
229230
// We need to clean everything now
230231
_mqttc.MqttMsgPublishReceived -= ClientMqttMsgReceived;
231-
_mqttc.Unsubscribe(new[] {
232+
try
233+
{
234+
_mqttc.Unsubscribe(new[] {
232235
DpsSubscription
233236
});
237+
}
238+
catch
239+
{
240+
// Nothing on purpose, just cleaning
241+
}
242+
234243
_mqttc.Disconnect();
235244
while (_mqttc.IsConnected)
236245
{
@@ -337,6 +346,12 @@ private ProvisioningRegistrationStatusType AssignStatus(string status)
337346
/// <inheritdoc/>
338347
public void Dispose()
339348
{
349+
if(_isDisposed)
350+
{
351+
return;
352+
}
353+
354+
_isDisposed = true;
340355
if (_mqttc != null)
341356
{
342357
CleanAll();

0 commit comments

Comments
 (0)