Skip to content

Stop outbound drain when Session hand-off takes its queue. Claim Will before publishing it. Do not report a successful cancel while a reader owns the request. Take the client lock over the Session replay pool in MqttClient_Connect. - #622

Merged
embhorn merged 11 commits into
wolfSSL:masterfrom
kareem-wolfssl:zd22494
Oct 28, 2026

Conversation

@kareem-wolfssl

@kareem-wolfssl kareem-wolfssl commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

ZD#22494

Thanks to Gwanhyun Lee for the reports!

@kareem-wolfssl
kareem-wolfssl requested review from embhorn and a balanced review from Copilot September 24, 2026 23:29
@kareem-wolfssl kareem-wolfssl self-assigned this Sep 24, 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.

Copilot review overview

🟡 Changes recommended

Build failure, replay races, non-terminating cancellation, and duplicate QoS 0 delivery remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 3 High severity · 1 Medium severity

Open (4)
What changed in this PR

Hardens MQTT client and broker state ownership during concurrent cancellation, session replay, queue hand-off, and Will publication.

Changes:

  • Adds synchronization and cancellation ownership safeguards.
  • Prevents broker queue and Will reuse during re-entrant callbacks.
  • Adds regression tests and allocator instrumentation.
File Description
wolfmqtt/​mqtt_client.h Documents cancellation ownership semantics.
src/​mqtt_client.c Synchronizes replay state and pending-response cancellation.
src/​mqtt_broker.c Handles re-entrant queue transfer and Will publication.
tests/​test_mqtt_client.c Tests replay locking and cancellation.
tests/​test_broker_connect.c Tests queue hand-off and Will re-entry.
tests/​include.am Injects allocator hooks into unit tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/mqtt_broker.c Resolved
Comment thread src/mqtt_client.c Resolved
Comment thread tests/include.am Resolved
Comment thread src/mqtt_client.c Resolved

@embhorn embhorn 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.

Review skoll comments and failing tests

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.

Comment thread src/mqtt_client.c Resolved
Comment thread src/mqtt_client.c Resolved
Comment thread src/mqtt_broker.c Resolved

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Coverage: 2 of 5 in-scope changed file(s) opened by the reviewer; not opened: tests/test_broker_connect.c, tests/test_mqtt_client.c, wolfmqtt/mqtt_client.h

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Lite

Comment thread src/mqtt_client.c Resolved

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Coverage: 2 of 5 in-scope changed file(s) opened by the reviewer; not opened: tests/test_broker_connect.c, tests/test_mqtt_client.c, wolfmqtt/mqtt_client.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review October 6, 2026 20:06

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

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.

Copilot review overview

🟡 Changes recommended

Replay tagging and partial-write handoff handling still contain correctness gaps.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
Resolved since last review (3)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Reconcile moved writes that make partial progress

src/​mqtt_broker.c:2021

A moved write that made partial progress is left unreconciled because only exact completion reaches BrokerOrphan_ReconcileSent. In a nonblocking/custom transport, MqttPacket_Write can return MQTT_CODE_CONTINUE with client.write.pos > 0 after the reentrant handoff. The orphan then retries a partially sent QoS 0 PUBLISH, or retries QoS 1/2 without DUP. Reconcile whenever the moved write either completed or advanced the write position.

Comment thread src/mqtt_client.c Resolved
Comment thread src/mqtt_client.c Resolved

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.

Copilot review overview

🟡 Changes recommended

Cancellation callers and forced cleanup still permit object-lifetime races while another thread owns the response.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Callers ignore CONTINUE and reuse objects still owned by readers

wolfmqtt/​mqtt_client.h:899

The new MQTT_CODE_CONTINUE contract is not adopted by the repository's multithreaded callers. For example, examples/multithread/multithread.c:719 calls MqttClient_CancelMessage once and then lets the stack-allocated publish[] go out of scope; the connect, subscribe, ping, and unsubscribe paths also ignore the result. If a reader owns one of those responses, cancellation now returns CONTINUE while the pending node still points into that object, so those paths can reuse or release it despite this new requirement. Update all callers to retry/retain the object, or preserve a blocking cancellation contract.

Comment thread src/mqtt_client.c Resolved

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Coverage: 2 of 3 in-scope changed file(s) opened by the reviewer; not opened: tests/test_mqtt_client.c

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Lite

Comment thread src/mqtt_client.c Resolved

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Not reviewed (no scan target covers them): tests/include.am
Coverage: 2 of 3 in-scope changed file(s) opened by the reviewer; not opened: tests/test_mqtt_client.c

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review October 23, 2026 00:50

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

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.

🟡 Changes recommended

Unresolved critical and moderate findings remain in broker hand-off, pre-CONNACK behavior, cancellation ownership, replay cleanup, and persistence-test guards.

4 open issues
1 resolved since last review

Review effort: Lite

Comment thread src/mqtt_broker.c Resolved
Comment thread src/mqtt_client.c Resolved
Comment thread tests/test_broker_connect.c Resolved
Comment thread src/mqtt_client.c Resolved
…kes its queue.

Thanks to Gwanhyun Lee for the report.
Thanks to Gwanhyun Lee for the report.
… MqttClient_Connect.

Thanks to Gwanhyun Lee for the report.
…le CONNECT handshake instead of only from CONNACK, releasing them if the server answers with a fresh Session.
…lag in MqttClient_Replay_ResetSafe. Treat fully and partially written packets the same in BrokerClient_OutQueueMoved.
@kareem-wolfssl

Copy link
Copy Markdown
Contributor Author

3e56ca7 from #626 superseded commit e04019d from this PR, so e04019d was dropped.

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Not reviewed (no scan target covers them): tests/include.am
Coverage: 2 of 5 in-scope changed file(s) opened by the reviewer; not opened: tests/test_broker_connect.c, tests/test_mqtt_client.c, wolfmqtt/mqtt_client.h

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Lite

Comment thread src/mqtt_client.c Resolved

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.

🟡 Changes recommended

Unresolved critical replay-retention and broker hand-off issues remain, along with moderate transport-buffer and allocator-test issues.

2 open issues
4 resolved since last review

Review effort: Lite

Comment thread src/mqtt_broker.c Resolved
Comment thread src/mqtt_client.c Resolved
@kareem-wolfssl
kareem-wolfssl requested review from wolfSSL-Fenrir-bot and a lite review from Copilot October 27, 2026 00:45

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@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 #622

Scan targets checked: wolfmqtt-src, wolfmqtt-bugs
Not reviewed (no scan target covers them): tests/include.am
Coverage: 1 of 2 in-scope changed file(s) opened by the reviewer; not opened: tests/test_mqtt_client.c

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review October 27, 2026 00:48

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

Add appropriate WOLFMQTT_TEST_BROKER_ALLOC_H guards for tests.
@embhorn
embhorn merged commit 3a55679 into wolfSSL:master Oct 28, 2026
65 of 66 checks passed
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.

4 participants