diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index a62530643..dc65140b9 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -708,6 +708,9 @@ async Task ProcessReceivedPublishPackets(CancellationToken cancellationToken) await eventArgs.AcknowledgeAsync(cancellationToken).ConfigureAwait(false); } } + catch (ObjectDisposedException) + { + } catch (OperationCanceledException) { } @@ -787,7 +790,7 @@ async Task ReceivePacketsLoop(CancellationToken cancellationToken) { try { - _logger.Verbose("Start receiving packets."); + _logger.Verbose("Start receiving packets"); while (!cancellationToken.IsCancellationRequested) { @@ -825,20 +828,22 @@ async Task ReceivePacketsLoop(CancellationToken cancellationToken) } else if (exception is MqttCommunicationException) { - _logger.Warning(exception, "Communication error while receiving packets."); + _logger.Warning(exception, "Communication error while receiving packets"); } else { - _logger.Error(exception, "Error while receiving packets."); + _logger.Error(exception, "Error while receiving packets"); } - _packetDispatcher.FailAll(exception); + // The packet dispatcher is set to null when the client is being disposed so it may + // already being gone! + _packetDispatcher?.FailAll(exception); await DisconnectInternal(_packetReceiverTask, exception, null).ConfigureAwait(false); } finally { - _logger.Verbose("Stopped receiving packets."); + _logger.Verbose("Stopped receiving packets"); } }