Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve client disconnect #1873

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Source/MQTTnet/Client/MqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ async Task ProcessReceivedPublishPackets(CancellationToken cancellationToken)
}
catch (Exception exception)
{
_logger.Error(exception, "Error while handling application message.");
_logger.Error(exception, "Error while handling application message");
}
}
}
Expand Down Expand Up @@ -835,8 +835,7 @@ async Task ReceivePacketsLoop(CancellationToken cancellationToken)
_logger.Error(exception, "Error while receiving packets");
}

// The packet dispatcher is set to null when the client is being disposed so it may
// already being gone!
// 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);
Expand Down Expand Up @@ -865,7 +864,7 @@ async Task<TResponsePacket> Request<TResponsePacket>(MqttPacket requestPacket, C
}
catch (Exception exception)
{
_logger.Warning(exception, "Error when sending request packet ({0}).", requestPacket.GetType().Name);
_logger.Warning(exception, "Error when sending {0} request packet", requestPacket.GetType().Name);
packetAwaitable.Fail(exception);
}

Expand All @@ -877,7 +876,7 @@ async Task<TResponsePacket> Request<TResponsePacket>(MqttPacket requestPacket, C
{
if (exception is MqttCommunicationTimedOutException)
{
_logger.Warning("Timeout while waiting for response packet ({0}).", typeof(TResponsePacket).Name);
_logger.Warning("Timeout while waiting for {0} response packet", typeof(TResponsePacket).Name);
}

throw;
Expand Down Expand Up @@ -943,7 +942,7 @@ void TryInitiateDisconnect()
}
catch (Exception exception)
{
_logger.Warning(exception, "Error while initiating disconnect.");
_logger.Warning(exception, "Error while initiating disconnect");
}
}
}
Expand Down Expand Up @@ -978,7 +977,7 @@ async Task TryProcessReceivedPacket(MqttPacket packet, CancellationToken cancell
}
else if (packet is MqttPingReqPacket)
{
throw new MqttProtocolViolationException("The PINGREQ Packet is sent from a Client to the Server only.");
throw new MqttProtocolViolationException("The PINGREQ Packet is sent from a client to the server only.");
}
else
{
Expand All @@ -1000,14 +999,15 @@ async Task TryProcessReceivedPacket(MqttPacket packet, CancellationToken cancell
}
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 processing received packet ({packet.GetType().Name}).");
_logger.Error(exception, "Error while processing received {0} packet", packet.GetType().Name);
}

_packetDispatcher.FailAll(exception);
// The packet dispatcher may already be gone due to disconnect etc!
_packetDispatcher?.FailAll(exception);

await DisconnectInternal(_packetReceiverTask, exception, null).ConfigureAwait(false);
}
Expand All @@ -1017,7 +1017,7 @@ async Task TrySendKeepAliveMessages(CancellationToken cancellationToken)
{
try
{
_logger.Verbose("Start sending keep alive packets.");
_logger.Verbose("Start sending keep alive packets");

var keepAlivePeriod = Options.KeepAlivePeriod;

Expand Down Expand Up @@ -1055,18 +1055,18 @@ async Task TrySendKeepAliveMessages(CancellationToken cancellationToken)
}
else if (exception is MqttCommunicationException)
{
_logger.Warning(exception, "Communication error while sending/receiving keep alive packets.");
_logger.Warning(exception, "Communication error while sending/receiving keep alive packets");
}
else
{
_logger.Error(exception, "Error exception while sending/receiving keep alive packets.");
_logger.Error(exception, "Error exception while sending/receiving keep alive packets");
}

await DisconnectInternal(_keepAlivePacketsSenderTask, exception, null).ConfigureAwait(false);
}
finally
{
_logger.Verbose("Stopped sending keep alive packets.");
_logger.Verbose("Stopped sending keep alive packets");
}
}
}
Expand Down