Skip to content

fix(binding-mqtt): answer an acknowledgement with an unknown packet id without failing - #3179

Merged
jfallows merged 4 commits into
fix/3164-mqtt-ack-failure-reason-codesfrom
fix/3165-mqtt-ack-unknown-packet-id
Nov 13, 2026
Merged

jfallows merged 4 commits into
fix/3164-mqtt-ack-failure-reason-codesfrom
fix/3165-mqtt-ack-unknown-packet-id

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

An inbound PUBACK, PUBREC, PUBCOMP or PUBREL whose packet id the server has no state for dereferenced null:

  • PUBACK, PUBREC and PUBCOMP in MqttServerFactory looked up qos1Subscribes / qos2Subscribes and 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".
  • PUBREL was always flushed to the application session and never answered by the server. In 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 session metadata itself is null (Cannot read field "partitions" because "this.metadata" is null), not only metadata.partitions.get(packetId).

What changed:

  • MqttServerFactory.onDecodePuback and onDecodePubcomp ignore an unknown id.
  • onDecodePubrec answers 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").
  • onDecodePubrel answers an id that is not in unreleasedPacketIds itself with PUBCOMP (0x92 for v5, plain for v3.1.1), without flushing to the application. An id leaves unreleasedPacketIds when its PUBCOMP is sent, and a new connection starts with none, so this covers both a repeated PUBREL and one resent after a reconnect.
  • doEncodePubrelV5 and doEncodePubcompV5 take the reason code. The reason code is written with reasonCode & 0xff, as the other v5 encoders in this class do, because the constants are signed bytes.
  • MqttKafkaSessionFactory.onMqttFlush releases a flush for an id with no offset state through the existing doMqttFlush path instead of dereferencing missing state. doMqttFlush tolerates a null metadata.

Fixes #3165

This PR is based on #3176's branch (fix/3164-mqtt-ack-failure-reason-codes), which is stacked on develop. It will be retargeted to develop after #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

  • v5 4.3.3 [MQTT-4.3.3-11]: the receiver "MUST respond to a PUBREL packet by sending a PUBCOMP packet containing the same Packet Identifier as the PUBREL". v3.1.1 4.3.3, receiver rule (clause [MQTT-4.3.3-2]): "MUST respond to a PUBREL packet by sending a PUBCOMP packet containing the same Packet Identifier as the PUBREL". Neither has an exception for an unknown id.
  • v5 3.7.2.1 defines 0x92 "Packet Identifier not found" for PUBCOMP: "The Packet Identifier is not known. This is not an error during recovery, but at other times indicates a mismatch between the Session State on the Client and Server." [MQTT-3.7.2-1] requires a PUBCOMP to use 0x00 or 0x92. So the v5 answer with 0x92 is a reason code the specification defines for exactly this packet, and plain 0x00 would also conform.
  • v5 4.13.2: an acknowledgement with a reason code of 0x80 or greater "indicate[s] 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", so the 0x92 answer does not close the connection.
  • v3.1.1 has no reason codes, so the answer is a plain PUBCOMP.

Not decided by the text: an unknown PUBACK, PUBCOMP, or PUBREC

For these the specification defines no action:

  • v5 3.4.2.1 gives PUBACK exactly 0x00, 0x10, 0x80, 0x83, 0x87, 0x90, 0x91, 0x97, 0x99 and [MQTT-3.4.2-1] requires the sender to use one of them; 3.5.2.1 does the same for PUBREC ([MQTT-3.5.2-1]), with the same list. Neither has a "not found" code, so these packets cannot be answered with 0x92. v5 3.4.4, 3.5.4, 3.7.4, 4.3.2 and 4.3.3, and v3.1.1 4.3.2 and 4.3.3, define no action for an acknowledgement whose id the receiver does not know.
  • Text that points towards closing the connection. v5 3.14.2.1 describes DISCONNECT reason code 0x82 (Protocol Error) as "An unexpected or out of order packet was received", and [MQTT-4.13.1-1] says a Server that detects a Malformed Packet or Protocol Error "and a Reason Code is given in the specification, … MUST close the Network Connection". v5 1.2 defines a Protocol Error as one "found to contain data that is not allowed by the protocol or is inconsistent with the state of the Client or Server". v3.1.1 [MQTT-4.8.0-1]: "Unless stated otherwise, if either the Server or Client encounters a protocol violation, it MUST close the Network Connection on which it received that Control Packet which caused the protocol violation." An acknowledgement for an id the receiver has no record of can be read as an unexpected packet and so as one of these. This is the strict reading, and the text allows it.
  • Text that points against treating it as a violation. Neither specification names an unknown-id acknowledgement as a Protocol Error or protocol violation: I searched both texts for every use of "Protocol Error" and "violation". v3.1.1 uses "protocol violation" only for a second CONNECT, a SUBSCRIBE or UNSUBSCRIBE with no payload ([MQTT-3.8.3-3], [MQTT-3.10.3-2]), and the general [MQTT-4.8.0-1]. The v5 Protocol Error cases are property and packet-format rules, and sections 3.4 to 3.7 mention only a duplicated Reason String. v3.1.1 4.1 (non-normative) says "Normal operation of the Client of Server could mean that stored state is lost or corrupted because of administrator action, hardware failure or software failure", and v5 4.1.1 (non-normative) says the same, so an acknowledgement for state the receiver no longer holds can occur without any misbehaviour. v5 3.2.2.1.1 says Session Present exists to let the Client and Server "have a consistent view of the Session State", and the 0x92 rows treat a mismatch as something to report, not to close over.
  • What the TC did with the question. OASIS MQTT-325 ("Use Disconnect RC 0x82 in place of Nack for incorrect Packet Id", Closed, Resolution: Fixed) proposed that PUBREL 0x92, PUBCOMP 0x92 and SUBACK 0x91 "amount to a protocol error" and be replaced with DISCONNECT 0x82, and noted that "The same thing could happen on Publish / PubRec / PubAck, Unsubscribe/Unsuback as well". Its recorded resolution is "Merge with MQTT 236 Consolidate handling of errors". The published v5 text keeps 0x92 on PUBREL and PUBCOMP and adds 4.13.2, so the replacement was not adopted for those two. The tracker pages I could fetch do not record the TC's reasoning, so this is an inference from the outcome, not a stated decision.

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).

Implementation (file) Unknown PUBACK Unknown PUBCOMP Unknown PUBREC Unknown PUBREL
Eclipse Mosquitto (lib/handle_pubackcomp.c, lib/handle_pubrec.c, lib/handle_pubrel.c, lib/send_mosq.c; same code for all protocol versions) warning, ignored warning, ignored warning, then PUBREL with reason code 0 PUBCOMP with reason code 0, comment: "this could be due to a repeated PUBREL after a client has reconnected"
EMQX (apps/emqx/src/emqx_channel.erl, process_puback to process_pubcomp) ignored, counted as packets.puback.missed ignored, packets.pubcomp.missed PUBREL with 0x92 PUBCOMP with 0x92
VerneMQ v5 (apps/vmq_server/src/vmq_mqtt5_fsm.erl) ignored session terminated (source comment: "TODOv5: we should probably not terminate normally here but use one of the new reason codes") PUBREL with 0x92 (for a reason code below 0x80; at 0x80 or above the state is removed and nothing is sent) PUBCOMP with 0x92
VerneMQ v3.1.1 (apps/vmq_server/src/vmq_mqtt_fsm.erl) ignored session terminated ("definitely not well behaving client") session terminated plain PUBCOMP

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

  • The OASIS mqtt-comment list 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.
  • The Paho Python test broker source (not found at the paths I tried), HiveMQ's and other brokers' source, and MQTT-236's resolution text (the tracker page shows only its original proposal).
  • v5 sections other than those named above (3.1.4, 4.2 and 4.9 only skimmed for relevance; 4.12 read and unrelated), v3.1.1 sections other than 3.2.2.2, 4.1, 4.3.2, 4.3.3, 4.4, 4.5 and 4.8.

Open readings and questions (code not changed in this PR)

  1. Unknown PUBACK, PUBCOMP and (v3.1.1) PUBREC: ignore or close? Decided: ignore. The strict reading stays open. The text allows both, as set out above. Ignoring is what the issue asks for, matches Mosquitto and EMQX on PUBCOMP and all three brokers on PUBACK, and cannot break a conforming client that lost or never had the state. Closing has an explicit textual anchor (3.14.2.1 and [MQTT-4.13.1-1], v3.1.1 [MQTT-4.8.0-1]) and is what VerneMQ does for PUBCOMP. If reviewers want the strict reading, onDecodePuback and onDecodePubcomp change to send DISCONNECT 0x82 (v5) and close, and the PUBACK/PUBCOMP script pairs change with them.
  2. Unknown PUBREC answered with PUBREL. Decided: keep. For v5, permitted by the 0x92 row of 3.6.2.1 and [MQTT-3.6.2-1] (not required: [MQTT-4.3.3-4] requires PUBREL for a PUBREC "with the same Packet Identifier as the original PUBLISH packet", and an unknown id has none). EMQX and VerneMQ v5 do the same, with 0x92; Mosquitto answers PUBREL with reason code 0. For v3.1.1 no text anchors a plain PUBREL, Mosquitto answers it and VerneMQ closes. The two ack.pubrec.unknown.packet.id script pairs are the only things that change if this becomes "ignore" or "close".
  3. PUBREL resent after a reconnect (recovery): covered, and the same-connection retransmit scenario was replaced. The 0x92 descriptions say "not an error during recovery", and [MQTT-4.4.0-1] says a PUBREL is resent only "when a Client reconnects with Clean Start set to 0 and a session is present". The first version of this PR resent the PUBREL on the same connection, which is a client violation of [MQTT-4.4.0-1]. 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 by publish.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 onDecodePubrec to take the v5 reason code: for 0x80 or above it completes the QoS 2 exchange through onDecodePubcomp and sends no PUBREL. The unknown-id handling sits in the branch for a reason code below 0x80. The branch for 0x80 or above delegates to onDecodePubcomp, 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 new binding-mqtt ITs for PUBACK, PUBREC and PUBCOMP failed with AgentTerminationException caused by NullPointerException (stream is null in doSubscribeWindow for 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 new MqttKafkaSessionProxyIT method failed with the NPE on metadata. 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 restored MqttServerFactory and MqttKafkaSessionFactory to that head's version and ran the new ITs against it. At that point there were eleven new binding-mqtt ITs (five scenarios in v4 and v5, including the same-connection retransmit scenario that was removed later, plus ack.pubrec.failure.reason.code.unknown.packet.id in v5), and all eleven and the new MqttKafkaSessionProxyIT method 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 new binding-mqtt ITs 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 uint8 field, 3 v5 errors), and my kafka scripts for session.flush.unknown.packet.id predated the retained-topic describe step added by #3162, so the session proxy IT timed out after the first rebase. I regenerated them from the current session.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 install with 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 tests
  • runtime/binding-mqtt: 397 ITs (6 skipped) + 44 unit tests
  • runtime/binding-mqtt-kafka: 143 ITs + 10 unit tests

I 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):

  • binding-mqtt network v4 and v5: 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 reuses session.connect for the four ack.* scenarios; recovery.pubrel.unknown.packet.id has its own application pair, shared by v4 and v5.
  • binding-mqtt network v5: ack.pubrec.failure.reason.code.unknown.packet.id.
  • binding-mqtt-kafka mqtt/ and kafka/: session.flush.unknown.packet.id.

IT methods follow the one IT per component rule: v4 and v5 SubscribeIT and PublishIT in the spec network ITs and the binding-mqtt runtime ITs, the application PublishIT for the recovery peer pair, MqttIT and KafkaIT in binding-mqtt-kafka.spec, and MqttKafkaSessionProxyIT. 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

  • The mqtt-kafka release of an unknown id answers the session flush with the same packet id, so the server then sends a plain PUBCOMP (reason code success), not 0x92, on that path. The path is only reachable if the server's unreleasedPacketIds and 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 to MqttSessionFlushEx and the issue did not ask for one.
  • A PUBCOMP that is lost in flight (never delivered) before the PUBREL is resent is not modelled (see open question 3).
  • The new binding-mqtt scenarios use the session.connect application 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

  • Decide the strict reading in open question 1 if reviewers want it.
  • Decide whether MqttSessionFlushEx should 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

…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
…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
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 00:47
@jfallows
jfallows merged commit c081c53 into develop Nov 13, 2026
17 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: acknowledgement with an unknown packet id causes a null dereference

2 participants