Repository navigation
fix(binding-mqtt): answer an acknowledgement with an unknown packet id without failing - #3179
Merged
jfallows merged 4 commits intoNov 13, 2026
Conversation
…unknown packet id Cover PUBACK, PUBREC, PUBCOMP and PUBREL for v4 and v5 on the mqtt server, a PUBREL retransmitted after PUBCOMP, and an unknown session flush packet id on the mqtt-kafka session proxy. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoUKWQVLPtbRGgBwMtC
…d without failing An unknown PUBACK or PUBCOMP is ignored, an unknown PUBREC is answered with PUBREL and an unknown PUBREL with PUBCOMP, carrying reason code 0x92 for MQTT 5. The mqtt-kafka session releases a flush for an unknown packet id instead of dereferencing missing offset metadata. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoUKWQVLPtbRGgBwMtC
jfallows
force-pushed
the
fix/3165-mqtt-ack-unknown-packet-id
branch
from
November 13, 2026 00:08
da8aa9b to
f41fb39
Compare
…nnect with a session present The client receives the PUBCOMP, loses the fact before persisting it, then reconnects with clean start 0 and resends the PUBREL as the specification requires. The server holds no state for the id and answers PUBCOMP, with reason code 0x92 for MQTT 5. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoUKWQVLPtbRGgBwMtC
…on the same connection The client resends a PUBREL after receiving its PUBCOMP without reconnecting, which [MQTT-4.4.0-1] does not allow. The reconnect scenario covers the same server answer for a conforming client, and the existing qos 2 scenario that reuses a packet id after the PUBCOMP covers the id being forgotten. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SiHVoUKWQVLPtbRGgBwMtC
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
An inbound PUBACK, PUBREC, PUBCOMP or PUBREL whose packet id the server has no state for dereferenced null:
MqttServerFactorylooked upqos1Subscribes/qos2Subscribesand called the result without a null check. An NPE there terminates the engine agent (AgentTerminationException), not only the connection, so one stray acknowledgement from one client takes down every connection on that worker. That raises the severity above the issue's "null dereference".MqttKafkaSessionFactory.onMqttFlush, the flush for an id with no offset state threw an NPE. The cause differs a little from the issue: in a freshly established sessionmetadataitself is null (Cannot read field "partitions" because "this.metadata" is null), not onlymetadata.partitions.get(packetId).What changed:
MqttServerFactory.onDecodePubackandonDecodePubcompignore an unknown id.onDecodePubrecanswers an unknown id with PUBREL (reason code 0x92 for v5, plain for v3.1.1), in the branch for a reason code below 0x80 (see "Adapting to mqtt: PUBACK or PUBREC with a failure reason code is treated as success #3164").onDecodePubrelanswers an id that is not inunreleasedPacketIdsitself with PUBCOMP (0x92 for v5, plain for v3.1.1), without flushing to the application. An id leavesunreleasedPacketIdswhen its PUBCOMP is sent, and a new connection starts with none, so this covers both a repeated PUBREL and one resent after a reconnect.doEncodePubrelV5anddoEncodePubcompV5take the reason code. The reason code is written withreasonCode & 0xff, as the other v5 encoders in this class do, because the constants are signed bytes.MqttKafkaSessionFactory.onMqttFlushreleases a flush for an id with no offset state through the existingdoMqttFlushpath instead of dereferencing missing state.doMqttFlushtolerates a nullmetadata.Fixes #3165
This PR is based on #3176's branch (
fix/3164-mqtt-ack-failure-reason-codes), which is stacked ondevelop. It will be retargeted todevelopafter #3176 merges.Spec compliance: what the text says, and where it does not decide
I downloaded both OASIS specifications in full (
mqtt-v5.0-os.html,mqtt-v3.1.1-os.html), converted them to text and read the sections below there, not through a summarising tool. Quotes are from that text. Clause ids are as the specification prints them. I also read the source of three open-source brokers and two OASIS TC Jira issues (listed under "Evidence outside the specification"), because the text leaves part of this open.Firm: PUBREL with an unknown id gets a PUBCOMP
Not decided by the text: an unknown PUBACK, PUBCOMP, or PUBREC
For these the specification defines no action:
Decision (recorded 2026-11-13): keep ignoring an unknown PUBACK and PUBCOMP, answer an unknown PUBREC with PUBREL and an unknown PUBREL with PUBCOMP, with 0x92 on v5 and plain on v3.1.1, as researched here. The evidence above does not show that these choices are required by the specification, so the strict reading is kept below as an open reading for reviewers.
Evidence outside the specification
I read the source below on 2026-11-13 from each project's default branch (no commit pinned).
lib/handle_pubackcomp.c,lib/handle_pubrec.c,lib/handle_pubrel.c,lib/send_mosq.c; same code for all protocol versions)apps/emqx/src/emqx_channel.erl,process_pubacktoprocess_pubcomp)packets.puback.missedpackets.pubcomp.missedapps/vmq_server/src/vmq_mqtt5_fsm.erl)apps/vmq_server/src/vmq_mqtt_fsm.erl)Implementations therefore agree on PUBREL (answered) and on PUBACK (ignored), and split on PUBCOMP (two ignore, one closes) and on PUBREC (answered with PUBREL by three, closed by one for v3.1.1 only). Mosquitto accepts 0x92 on a received PUBREL or PUBCOMP but never sends it. The Eclipse Paho interoperability client tests (
interoperability/client_test5.py) contain no test of an unknown packet identifier, so no conformance suite I could find pins this down.Not read, or not reachable
mqtt-commentlist thread (August 2021) that the search surfaced about answering an unexpected PUBREC with PUBREL 0x92 or DISCONNECT 0x82: the archive returned HTTP 403 (a bot challenge), so I did not read it and make no claim from it. The Google Groups thread on the v3.1.1 version of the question is blocked by this environment's network policy and was not read.Open readings and questions (code not changed in this PR)
onDecodePubackandonDecodePubcompchange to send DISCONNECT 0x82 (v5) and close, and the PUBACK/PUBCOMP script pairs change with them.ack.pubrec.unknown.packet.idscript pairs are the only things that change if this becomes "ignore" or "close".recovery.pubrel.unknown.packet.id(v4 and v5) now reconnects with clean start 0, gets Session Present 1, resends the PUBREL for an id the server no longer holds, and gets the PUBCOMP (0x92 for v5). The same-connection scenario was removed because the reconnect scenario produces the same server answer through the same decision for a conforming client, and the one other thing it exercised, the server forgetting an id after completing it, is covered bypublish.qos2.no.dupicate.before.pubrel(the id is reused for a new message after the PUBCOMP, [MQTT-4.3.3-12]). The consequence is that the issue's wording "PUBREL retransmitted after the PUBCOMP" is met by the reconnect form only. The recovery scenario's story is a client that received the PUBCOMP and crashed before persisting it; a PUBCOMP that is never delivered is not modelled, because k3po fails a client script that does not read data the server sends, and avoiding that needs a race.Adapting to #3164
#3164 changes
onDecodePubrecto take the v5 reason code: for 0x80 or above it completes the QoS 2 exchange throughonDecodePubcompand sends no PUBREL. The unknown-id handling sits in the branch for a reason code below 0x80. The branch for 0x80 or above delegates toonDecodePubcomp, which is now null-safe, so an unknown id with a failure reason code is ignored (v5 4.4 [MQTT-4.4.0-2]: such a PUBREC means the PUBLISH "is treated as acknowledged"; VerneMQ v5 does the same). That interaction has its own v5 scenario (ack.pubrec.failure.reason.code.unknown.packet.id).Test evidence
Commits above #3176's head, in order: the failing scripts and IT methods (
0f9ff22f4), the fix (f41fb39de), the recovery scenario (c1d7728df), and the removal of the same-connection scenario (56994b6ea). The recovery test sits after the fix because the first three commits were already pushed when it was added, and rewriting them would have changed the base of the PRs above this one.Test-first: the first commit holds the scripts and IT methods only. Against unchanged production code (develop at
b1a53e98f, before the first rebase onto #3164) the newbinding-mqttITs for PUBACK, PUBREC and PUBCOMP failed withAgentTerminationExceptioncaused byNullPointerException(streamis null indoSubscribeWindowfor PUBACK and PUBCOMP,qos2Subscribes.get(packetId)is null for PUBREC). The PUBREL ITs, v4 and v5, failed by timeout at 10 s, because the server flushed the unknown id to the application and never answered. The newMqttKafkaSessionProxyITmethod failed with the NPE onmetadata. The spec peer-to-peer ITs passed, so the scripts agree with each other.After the restack onto #3176's current head (
46ec36cd1, rebase without conflicts), I restoredMqttServerFactoryandMqttKafkaSessionFactoryto that head's version and ran the new ITs against it. At that point there were eleven newbinding-mqttITs (five scenarios in v4 and v5, including the same-connection retransmit scenario that was removed later, plusack.pubrec.failure.reason.code.unknown.packet.idin v5), and all eleven and the newMqttKafkaSessionProxyITmethod errored. Nine of those eleven remain. The two recovery ITs (v4 and v5) were run the same way when they were added: both errored with a 10 s timeout waiting for the PUBCOMP, and with the fix they passed in 3 of 3 runs. So all eleven newbinding-mqttITs that are in this PR now, and the mqtt-kafka IT, fail without this change. I did not record the failure cause of each on the restack run; the causes above are from the earlier runs.Two mistakes of mine were found by the first verification runs and fixed before the first push: the 0x92 reason code was written to the builder as a signed byte (-110, rejected by the
uint8field, 3 v5 errors), and my kafka scripts forsession.flush.unknown.packet.idpredated the retained-topic describe step added by #3162, so the session proxy IT timed out after the first rebase. I regenerated them from the currentsession.client.sent.reset, and re-checked after the restack that they still differ from it only by my own added lines. While writing the recovery scenario I also removed an assertion on how the publish stream terminates when the network aborts, because that is outside what the scenario tests and does not weaken any check on the PUBREL answer.Final verification on head
56994b6ea:clean installwith checkstyle, license and coverage checks on, build cache disabled (-Dmaven.build.cache.skipCache=true), upstream modules installed with-am, over the four modules touched. BUILD SUCCESS, 0 failures, 0 errors:specs/binding-mqtt.spec: 481 ITs (2 skipped) + 75 unit and schema tests (4 skipped)specs/binding-mqtt-kafka.spec: 250 ITs + 27 unit testsruntime/binding-mqtt: 397 ITs (6 skipped) + 44 unit testsruntime/binding-mqtt-kafka: 143 ITs + 10 unit testsI ran this once on the final head, so I have no flake data beyond the three runs of the recovery scenario.
Scripts added (client and server, a pair each):
ack.puback.unknown.packet.id,ack.pubrec.unknown.packet.id,ack.pubcomp.unknown.packet.id,ack.pubrel.unknown.packet.id,recovery.pubrel.unknown.packet.id. All five end with a PINGREQ and PINGRESP to show the connection stays usable. The application side reusessession.connectfor the fourack.*scenarios;recovery.pubrel.unknown.packet.idhas its own application pair, shared by v4 and v5.ack.pubrec.failure.reason.code.unknown.packet.id.mqtt/andkafka/:session.flush.unknown.packet.id.IT methods follow the one IT per component rule: v4 and v5
SubscribeITandPublishITin the spec network ITs and thebinding-mqttruntime ITs, the applicationPublishITfor the recovery peer pair,MqttITandKafkaITinbinding-mqtt-kafka.spec, andMqttKafkaSessionProxyIT. No new IT class.CI: this PR's base is #3176's branch, not
develop. The checks that run on it are reported by the PR's check runs, not by this description.Not delivered
unreleasedPacketIdsand the proxy's offset metadata disagree, because the server already answers a PUBREL it does not track itself. I did not add a way for the proxy to report "not found" to the server, because that needs an IDL change toMqttSessionFlushExand the issue did not ask for one.session.connectapplication script (or the recovery application pair), so they assert that no frame reaches the application session for the unknown id, but not that no stream is touched. They do not cover PUBREL for an id that is tracked by the server but whose flush the application never answers.Follow-ups
MqttSessionFlushExshould carry a reason code so the proxy can report "not found" to the server (see the first item under "Not delivered").🤖 Generated with Claude Code
https://claude.ai/code/session_01SiHVoUKWQVLPtbRGgBwMtC