Skip to content

fix(binding-mqtt-kafka): answer a filter that no route matches with a per-filter suback failure - #3231

Merged
jfallows merged 5 commits into
fix/3220-mqtt-unsubscribe-in-flight-publishfrom
fix/3208-mqtt-kafka-unmatched-filter-suback
Nov 14, 2026
Merged

jfallows merged 5 commits into
fix/3220-mqtt-unsubscribe-in-flight-publishfrom
fix/3208-mqtt-kafka-unmatched-filter-suback

Conversation

@jfallows

@jfallows jfallows commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

MqttKafkaBindingConfig.resolveAll keeps the mqtt-kafka routes that match any filter of a SUBSCRIBE, against a route table separate from the mqtt binding's. If no route matched any filter, MqttKafkaSubscribeFactory rejected the stream and the server answered DISCONNECT 0x83 (onSubscribeReset). If only some filters matched, the unmatched ones were granted in the SUBACK and never delivered. The design is posted on #3208 (#3208 (comment)).

What changed:

  • Decision point. The mqtt-kafka session factory marks each filter of the session state it echoes to the mqtt binding with MqttTopicFilter.reasonCode = 0x80 when no subscribe route authorized for the session matches the filter. This happens when the state is echoed after a SUBSCRIBE and when the stored state is delivered at CONNECT. Nothing new is persisted: the compacted sessions topic keeps the filter as the mqtt binding wrote it, and the code is recomputed from the current routes on every echo, so a code cannot outlive a route change. There is no distinction between "no route" and "no authorized route".
  • Shared resolution. A new MqttKafkaBindingConfig.resolveSubscribeReasonCode and the existing resolveAll share one predicate (subscribes), so the session and subscribe factories cannot disagree.
  • Size and credit. reasonCode is an existing uint8 with default 0, so the rewritten state has the same size as the original and the reserved credit of the DATA frame is unchanged. No IDL change.
  • mqtt binding. The echo path stored every echoed filter in the session state, including one carrying a failure code (this is the gap mqtt-kafka: a filter that no mqtt-kafka route matches is granted in the SUBACK but never delivered #3208 describes, reachable only once something sets a code). It now keeps only filters with SUCCESS. On resume, the code of a stored filter is carried so openSubscribeStreams skips it. The SUBACK position and code of the failed filter come from the existing acknowledgedSubscriptions / doEncodeSuback path.
  • Whole-SUBSCRIBE mismatch. It no longer reaches the subscribe stream: every filter is marked, openSubscribeStreams opens no stream, a SUBACK with a failure code per filter is encoded, and the connection stays open. The subscribe factory's reject and RESET for a stream with no matching route is unchanged.
  • Code. 0x80 on MQTT 5 per the maintainer's decision (fix(binding-mqtt): answer an unresolved subscribe filter or publish acknowledgement with 0x80 #3223 makes the mqtt binding's own resolveSubscribeReasonCode return the same 0x80). MQTT 3.1.1 SUBACK failure is 0x80 as before. MqttBindingConfig.resolveSubscribeReasonCode is not touched.

Pattern mirrored: the mqtt binding's doSendSessionState, which builds an MqttSessionStateFW with subscriptionsItem and a flyweight builder, for the state rewrite; and MqttBindingConfig.resolveSubscribeReasonCode (#3191) for the per-filter decision. MqttKafkaSessionFactory did not rebuild the state payload before, so the rewrite is new in that module; the maintainer accepted this on #3208.

Fixes #3208

Specification statements

I downloaded the OASIS MQTT 5.0 and 3.1.1 Standard pages, converted them to text and read these myself:

  • 5.0 section 3.8.4 in full. [MQTT-3.8.4-1]: "the Server MUST respond with a SUBACK packet". [MQTT-3.8.4-5]: a SUBSCRIBE with multiple Topic Filters is handled "as if it had received a sequence of multiple SUBSCRIBE packets, except that it combines their responses into a single SUBACK response". [MQTT-3.8.4-6]: the SUBACK "MUST contain a Reason Code for each Topic Filter/Subscription Option pair". [MQTT-3.8.4-7]: the Reason Code "MUST either show the maximum QoS that was granted for that Subscription or indicate that the subscription failed".
  • 5.0 SUBACK Reason Codes: 0x80 "Unspecified error": "The subscription is not accepted and the Server either does not wish to reveal the reason or none of the other Reason Codes apply." [MQTT-3.9.3-1] orders the codes as the Topic Filters.
  • 3.1.1 section 3.8.4: [MQTT-3.8.4-1] (SUBACK MUST be sent), [MQTT-3.8.4-4] (multi-filter combined SUBACK), [MQTT-3.8.4-5] (a return code per filter that "MUST either show the maximum QoS that was granted for that Subscription or indicate that the subscription failed"). [MQTT-3.9.3-2]: return codes other than 0x00, 0x01, 0x02 and 0x80 are reserved and MUST NOT be used, which is why a failure is 0x80 in 3.1.1.
  • 5.0 section 4.13.2: "Acknowledgment packets PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK with a Reason Code of 0x80 or greater indicate that the received packet, identified by a Packet Identifier, was in error. There are no consequences for other Sessions or other Packets flowing on the same Session." Closing is required for Malformed Packet and Protocol Error [MQTT-4.13.1-1] and for CONNACK/DISCONNECT with 0x80 or more [MQTT-4.13.2-1]. An unmatched filter is neither, so the DISCONNECT 0x83 of onSubscribeReset is not required.
  • 3.1.1 section 4.8: [MQTT-4.8.0-1] and [MQTT-4.8.0-2] require closing only for a protocol violation or a transient error.

Not read: the rest of 5.0 section 3.9 beyond the code table rows above, and the full 3-8/3-9 tables. No clause says whether a failed subscription is part of the Session State; leaving it out of the stored state the mqtt binding writes is a reading, not a MUST.

Test evidence

Test-first: the first two commits hold only scripts, ITs and a session-state builder in MqttFunctions; the third holds the production change. The second commit corrects a defect in my own first-commit script, not production behavior: the application scripts of subscribe.topic.filters.non.successful.unsubscribe are shared by v4 and v5, and a v3.1.1 SUBSCRIBE always sends SEND_RETAINED, so the state they expect must carry that flag (the first commit failed v4 for that reason, not the intended one). This is why MqttFunctions gained subscriptionWithReasonCode(pattern, id, qos, reasonCode, flags...), covered in MqttFunctionsTest.shouldEncodeMqttSessionState.

New scenarios (spec peer-to-peer ITs run in NetworkIT/ApplicationIT-style classes, binding ITs against the engine):

  • specs/binding-mqtt-kafka.spec: session.subscribe.unmatched.filter (one matching, one non-matching filter), session.subscribe.unmatched.all (no filter matches), session.subscribe.unmatched.resume (stored state delivered at CONNECT). MqttIT and KafkaIT pair each, and MqttKafkaSessionProxyIT runs each against the engine with proxy.when.subscribe.topic.with.messages.yaml.
  • specs/binding-mqtt.spec: subscribe.topic.filters.non.successful.unsubscribe (network v4 and v5, application): a failed filter echoed in the state is absent from the state written by the next UNSUBSCRIBE. session.subscribe.non.successful.resume (network v4 and v5, application, plus .session.expiry.never for v4): a stored filter carrying a failure code is not subscribed on resume.

Red, against the unchanged production code, confirmed for the right reason (logs kept):

  • MqttKafkaSessionProxyIT.shouldSessionSubscribeUnmatchedFilter, shouldSessionSubscribeUnmatchedAll, shouldSessionSubscribeUnmatchedResume: the echoed state expected subscriptionWithReasonCode("other/one", 1, 128) and read [0x01 0x29 0x00 0x00 0x00 0x02 …] with reason code 0 (3 of 3 failed).
  • mqtt server v5 and v4 SessionIT.shouldSessionSubscribeNonSuccessfulResume: the application stream saw MQTT_SUBSCRIBE_BEGIN_EX [clientId=client, qos=0, filters=array32<32, 2>], a subscribe stream for both filters instead of only reply.
  • mqtt server v5 and v4 SubscribeIT.shouldSubscribeTopicFiltersNonSuccessfulUnsubscribe: the state after UNSUBSCRIBE read [0x01 0x17 0x00 0x00 0x00 0x01 0x00 0x00 0x00 …], one filter (sensor/two) left in the state, where an empty state was expected.
  • The spec peer-to-peer ITs for these scenarios passed against each other (7 in binding-mqtt.spec, 6 in binding-mqtt-kafka.spec).

After the fix, each module clean install with checkstyle and license checks on, build cache skipped (-Dmaven.build.cache.skipCache=true), upstream modules installed beforehand, system mvn (the mvnw wrapper jar download returned 404 from Maven Central in this environment):

  • specs/binding-mqtt.spec: 525 ITs, 0 failures, 2 skipped (+ 78 unit tests, 4 skipped)
  • specs/binding-mqtt-kafka.spec: 285 ITs, 0 failures (+ 27 unit tests)
  • runtime/binding-mqtt: 423 ITs, 0 failures, 6 skipped (+ 44 unit tests)
  • runtime/binding-mqtt-kafka: 163 ITs, 0 failures (+ 10 unit tests)

Not delivered

  • No subscribe-factory change beyond the shared resolution: its reject-and-RESET for a stream with no matching route stays, as a guard for a peer that bypasses the session. No new test pins that path.
  • The state rewrite assumes the echoed state arrives in one DATA frame, as the mqtt server already does when it reads it (tryWrap on each DATA frame); a state split across frames is not handled.
  • The route resolution for the state echo uses the authorization of the reply DATA frame. The ITs use unguarded routes, so no test distinguishes a guarded session authorization.
  • From reading the code, session.setSubscriptions is called at one site only (the echo path after CONNECT), so the mqtt server may not populate its session subscriptions from the state delivered at resume. This is a reading of the code that was not tested, and this PR does not change it.
  • The stored state is not pruned: the failed filter stays in the compacted topic until the mqtt binding writes the next state.
  • A repeated echo of an unchanged stored record that still contains a marked filter is not covered by a script.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoW6JiEUeLJ93BQKGfQ

jfallows and others added 4 commits November 13, 2026 10:35
* feat(common-json): mark and rewind the generator

A generator can mark its position between writes and rewind to it, which discards the bytes written since and
restores the structural context, so a unit of output can be generated, measured and then kept or taken back and
generated again. This is the output-side counterpart of the parser mark and rewind.

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

* refactor(common-json): require every generator to support mark and rewind

The generator mark and rewind are declared without a default, as JsonGeneratorImpl is the one implementation
of JsonGeneratorEx and supports both.

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

* test(common-json): rewind the generator that escapes its output

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

---------

Co-authored-by: Claude <[email protected]>
…ter no route matches

Failing scripts and ITs only, ahead of the production change for #3208:
mqtt-kafka echoes the stored session state with reasonCode 0x80 on each
filter no subscribe route matches, and the mqtt binding leaves such a
filter out of its session state and skips it on resume.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6JiEUeLJ93BQKGfQ
…fter-failed-filter scenario

The application scripts of subscribe.topic.filters.non.successful.unsubscribe
are shared by the v4 and v5 ITs, and a v3.1.1 SUBSCRIBE always requests the
retained messages to be sent, so the state they expect must carry the
SEND_RETAINED flag in both versions. Adds the session state builder that sets
the qos, flags and reason code of a filter together.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6JiEUeLJ93BQKGfQ
… per-filter suback failure

The session factory marks each filter of the session state it echoes to the
mqtt binding, on a SUBSCRIBE and when the stored state is delivered at CONNECT,
with reason code 0x80 when no subscribe route authorized for the session
matches it. The mqtt binding carries the code into the SUBACK, opens no
subscribe stream for the filter, leaves it out of its session state and
skips it on resume, so the connection stays open and the other filters of the
SUBSCRIBE are delivered as usual. Nothing new is persisted.

Fixes #3208

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW6JiEUeLJ93BQKGfQ
…ght-publish' into fix/3208-mqtt-kafka-unmatched-filter-suback

# 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/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/3220-mqtt-unsubscribe-in-flight-publish November 14, 2026 21:23
@jfallows
jfallows added this pull request to stack #3236 November 14, 2026 23:00
@jfallows
jfallows merged commit d5f19d0 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-kafka: a filter that no mqtt-kafka route matches is granted in the SUBACK but never delivered

2 participants