Only set _cleanDisconnectInitiated if no disconnect is pending

This commit is contained in:
Tobias Frick
2020-12-21 13:26:59 +01:00
parent b95d6b08bc
commit 91c8a15f65
+7 -7
View File
@@ -142,27 +142,27 @@ namespace MQTTnet.Client
ThrowIfDisposed(); ThrowIfDisposed();
var disconnectIsPending = DisconnectIsPending(); if (DisconnectIsPending())
{
return;
}
try try
{ {
_disconnectReason = MqttClientDisconnectReason.NormalDisconnection; _disconnectReason = MqttClientDisconnectReason.NormalDisconnection;
_cleanDisconnectInitiated = true; _cleanDisconnectInitiated = true;
if (!disconnectIsPending && _isConnected) if (_isConnected)
{ {
var disconnectPacket = _adapter.PacketFormatterAdapter.DataConverter.CreateDisconnectPacket(options); var disconnectPacket = _adapter.PacketFormatterAdapter.DataConverter.CreateDisconnectPacket(options);
await SendAsync(disconnectPacket, cancellationToken).ConfigureAwait(false); await SendAsync(disconnectPacket, cancellationToken).ConfigureAwait(false);
} }
} }
finally finally
{
if (!disconnectIsPending)
{ {
await DisconnectInternalAsync(null, null, null).ConfigureAwait(false); await DisconnectInternalAsync(null, null, null).ConfigureAwait(false);
} }
} }
}
public Task PingAsync(CancellationToken cancellationToken) public Task PingAsync(CancellationToken cancellationToken)
{ {
@@ -572,7 +572,7 @@ namespace MQTTnet.Client
if (!DisconnectIsPending()) if (!DisconnectIsPending())
{ {
await DisconnectInternalAsync(_packetReceiverTask, null, null); await DisconnectInternalAsync(_packetReceiverTask, null, null).ConfigureAwait(false);
} }
} }
else if (packet is MqttAuthPacket authPacket) else if (packet is MqttAuthPacket authPacket)
@@ -634,7 +634,7 @@ namespace MQTTnet.Client
{ {
try try
{ {
var publishPacketDequeueResult = await _publishPacketReceiverQueue.TryDequeueAsync(cancellationToken); var publishPacketDequeueResult = await _publishPacketReceiverQueue.TryDequeueAsync(cancellationToken).ConfigureAwait(false);
if (!publishPacketDequeueResult.IsSuccess) if (!publishPacketDequeueResult.IsSuccess)
{ {
return; return;