Skip to content

fix(binding-mqtt): replace the subscription when a filter is subscribed again - #3234

Merged
jfallows merged 3 commits into
fix/3218-mqtt-suback-granted-qosfrom
fix/3217-mqtt-resubscribe-replaces
Nov 14, 2026
Merged

jfallows merged 3 commits into
fix/3218-mqtt-suback-granted-qosfrom
fix/3217-mqtt-resubscribe-replaces

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

A SUBSCRIBE for a Topic Filter identical to an existing Non-shared Subscription added a second entry to the session state and to the per-QoS-level subscribe streams, so the old entry stayed live. A QoS 2 to 0 downgrade kept delivering at QoS 2 and the old Subscription Identifier persisted.

It now replaces the subscription, in MqttServerFactory:

  • Session state: replaceSubscriptions builds the state with one entry per filter (a new successful entry replaces the existing one with the same filter), in onDecodeSubscribeV4 / onDecodeSubscribeV5.
  • Level streams: openSubscribeStreams first removes the old entry for the filter from every level stream of the route (MqttSubscribeStream.removeSubscriptions). Levels up to the new QoS then receive the new entry through the existing begin-or-flush path. Levels above it are flushed without the entry through doSubscribeFlushOrEnd.
  • Subscription Identifier: it is carried by the entry, so the new entry's identifier (or the generated one when the SUBSCRIBE has none, which is never sent to the client) replaces the old.
  • Identical re-subscribe: when the replacement is equal to the existing entry (same id, QoS, flags), onSessionData still recognises it as the acknowledged new subscription, so the SUBACK is sent and the streams are flushed. Previously the equality check dropped it.
  • Retain Handling 0 resends through the existing retained path: the flush now carries the replacement entry with its SEND_RETAINED flag.

Pattern mirrored: the UNSUBSCRIBE path, which is already keyed by filter (doSendSessionState, getSubscriptionByFilter, doSubscribeFlushOrEnd). The mqtt-kafka subscribe proxy already rebuilds its fetch filters from the full filter list in the flush, so it did not change. There is no IDL change.

Fixes #3217

Specification statements

Read in the OASIS pages, quoted by sentence (clause numbers differ between the body text and the summary tables, so the body text is quoted):

MQTT 5.0, 3.8.4 (SUBSCRIBE Actions)

  • [MQTT-3.8.4-3]: "If a Server receives a SUBSCRIBE packet containing a Topic Filter that is identical to a Non-shared Subscription" ... "then it MUST replace that existing Subscription with a new Subscription".
  • [MQTT-3.8.4-4]: "If the Retain Handling option is 0, any existing retained messages matching the Topic Filter MUST be re-sent" "but Applicaton Messages MUST NOT be lost due to replacing the Subscription" (the standard spells "Applicaton").
  • [MQTT-3.8.4-8]: "The QoS of Application Messages sent in response to a Subscription MUST be the minimum of the QoS" "of the originally published message and the Maximum QoS granted by the Server".

MQTT 5.0, 3.8.2.1.2 (Subscription Identifier)

  • "If there is a Subscription Identifier, it is stored with the subscription."
  • "If this property is not specified, then the absence of a Subscription Identifier is stored with the subscription."
  • Non-normative text under 3.8.4: identifiers are removed from the Session State when the Server receives "a SUBSCRIBE packet from the Client for the same Topic Filter" "but with a different Subscription Identifier or with no Subscription Identifier."

MQTT 5.0, 3.8.3.1 (Subscription Options)

  • "0 = Send retained messages at the time of the subscribe"
  • "1 = Send retained messages at subscribe only if the subscription does not currently exist"

MQTT 3.1.1, 3.8.4 (Response)

  • [MQTT-3.8.4-3]: "If a Server receives a SUBSCRIBE Packet containing a Topic Filter that is identical to an existing Subscription's Topic Filter" "then it MUST completely replace that existing Subscription with a new Subscription." "Any existing retained messages matching the Topic Filter MUST be re-sent," "but the flow of publications MUST NOT be interrupted [MQTT-3.8.4-3]."
  • [MQTT-3.8.4-6]: "The QoS of Payload Messages sent in response to a Subscription MUST be the minimum of the QoS" "of the originally published message and the maximum QoS granted by the Server."

Read: 5.0 3.8.4, 3.8.2.1.2 and 3.8.3.1; 3.1.1 3.8.4; 3.1.1 4.6 (message ordering, [MQTT-4.6.0-1] to [MQTT-4.6.0-6]) and the sentences of 3.1.1 4.3 on the QoS used for delivery. Not read: the body of 3.1.1 3.8.3 (only its Appendix B summary came back); 5.0 4.3 and 4.6 (they were not in the page text returned); the last 6,290 characters of the 5.0 page.

This change does not fully meet [MQTT-3.8.4-4] (5.0) or the "flow of publications MUST NOT be interrupted" sentence of [MQTT-3.8.4-3] (3.1.1). Frames already received on a level stream that loses its last entry (the QoS 2 to 0 case) are still dropped, as they are for UNSUBSCRIBE today. That is tracked in #3230. The entry replacement here is the part of [MQTT-3.8.4-3] and [MQTT-3.8.4-8] that does not depend on it: after the replacement, messages published are delivered at the new QoS, with the new identifier, once.

Test evidence

Commit 1 holds only scripts and ITs (new .rpt pairs and methods in the existing SubscribeIT classes, no new IT classes). The two existing application scripts subscribe.retain.handling.0.existing.filter and subscribe.retain.handling.1.existing.filter are corrected: they asserted two session state entries for one filter, which violates the replacement rule.

Against the unchanged production code (mvn -B clean verify -pl runtime/binding-mqtt -Dit.test=SubscribeIT), 11 runtime IT methods failed for the right reason: after the second SUBSCRIBE the server wrote a session state with two entries (observed bytes 0x01 0x2a 0x00 0x00 0x00 0x02 ..., length 0x2a, two items) where one was expected, and the script then timed out with Specified behavior did not match expected / TestTimedOutException:

  • v5: shouldSubscribeReplaceSameQos, shouldSubscribeReplaceSameOptions, shouldSubscribeReplaceDowngradeQos, shouldSubscribeReplaceUpgradeQos, shouldSubscribeReplaceAddSubscriptionId, shouldSubscribeReplaceRemoveSubscriptionId, shouldSubscribeRetainHandling0ExistingFilter, shouldSubscribeRetainHandling1ExistingFilter
  • v4: shouldSubscribeReplaceSameQos, shouldSubscribeReplaceDowngradeQos, shouldSubscribeReplaceUpgradeQos

The new scripts pass peer to peer in specs/binding-mqtt.spec (ApplicationIT, v4 and v5 SubscribeIT) before the fix.

After the fix (commit 2), checkstyle, license and jacoco checks on, system mvn:

  • mvn -B clean verify -pl specs/binding-mqtt.spec,runtime/binding-mqtt: runtime module 44 unit tests, 428 ITs, 0 failures, 6 skipped. v5 SubscribeIT 71 and v4 SubscribeIT 44 all pass.
  • mvn -B clean verify -pl specs/binding-mqtt.spec -Dmaven.build.cache.skipCache=true (the cache had restored this module in the combined run): 78 unit tests (4 skipped), 533 ITs, 0 failures, 2 skipped.
  • Upstream modules were built with -am first from the restored develop build cache.

Not run: the mqtt-kafka modules and the full reactor. No mqtt-kafka source or script changed. The new scripts were not run against a real MQTT client.

Not delivered

  • Messages already received on a level stream that loses its last entry are still dropped (mqtt: messages already in flight on a QoS level stream are dropped when a re-subscribe leaves that stream without entries #3230). This is the part of [MQTT-3.8.4-4] and 3.1.1 [MQTT-3.8.4-3] that is not met.
  • A message published at QoS 2 before a downgrade and still queued in the mqtt-kafka fetch of the old level can be fetched on that level after the change. The proxy ignores a flush with no filters, which is also the case for UNSUBSCRIBE today, so the old level's Kafka fetch is not narrowed. It was not traced here.
  • doSubscribeFlushOrEnd closes a stream that has not yet received its first window instead of flushing it, whatever filters remain. A level stream that only loses an entry goes through it, so this affects the window race of replace as well as UNSUBSCRIBE. It was left as it is.
  • A SUBSCRIBE with the same filter twice in one packet keeps the last entry in the session state; no script covers it.
  • A Shared Subscription is not covered by the replacement; only Non-shared filters were exercised.

Generated by Claude Code

…subscription

Add failing specs and ITs for a SUBSCRIBE whose Topic Filter is identical to an
existing Non-shared Subscription (MQTT 5.0 [MQTT-3.8.4-3], MQTT 3.1.1
[MQTT-3.8.4-3]): same QoS, same options and Subscription Identifier, QoS 2 to 0
downgrade, QoS 0 to 2 upgrade, and (v5) Subscription Identifier added, changed
and removed. Correct the two existing retain-handling existing-filter
application scripts, which asserted two session state entries for one filter
and so violated the replacement rule.

Against the current code the eleven scenarios below fail on the application
session stream: after the second SUBSCRIBE the server writes a session state with
two entries (the old entry and the new one) where one entry is expected (observed
bytes begin 0x01 0x2a 0x00 0x00 0x00 0x02 ..., length 0x2a, two items), so the
script does not match and then times out.

  server.v5.SubscribeIT: shouldSubscribeReplaceSameQos,
    shouldSubscribeReplaceSameOptions, shouldSubscribeReplaceDowngradeQos,
    shouldSubscribeReplaceUpgradeQos, shouldSubscribeReplaceAddSubscriptionId,
    shouldSubscribeReplaceRemoveSubscriptionId,
    shouldSubscribeRetainHandling0ExistingFilter,
    shouldSubscribeRetainHandling1ExistingFilter
  server.v4.SubscribeIT: shouldSubscribeReplaceSameQos,
    shouldSubscribeReplaceDowngradeQos, shouldSubscribeReplaceUpgradeQos

The scripts pass peer to peer in specs/binding-mqtt.spec.

Refs #3217

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoXftpX2HBvgygJcdft
…ed again

A SUBSCRIBE for a Topic Filter identical to an existing Non-shared Subscription
now replaces it: the session state holds one entry per filter, the new QoS,
options and Subscription Identifier take effect, the per-QoS-level subscribe
streams drop the old entry (levels above the new QoS are flushed without it) and
levels up to the new QoS carry the new entry. An identical re-subscribe, whose
state entry equals the existing one, is still recognised as the new subscription
and acknowledged.

Frames already received on a level stream that loses its last entry are still
dropped, as for UNSUBSCRIBE; see #3230.

Fixes #3217

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoXftpX2HBvgygJcdft
…' into fix/3217-mqtt-resubscribe-replaces

# Conflicts:
#	runtime/binding-mqtt/src/test/java/io/aklivity/zilla/runtime/binding/mqtt/internal/stream/server/v4/SubscribeIT.java
#	runtime/binding-mqtt/src/test/java/io/aklivity/zilla/runtime/binding/mqtt/internal/stream/server/v5/SubscribeIT.java
#	specs/binding-mqtt.spec/src/test/java/io/aklivity/zilla/specs/binding/mqtt/streams/application/SubscribeIT.java
#	specs/binding-mqtt.spec/src/test/java/io/aklivity/zilla/specs/binding/mqtt/streams/network/v4/SubscribeIT.java
#	specs/binding-mqtt.spec/src/test/java/io/aklivity/zilla/specs/binding/mqtt/streams/network/v5/SubscribeIT.java
@jfallows
jfallows changed the base branch from develop to fix/3218-mqtt-suback-granted-qos November 14, 2026 21:28
@jfallows
jfallows added this pull request to stack #3236 November 14, 2026 23:00
@jfallows
jfallows merged commit db1f16e into develop Nov 14, 2026
68 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.

mqtt: re-subscribing to the same filter adds a second subscription instead of replacing the first

2 participants