From 7636cd0fdb6887ea0d53f30c6b8b9214b510d7f3 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sun, 2 Mar 2025 11:57:02 +0100 Subject: [PATCH] Backport of: 2129 cancellation token backport (#2144) * Increase version * Update release notes * Adjust cancellation token handling * Fix CI build --- .github/workflows/ReleaseNotes.md | 6 +----- .github/workflows/ci.yml | 4 ++-- Source/MQTTnet/Client/MqttClient.cs | 9 ++++++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 999e0b2f0..4a62f4b97 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,5 +1 @@ -* Core: Fixed issue when parsing AUTH packet with 0 length body (#2039). -* nuget: Changed code signing and nuget certificate (**BREAKING CHANGE**). -* TopicTemplates: Updated samples, parameter validation (#2022). -* ManagedClient: Switch SubscribeAsync/UnsubscribeAsync to IEnumerable (#2026). -* Server: Fix _LoadingRetainedMessageAsync_ not executed (#2025). \ No newline at end of file +* Client: Fixed wrong timeout for keep alive check (thanks to @Erw1nT, #2129) \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da2e1956c..03c0f617d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: [push, pull_request] env: - VERSION: "4.3.7.${{github.run_number}}" + VERSION: "4.3.8.${{github.run_number}}" jobs: build: @@ -12,7 +12,7 @@ jobs: steps: - name: Setup Windows SDK - uses: GuillaumeFalourd/setup-windows10-sdk-action@v1 + uses: GuillaumeFalourd/setup-windows10-sdk-action@v2.4 with: sdk-version: 18362 diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 4bf34c062..8a79421c4 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -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); } }