Skip to content

Add auto-ping feature to client#563

Open
embhorn wants to merge 6 commits into
wolfSSL:masterfrom
embhorn:gh501
Open

Add auto-ping feature to client#563
embhorn wants to merge 6 commits into
wolfSSL:masterfrom
embhorn:gh501

Conversation

@embhorn

@embhorn embhorn commented Jul 15, 2026

Copy link
Copy Markdown
Member

The core client now tracks outbound idle time and sends a PINGREQ from MqttClient_WaitMessage once the link has been idle for about three quarters of the negotiated keep-alive, so applications no longer have to schedule pings themselves. The time source is the compile-time macro WOLFMQTT_GET_TIME_S() (defaults to time(NULL), overridable; define WOLFMQTT_NO_TIME to compile it out), mirroring the broker. A v5 CONNACK Server Keep Alive override is honored, the scheduler is disarmed on disconnect, the mqttclient example keeps its old manual loop under WOLFMQTT_NO_TIME, and unit tests cover the scheduling paths.

Fixes #501

This comment was marked as resolved.

wolfSSL-Fenrir-bot

This comment was marked as resolved.

This comment was marked as resolved.

wolfSSL-Fenrir-bot

This comment was marked as resolved.

Comment thread src/mqtt_client.c Outdated
Comment thread src/mqtt_packet.c Outdated
aidangarske

This comment was marked as resolved.

@aidangarske aidangarske assigned embhorn and unassigned aidangarske Jul 15, 2026

This comment was marked as resolved.

wolfSSL-Fenrir-bot

This comment was marked as resolved.

@embhorn
embhorn requested a review from aidangarske July 16, 2026 15:42
@embhorn embhorn assigned aidangarske and unassigned embhorn Jul 16, 2026
Comment thread src/mqtt_client.c
return MQTT_CODE_SUCCESS;
}

/* Ping at ~3/4 of the interval so the PINGREQ reaches the broker before

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these comments are pretty large do you want to clean them up before merge?

Comment thread wolfmqtt/mqtt_client.h
#include "wolfmqtt/mqtt_sn_packet.h"
#endif

/* Automatic keep-alive scheduling. When a non-zero keep-alive is negotiated in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is crazy long is there a better place to document this?

Comment thread wolfmqtt/mqtt_client.h
* suitable clock, or define WOLFMQTT_NO_TIME to compile out automatic
* scheduling entirely; the explicit MqttClient_Ping / MqttClient_Ping_ex APIs
* still work in that case. */
#ifndef WOLFMQTT_NO_TIME

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to document this new macro anywhere other than in code?

Comment thread ChangeLog.md
@@ -1,5 +1,26 @@
## Release Notes

### v2.1.1 (In Development)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we normally do this? I think usually we wait to add changelog entries till release pr

@aidangarske aidangarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skoll Multi-Scan Review

Modes: review + review-security + bugsOverall recommendation: REQUEST_CHANGES
Findings: 3 total — 3 posted, 0 skipped
2 finding(s) posted as inline comments (see file-level comments below)
1 finding(s) not tied to a diff line (full detail below)

Posted findings

  • [Medium] [review] Missing non-blocking auto-ping timeout coveragetests/test_mqtt_client.c:2023-2402
  • [Low] [bugs] Auto-ping tests assume the time source is already past startuptests/test_mqtt_client.c:2045-2124

Findings not tied to a diff line

Non-blocking examples swallow unanswered auto-ping timeouts

File: examples/nbclient/nbclient.c:526-538, examples/mqttclient/mqttclient.c:600-616
Function: mqttclient_test
Severity: High
Category: Liveness / Error Handling

With WOLFMQTT_NONBLOCK and the new auto-ping enabled, MqttClient_WaitMessage can start client->keep_alive_ping and return MQTT_CODE_CONTINUE while waiting for PINGRESP. These examples then let mqtt_check_timeout convert repeated CONTINUE returns into MQTT_CODE_ERROR_TIMEOUT, but the new non-WOLFMQTT_NO_TIME branch treats that timeout as ordinary idle and continues. That loses the application's liveness deadline, which the PR says remains required under WOLFMQTT_NONBLOCK, so an unanswered auto PINGREQ can loop forever instead of disconnecting or surfacing an error.

Recommendation: Restore non-blocking liveness handling before merge. In WOLFMQTT_NONBLOCK builds, distinguish a plain idle poll from an auto-ping already in progress: if client->keep_alive_ping is active when mqtt_check_timeout fires, cancel or disconnect and return a real error; only swallow the timeout when no keep-alive exchange is pending.

Referenced code: examples/nbclient/nbclient.c:526-538, examples/mqttclient/mqttclient.c:600-611 (12 lines)


Review generated by Skoll

Comment thread tests/test_mqtt_client.c
Comment thread tests/test_mqtt_client.c
@aidangarske aidangarske assigned embhorn and unassigned aidangarske Jul 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comment thread src/mqtt_client.c

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #563

Scan targets checked: wolfmqtt-bugs, wolfmqtt-src

No new issues found in the changed files. ✅

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Comment thread src/mqtt_client.c Outdated
Comment thread src/mqtt_client.c Outdated
Comment thread src/mqtt_client.c Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #563

Scan targets checked: wolfmqtt-bugs, wolfmqtt-src

No new issues found in the changed files. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client Keep Alive PINGREQ Scheduling Is Not Built Into the Core Client

4 participants