Skip to content

fix(binding-mqtt): send retained messages for retain handling 1 only on a new subscription - #3160

Merged
jfallows merged 3 commits into
developfrom
fix/3151-mqtt-retain-handling-1
Nov 12, 2026
Merged

jfallows merged 3 commits into
developfrom
fix/3151-mqtt-retain-handling-1

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

MQTT 5.0 [MQTT-3.3.1-10]: with Retain Handling 1 the server MUST send retained messages matching the filter if the subscription did not already exist, and MUST NOT send them if it did. MqttServerFactory set the send-retained flag only for Retain Handling 0, so value 1 behaved like 2 and never replayed.

Retain Handling 1 now carries SEND_RETAINED on the subscription (the same flag already carried in the session state and the subscribe begin/flush filters for value 0) only when the filter is not already among the session's subscriptions before this SUBSCRIBE. The production change is only in calculateSubscribeFlags. Retain Handling 0 and 2 handling is untouched, value 3 is still a protocol error, and MQTT 3.1.1 is unchanged. No Kafka-side change.

Based on develop.

Test evidence (test-first)

New v5 network and application script pairs, each with spec-level IT methods and server.v5.SubscribeIT methods:

  • subscribe.retain.handling.1.new.filter
  • subscribe.retain.handling.1.existing.filter
  • subscribe.retain.handling.0.existing.filter

Retain Handling 2 is already covered by subscribe.qos0.publish.retained.no.replay.

What failed first, run on the production code from before the fix with the final (corrected) scripts:

  • shouldSubscribeRetainHandling1NewFilter failed because of the production code: Retain Handling 1 on a new filter did not set SEND_RETAINED.
  • shouldSubscribeRetainHandling0ExistingFilter also failed, but only through its first step. That script's first SUBSCRIBE uses Retain Handling 1 on a new filter, so it hits the same production defect. The Retain Handling 0 re-subscribe step itself needed no production change; Retain Handling 0 behavior is unchanged.
  • shouldSubscribeRetainHandling1ExistingFilter passed before the fix and stays as a regression guard that a re-subscribed filter does not replay.

Scripts corrected in the second test commit (follow the flush on the existing subscribe stream when re-subscribing a filter): the two existing.filter application scripts. My first draft expected a second subscribe stream on a re-subscribe; the existing behavior is a FLUSH on the already open subscribe stream carrying all of its filters. This was a wrong script expectation, not a production defect. The new 1.new.filter scripts and the network scripts were not changed by it.

After the fix: specs/binding-mqtt.spec verify 75 unit + 458 IT, 0 failures; runtime/binding-mqtt verify 379 ITs, 0 failures (skips were already skipped), with checkstyle and license on and none of these modules restored from the build cache.

Not delivered / follow-ups

A filter repeated within one SUBSCRIBE, or re-subscribed before the earlier SUBSCRIBE is acknowledged, is judged against the acknowledged session subscriptions only.

Fixes #3151

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoY353ZpHcxZr2Kv8dD

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.

mqtt: Retain Handling 1 is treated as Retain Handling 2

2 participants