Repository navigation
fix(binding-mqtt): answer an unroutable filter with a per-filter suback failure - #3191
Conversation
…uback failure An unroutable or unauthorized filter in a SUBSCRIBE should get a failure code in its SUBACK position and leave the connection open, and an UNSUBSCRIBE for a filter that is unroutable or was never subscribed should get an UNSUBACK entry per filter (0x11 on MQTT 5). Network and application scripts for v5 and v4: mixed granted and unroutable filters, all filters unroutable, an unauthorized filter, UNSUBSCRIBE for an unroutable filter, and a mixed UNSUBSCRIBE. Against the unchanged binding the new server ITs fail: the SUBSCRIBE closes the connection instead of answering, and the UNSUBSCRIBE throws a NullPointerException on the unresolved route. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoVWUEF6tRdct6rasgP
…k form subscribe.unroutable and subscribe.invalid.topic.guarded.identity.param (network v5 and v4, application) encoded DISCONNECT 0x83 on v5 and a bare close on v4 for a filter that cannot be routed. The decision recorded on #3171 and #3177 is the SUBACK form: the filter gets a failure code in its SUBACK position and the connection stays open (MQTT 5.0 [MQTT-3.8.4-1], [MQTT-3.8.4-6], [MQTT-3.8.4-7], section 3.9.3; MQTT 3.1.1 [MQTT-3.8.4-1], [MQTT-3.8.4-5], return code 0x80). The scripts now expect SUBACK 0x83 (0x80 on v4) followed by a normal DISCONNECT, and the application side no longer sees the filter in the session state. The IT methods are renamed to match the scenarios. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoVWUEF6tRdct6rasgP
…le stored filter A resumed session whose stored state still holds a filter that no longer routes should connect with the session present, skip that filter, and resubscribe only the routable ones. Against the unchanged binding the CONNECT is answered with a close instead of a CONNACK. Network v5 and v4, application scripts for session expiry unset (v5) and session expiry never (v4). Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoVWUEF6tRdct6rasgP
…ck failure A filter that cannot be routed, or that no route authorizes for the client, now gets a failure code in its SUBACK position (0x87 not authorized, 0x83 otherwise; 0x80 on MQTT 3.1.1) and the connection stays open. The other filters of the SUBSCRIBE are granted as usual. The failure is decided per filter before the session state is written, so a failed filter is never written to the session state or subscribed. A SUBSCRIBE whose filters all fail is answered directly. A resumed session skips a stored filter that no longer routes instead of closing the connection. An UNSUBSCRIBE for a filter that matches no route or was never subscribed now gets its UNSUBACK entry (0x11 on MQTT 5) instead of a NullPointerException, and a mixed UNSUBSCRIBE gets one reason code per filter in order. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoVWUEF6tRdct6rasgP
CI:
|
Description
A SUBSCRIBE containing a filter that no route accepts, or that no route authorizes for the client, closed the connection (DISCONNECT 0x83 on MQTT 5, a bare close on MQTT 3.1.1), and an unauthorized filter was reported as 0x83, never 0x87. The filter was written to the session state before it was resolved. An UNSUBSCRIBE for a filter that matched no route threw a
NullPointerExceptioninsendUnsuback, and a mixed UNSUBSCRIBE answered fewer reason codes than filters.What changed (design as posted on #3177):
onDecodeSubscribeV4/V5, before anything is written to the session state, using the same route resolutionopenSubscribeStreamsuses. A newMqttBindingConfig.resolveSubscribeReasonCodereturns 0x87 when a route matches the topic but its guard does not authorize the client, otherwise 0x83. MQTT 3.1.1 maps either to 0x80 through the existingSUBACK_FAILURE_CODE_V4.Subscription.reasonCode, whichopenSubscribeStreamsalready encodes into the SUBACK and already uses to drop failed filters from the subscribe streams. A failed filter is added tosession.unAckedSubscriptions(so its SUBACK position is kept) and is skipped when the session state is built, so it is never written to the session state, never reaches Kafka, and cannot reappear on resume.unAckedSubscriptionsentries: echoed filters take their echoed code, failed ones keep theirs.unsubscribePacketFilters, mirroringsession.deferredUnsubscribes) so the v5 UNSUBACK carries one code per filter, in order. A failed filter inunAckedSubscriptionsno longer defers an UNSUBSCRIBE.MqttTopicFilter.reasonCodealready exists). Nomqtt-kafkaproduction change: its session factory echoes the state it stores, and a failed filter never reaches it.Fixes #3177
This PR is based on #3190's branch (
fix/3175-mqtt-v5-failure-publish-acks), the top of the stackdevelop<- #3176 <- #3179 <- #3186 <- #3190. It will be retargeted after the PRs below it merge.Statements checked against the OASIS text
I read the full MQTT 5.0 and 3.1.1 OASIS Standard pages myself (downloaded, converted to text, read with grep and sed) and checked the clause texts below. The same reading is posted on #3177.
Scripts rewritten, with the reason
subscribe.unroutableandsubscribe.invalid.topic.guarded.identity.param(network v5 and v4, application) encoded DISCONNECT 0x83 (v5) and a bare close (v3.1.1) for an unroutable filter. The decision recorded on #3171 and #3177 is that the SUBACK form is the intended behavior: the "MUST respond" clauses ([MQTT-3.8.4-1], v5 [MQTT-3.8.4-6] and [MQTT-3.8.4-7], v3.1.1 [MQTT-3.8.4-5]) and 4.13.2 were not reconciled by the specification, and the issue chose the acknowledgement form, which 4.13.2 describes as having "no consequences for other ... Packets flowing on the same Session". They are rewritten in their own commit so a reviewer can see exactly what changed: they now expect SUBACK 0x83 (0x80 on v4) followed by a normal DISCONNECT. The IT methods are renamed to match.Beyond a mechanical merge
Rebasing onto #3190 had one real conflict, in the
MqttServerconstructor: #3186 removed theunAckedReceivedQos1PacketIdsandunAckedReceivedQos2PacketIdsinitializers next to where I addunsubscribePacketFilters. I kept only my initializer. The three IT files where both sides appended methods at the end of the class were resolved by taking the base file and re-appending my methods. Nothing else was changed.Test evidence
Test-first: the first commits hold the scripts and ITs only. Against the unchanged production code (those commits on top of #3190's head,
9c85f3088), the new and rewritten server ITs failed, for v5 and v4:shouldSubscribeTopicFiltersUnroutableMixed,shouldSubscribeTopicFiltersUnroutableAll,shouldSubscribeTopicFilterUnauthorized,shouldSubscribeUnroutable,shouldSubscribeInvalidTopicGuardedIdentityParam: the connection was closed instead of a SUBACK being sent.shouldUnsubscribeTopicFilterUnroutable:NullPointerException: Cannot read field "id" because "resolved" is null.shouldUnsubscribeTopicFiltersMixedSubscribedAndNot(v5): the UNSUBACK had one reason code where two were expected. The v4 variant passes on unchanged code because a v3.1.1 UNSUBACK has no payload; it is a no-regression check and not a failing-first test.shouldSessionSubscribeUnroutableResume: the CONNECT was closed with no CONNACK.The spec peer-to-peer ITs passed, so the scripts agree with each other. No other test in those classes failed.
After the fix, each module run with
clean verify, checkstyle and license checks on, build cache skipped (-Dmaven.build.cache.skipCache=true), upstream modules installed with-am:specs/binding-mqtt.spec: 518 ITs, 0 failures, 2 skipped (+ 78 unit and schema tests, 4 skipped)specs/binding-mqtt-kafka.spec: 260 ITs, 0 failures (+ 27 unit tests)runtime/binding-mqtt: 419 ITs, 0 failures, 6 skipped (+ 44 unit tests)runtime/binding-mqtt-kafka: 148 ITs, 0 failures (+ 10 unit tests)MqttKafkaSubscribeProxyIT.shouldReceiveServerSentRetainedResetdid not fail in this run.New scenarios, each with network v5 and v4 scripts (except where noted) and an application script pair:
subscribe.topic.filters.unroutable.mixed,subscribe.topic.filters.unroutable.all,subscribe.topic.filter.unauthorized(guard roles config, v5 0x87, v4 0x80),unsubscribe.topic.filter.unroutable,unsubscribe.topic.filters.mixed.subscribed.and.not,session.subscribe.unroutable.resume(the v4 application script is...resume.session.expiry.never).Not delivered
mqtt-kafkaproduction change or script. Its session factory echoes the stored state and a failed filter never reaches it, so there is nothing new to pin there. Bothmqtt-kafkamodules are verified above only as dependents.*.non.successfulscripts) still leaves that filter in the session state. Nothing sets such a code today.sendUnsubackstill takes the reason code of the last stream when a filter appears on several QoS streams.Follow-ups
mqtt-kafkaroute mismatch.MqttKafkaBindingConfig.resolveAllkeeps the routes that match any filter of a SUBSCRIBE, against a route table separate from the mqtt binding's. If no route matches,MqttKafkaSubscribeFactoryrejects the stream, and the server answers DISCONNECT 0x83 (onSubscribeReset). If only some filters match, the unmatched ones are granted in the SUBACK but never delivered. Originating a per-filter code frommqtt-kafkameans settingreasonCodein the state that the session factory persists in the compacted sessions topic.sendUnsubackshould combine the codes of several streams.Generated by Claude Code