Skip to content

mqtt-kafka: a filter that no mqtt-kafka route matches is granted in the SUBACK but never delivered #3208

Closed

Description

@jfallows

Problem

MqttKafkaBindingConfig.resolveAll keeps the routes that match any filter of a SUBSCRIBE, against a route table separate from the mqtt binding's. Read on develop, not run as a failing scenario:

  • If no route matches any filter of the SUBSCRIBE, MqttKafkaSubscribeFactory rejects the stream and the server answers DISCONNECT 0x83 (onSubscribeReset), closing a connection that may carry other in-flight packets.
  • If only some filters match, the unmatched ones are granted in the SUBACK but never delivered.

The mqtt binding already answers a filter that its own routes cannot resolve with a per-filter SUBACK failure and leaves it out of the session state. A filter that only the mqtt-kafka route table fails to match gets neither.

Related: a failure code set by the application peer on a filter in the echoed session state (MqttTopicFilter.reasonCode) is carried into the SUBACK, but the filter itself stays in the stored session state. Nothing sets such a code today; this change would.

Expected

  • A filter that no mqtt-kafka route matches is answered with the same per-filter failure code as a filter that no route matches in the mqtt binding (0x83 on MQTT 5, 0x80 on MQTT 3.1.1), with no distinction between "no route" and "no authorized route".
  • The other filters of the SUBSCRIBE are granted and delivered as usual.
  • A SUBSCRIBE in which no filter matches gets a SUBACK with a failure code per filter and the connection stays open.
  • A failed filter is not left in the session state.

Design first

Before any code, post a short design on this issue: where the failure is decided and how it reaches the SUBACK (the existing reasonCode on the filter in the session state and application protocol, which component sets it and which persists it in the compacted sessions topic), what happens to the session state when a failed filter is echoed back, how a whole-SUBSCRIBE mismatch flows through the subscribe stream, and what changes in the mqtt-kafka session and subscribe factories. The pattern to mirror must be an existing implementation in the same modules; if none fits, or an IDL change is needed, say so and ask.

Test plan (test-first)

After the design is accepted: scripts for v5 and v4 in specs/binding-mqtt-kafka.spec (and specs/binding-mqtt.spec if the application protocol changes) for a SUBSCRIBE with one matching and one non-matching filter, a SUBSCRIBE with no matching filter, and a failed filter absent from the stored state on resume. The first commit holds the failing scripts.

Notes

Follow-up to the per-filter SUBACK failure change under #3177. Part of the MQTT conformance work under #512.

Activity

  1. jfallows commented on Nov 13, 2026

    @jfallows
    ContributorAuthor

    Design

    Update: the per-filter code is 0x80 (Unspecified error) on MQTT 5 and on MQTT 3.1.1 (the maintainer's decision, same value as the mqtt binding in #3223); the earlier text of this comment said 0x83 on MQTT 5. The three checks listed as unverified are done (results below), and the read side was chosen. The implementation is in #3231.

    Policy as given: no distinction between "no route" and "no authorized route". A filter that no mqtt-kafka SUBSCRIBE route matches gets the same per-filter code as an unroutable filter in the mqtt binding: 0x80 on MQTT 5 and on MQTT 3.1.1.

    What I read on develop

    • mqtt server (MqttServerFactory): per-filter failure travels on Subscription.reasonCode. openSubscribeStreams skips filters whose code is not SUCCESS, encodes the SUBACK from the list, and drops failed filters from the subscribe streams. MqttBindingConfig.resolveSubscribeReasonCode is the decision in the mqtt binding (fix(binding-mqtt): answer an unroutable filter with a per-filter suback failure #3191). When the session replies with state, the echoed filters are read with subscription.reasonCode = filter.reasonCode() and handed to openSubscribeStreams through acknowledgedSubscriptions.
    • Gap on the echo path. After the echo, session.setSubscriptions(newState) stores every echoed filter, including one whose code is a failure. This is the gap the issue describes, and it only becomes reachable once something sets a code.
    • mqtt server resume. The CONNECT/resume branch builds the stored filters without filter.reasonCode() before openSubscribeStreams(…, implicitSubscribe=true), so it must carry the code for a marked filter to be skipped there too. This is an extra finding beyond the first draft of the design.
    • mqtt-kafka session factory: opaque to filters. onMqttStateData forwards the state payload to the compacted sessions topic unchanged. KafkaSessionStateProxy.onKafkaDataImpl wraps the fetched value as MqttSessionStateFW and echoes it with doMqttData. It never reads subscriptions() or reasonCode.
    • mqtt-kafka subscribe factory: it does see filters, on BEGIN and on FLUSH, through MqttKafkaBindingConfig.resolveAll. resolveAll returns routes that match any filter, so a per-filter answer is lost. It cannot return a per-filter code on the request direction: a null route list becomes a RESET, which the mqtt server turns into DISCONNECT 0x83 (onSubscribeReset).

    Where the failure is decided

    In the mqtt-kafka session factory, at the point the state reaches the mqtt binding. A reply-side per-filter signal on the subscribe stream would need an IDL change. The session state already carries MqttTopicFilter.reasonCode, and the SUBACK is built from it, so no IDL change is needed.

    • A new MqttKafkaBindingConfig.resolveSubscribeReasonCode(authorization, filter) returns SUCCESS when a route authorized for the session matches the filter under MqttKafkaConditionKind.SUBSCRIBE, otherwise 0x80 (a constant of the mqtt-kafka binding). It mirrors MqttBindingConfig.resolveSubscribeReasonCode minus the 0x87 branch, which the policy removes. resolveAll and the new method share the same matching predicate, so the subscribe factory and the session factory cannot disagree.
    • The session factory sets reasonCode = 0x80 on each filter of the echoed state for which that method returns non-SUCCESS, and forwards the rest unchanged. reasonCode is an existing uint8 field with a default of 0, so the rewritten payload keeps its size and the credit accounting (reserved, window) is unaffected.

    Decided (maintainer): read side. The state is annotated when it is echoed to the mqtt binding (onKafkaDataImpl), not when it is written. The compacted topic keeps the filter exactly as the mqtt binding wrote it and no code is ever persisted. The annotation is recomputed from the current routes on every echo and on every resume, so a stale code cannot outlive a route change. The same code path covers the live SUBSCRIBE echo and the state delivered at CONNECT. Neither choice needed an IDL change.

    No existing implementation in the module to mirror. MqttKafkaSessionFactory has no code that rebuilds the state payload. The closest implementations are the mqtt binding's doSendSessionState, which builds an MqttSessionStateFW with subscriptionsItem and a flyweight builder, and resolveSubscribeReasonCode for the decision. The read-side rewrite is new in this module; the maintainer accepted that.

    What happens to a failed filter echoed back (mqtt binding change)

    On the echo path, session.setSubscriptions(...) keeps only filters with reasonCode == SUCCESS (the same skip #3191 applies when it builds the state). The failed filter's SUBACK position is kept because the SUBACK is encoded from the packet's unAckedSubscriptions/acknowledgedSubscriptions list, as today. On resume the code of a stored filter is carried, so openSubscribeStreams skips it.

    Whole-SUBSCRIBE mismatch

    It never reaches the subscribe stream. When every filter is annotated, openSubscribeStreams opens no subscribe stream and encodes a SUBACK with a failure code per filter; the connection stays open. For a partial match, only the matching filters reach the subscribe factory, so "granted but never delivered" cannot occur.

    • The subscribe factory's reject-and-RESET for a stream with no matching route is left as it is. It is now a guard against a peer that bypasses the session.
    • Production changes: MqttKafkaBindingConfig (the shared resolution), MqttKafkaSessionFactory (annotate the echoed state), MqttServerFactory (skip failed filters when storing the echoed state, carry the code on resume). MqttKafkaSubscribeFactory is not changed.

    Statements anchored in the specification

    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] SUBACK MUST be sent, [MQTT-3.8.4-5] a multi-filter SUBSCRIBE is handled as a sequence of SUBSCRIBEs with the responses combined into one SUBACK, [MQTT-3.8.4-6] one reason code per filter, [MQTT-3.8.4-7] the code "MUST either show the maximum QoS that was granted for that Subscription or indicate that the subscription failed".
    • 5.0 SUBACK Reason Code 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."
    • 3.1.1 section 3.8.4: [MQTT-3.8.4-1], [MQTT-3.8.4-4] (the multi-filter clause, numbered -4 in 3.1.1), [MQTT-3.8.4-5] (return code per filter, may indicate failure).
    • 3.1.1 [MQTT-3.9.3-2]: return codes other than 0x00, 0x01, 0x02 and 0x80 are reserved, which is why 3.1.1 uses 0x80 for the failure.
    • 5.0 4.13.1 / 4.13.2: [MQTT-4.13.1-1] and [MQTT-4.13.2-1] require closing for Malformed Packet, Protocol Error and CONNACK/DISCONNECT with 0x80 or more. SUBACK with a code of 0x80 or greater "indicate[s] that the received packet … was in error. There are no consequences for other Sessions or other Packets flowing on the same Session." So an unmatched filter does not justify the DISCONNECT 0x83 in onSubscribeReset.
    • 3.1.1 4.8: [MQTT-4.8.0-1] and [MQTT-4.8.0-2] close only for a protocol violation or a transient error.

    Not read: the rest of 5.0 section 3.9 beyond the code row above, and the full tables 3-8 and 3-9. No clause says whether a failed subscription is part of the session state; leaving it out is a reading, not a MUST.

    The three checks

    • Fragmented echo. The mqtt server already calls tryWrap on each session DATA frame without reassembly, so the whole state must already arrive in one frame; the rewrite adds no new constraint.
    • Authorization. The mqtt server opens the session stream and the subscribe stream with the same sessionId, and both route checks use it.
    • Subscription equality. equals includes reasonCode, so a failed filter, never stored in session.subscriptions(), is seen as new once and acknowledged with its echoed code.

    Test plan

    Test-first; implemented in #3231.

    • specs/binding-mqtt-kafka.spec (mqtt and kafka streams, no network side): one matching and one non-matching filter, no matching filter, and a stored non-matching filter delivered at CONNECT, each with the state echoed with reasonCode 0x80 on the non-matching filter.
    • specs/binding-mqtt.spec: network v4 and v5 plus application scripts for a failed filter that is absent from the state the binding writes next (UNSUBSCRIBE), and a stored filter carrying a failure code that is skipped on resume. No IDL change, so the application protocol is not touched.

    Generated by Claude Code

  2. added a commit that references this issue on Nov 14, 2026
    bb84239
  3. self-assigned this
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions