Repository navigation
Description
Problem
By reading MqttServerFactory (the MQTT 5 SUBSCRIBE option loop in decodeSubscribeV5, found while fixing #3216; not run): when a filter in a SUBSCRIBE is rejected (No Local on a shared subscription, wildcard rejections, Maximum QoS 3, reserved option bits, a repeated Subscription Identifier), the loop breaks, but the subscriptions collected from the earlier filters of the same packet are still processed after the DISCONNECT has been sent. The connection is closed, yet subscription state for filters of a packet that is malformed as a whole may have been created. MQTT 3.1.1 (decodeSubscribeV4) has to be checked for the same behaviour.
Specification statements (to be re-read from the OASIS text before writing scripts)
MQTT 5.0:
- 4.13.1 [MQTT-4.13.1-1]: "When a Server detects a Malformed Packet or Protocol Error, and a Reason Code is given in the specification, it MUST close the Network Connection".
- 3.8.3.1 [MQTT-3.8.3-5]: "The Server MUST treat a SUBSCRIBE packet as malformed if any of Reserved bits in the Payload are non-zero".
- 1.2: "Malformed Packet: A control packet that cannot be parsed according to this specification."
MQTT 3.1.1:
- 3.8.3 [MQTT-3-8.3-4] (the id is misspelled in the standard): "The Server MUST treat a SUBSCRIBE packet as malformed and close the Network Connection if any of Reserved bits in the payload are non-zero, or QoS is not 0,1 or 2."
The clause numbers differ between the body and the summary tables of the OASIS text, so quote the sentence, not only the id. Settle in this issue whether a packet treated as malformed may establish any subscription at all, from the text of 3.8.4 and 4.13.
Expected
- A SUBSCRIBE rejected as malformed or as a Protocol Error leaves the session with none of the subscriptions the packet carried, and no SUBACK is sent. This holds for a rejected filter at any position in the packet.
- A valid multi-filter SUBSCRIBE is unaffected.
- Where a filter is rejected for a per-filter reason that the text answers with a SUBACK reason code (no route, not authorized), the behaviour of mqtt: answer a filter that no route authorizes the same way as a filter that no route matches #3207 and mqtt: an unroutable or unauthorized filter should get a per-filter SUBACK failure code, not close the connection #3177 is not changed.
Test plan (test-first)
For v5 and v4, a script pair in specs/binding-mqtt.spec with a valid first filter followed by an invalid second filter, then a PUBLISH matching the first filter that must not be delivered, and the runtime server IT methods in the existing SubscribeIT classes. The first commit holds the failing scripts. If the code already behaves correctly for a case, say so with the passing run instead of changing code.
Notes
Follow-up from #3216 (PR #3225). Part of the MQTT conformance work under #512.
Activity
jfallows commentedon Nov 14, 2026
Analysis: the defect is in the code, but it is not observable on the wire
What the code does (current
develop,MqttServerFactory.onDecodeSubscribeV5and the v4 decode)Each rejection in the filter loop calls
onDecodeError, setsdecoder = decodeIgnoreAlland doesbreak. Thatbreakleaves only theforloop, not the enclosingdecode:block, so the filters collected before the rejected one are still processed.onDecodeErrorhas already calledcleanupStreamsUsingAbort, so the session stream is aborted first.What I measured
I added a temporary
System.errprobe todoSessionData(reverted, no production change) and ran three scenarios (valid first filter, rejected second filter):In all three the probe printed
data after abort=true: the session state carrying the earlier filter is still written to the session stream after that stream'sABORT.doSessionDatais guarded by!StreamState.closed(state), which needs both directions closed, and only the initial direction has been aborted.Other effects, read from the code only (not run)
subscribePacketIds.put(filter, packetId)runs inside the loop, before the packet is known to be valid.session.unAckedSubscriptions.addAll(newSubscriptions)runs after the loop for the earlier filters.subscribeFailedis true anddoSubscribeFailurestill runs after the DISCONNECT.Why no k3po script can observe it
The session stream has already delivered
abortedto the application script, which has finished, so the DATA frame that follows the ABORT cannot be read by a script. The subscribe streams are only opened when the application echoes the state back, and that stream is gone, so no subscription is established downstream in these scenarios.I therefore wrote the scripts as the issue describes (valid first filter, invalid second filter, connection closed, no SUBACK, no session state) and ran them against the unchanged code. They pass:
SubscribeIT:shouldRejectSecondFilterReservedOptionBit6,shouldRejectSecondFilterMaximumQos3(v5),shouldRejectSecondFilterMalformedSubscriptionOptions(v4)Per the maintainer's rules I have not changed production code. A failing test for the leaked state would need an observation that is not on the wire.
Spec text
The OASIS text does not settle whether a rejected packet may leave earlier filters subscribed. What I read (extracts of the v5.0 OS and v3.1.1 OS HTML; neither document in full):
The maintainer chose to treat a rejected packet as void as a whole (no session state, no
subscribePacketIdsentries, no SUBACK). That choice is not mandated by the text.The scripts and ITs are being added as coverage only, so this issue stays open.
Generated by Claude Code
jfallows commentedon Nov 14, 2026
The Retain Handling 3 check in
calculateSubscribeFlags(called from the v5 SUBSCRIBE loop) is one more instance of the post-error fall-through recorded here:onDecodeErroris called outside the loop'sdecoder = decodeIgnoreAll; breakexit, so the loop keeps building state after the DISCONNECT. As with the multi-filter case, it cannot be observed on the wire, because the session stream has already been aborted. The analysis, with code references and spec text, is in #3238 (comment). #3238 is closed as not planned.Generated by Claude Code
test(binding-mqtt): cover SUBSCRIBE with a valid then a rejected filt…
test(binding-mqtt): rename subscribe.reject.missing.packet.id to omit…