Skip to content

Commit 948c7c2

Browse files
Erw1nTchkr1011
andauthored
Corrected Cancellation Source for KeepAlive Messages (#2129)
* Corrected Cancellation Source for KeepAlive Messages * Use half of the keep alive value for the ping timeout * Update release notes * Update ReleaseNotes.md * Update ReleaseNotes.md * Update ReleaseNotes.md --------- Co-authored-by: christian <[email protected]>
1 parent 1cdc343 commit 948c7c2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Source/MQTTnet/MqttClient.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,11 +1041,13 @@ async Task TrySendKeepAliveMessages(CancellationToken cancellationToken)
10411041

10421042
if (timeWithoutPacketSent > keepAlivePeriod)
10431043
{
1044-
using (var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
1045-
{
1046-
timeoutCancellationTokenSource.CancelAfter(Options.Timeout);
1047-
await PingAsync(timeoutCancellationTokenSource.Token).ConfigureAwait(false);
1048-
}
1044+
using var pingTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
1045+
1046+
// We already reached the keep alive timeout. Due to the RFC part [MQTT-3.1.2-24] the server will wait another
1047+
// 1/2 of the keep alive time. So we can also use this value as the timeout.
1048+
pingTimeout.CancelAfter(keepAlivePeriod / 2);
1049+
1050+
await PingAsync(pingTimeout.Token).ConfigureAwait(false);
10491051
}
10501052

10511053
// Wait a fixed time in all cases. Calculation of the remaining time is complicated

Source/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Client: MQTT 5.0.0 is now the default version when connecting with a server **(BREAKING CHANGE)**
1414
* Client: Fixed enhanced authentication.
1515
* Client: Exposed WebSocket compression options in MQTT client options (thanks to @victornor, #2127)
16+
* Client: Fixed wrong timeout for keep alive check (thanks to @Erw1nT, #2129)
1617
* Server: Fixed enhanced authentication.
1718
* Server: Set default for "MaxPendingMessagesPerClient" to 1000 **(BREAKING CHANGE)**
1819
* Server: Set SSL version to "None" which will let the OS choose the version **(BREAKING CHANGE)**

0 commit comments

Comments
 (0)