Skip to content

Commit

Permalink
Adjust cancellation token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Jan 26, 2025
1 parent 0b82b3f commit acdab05
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Source/MQTTnet/Client/MqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,13 @@ async Task TrySendKeepAliveMessages(CancellationToken cancellationToken)

if (timeWithoutPacketSent > keepAlivePeriod)
{
using (var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
using (var pingTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
{
timeoutCancellationTokenSource.CancelAfter(Options.Timeout);
await PingAsync(timeoutCancellationTokenSource.Token).ConfigureAwait(false);
// We already reached the keep alive timeout. Due to the RFC part [MQTT-3.1.2-24] the server will wait another
// 1/2 of the keep alive time. So we can also use this value as the timeout.
pingTimeout.CancelAfter((int)(keepAlivePeriod.TotalMilliseconds / 2));

await PingAsync(pingTimeout.Token).ConfigureAwait(false);
}
}

Expand Down

0 comments on commit acdab05

Please sign in to comment.