Skip to content

fix(binding-mqtt): key pending acknowledgements per publish stream - #3186

Merged
jfallows merged 2 commits into
fix/3165-mqtt-ack-unknown-packet-idfrom
fix/3166-mqtt-pending-ack-keys
Nov 13, 2026
Merged

jfallows merged 2 commits into
fix/3165-mqtt-ack-unknown-packet-idfrom
fix/3166-mqtt-pending-ack-keys

Conversation

@jfallows

@jfallows jfallows commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

MqttServerFactory kept the pending QoS 1 and QoS 2 acknowledgements in two connection-level Long2LongHashMaps (unAckedReceivedQos1PacketIds, unAckedReceivedQos2PacketIds), keyed by the initialSeq of whichever publish stream carried the message. Each topic has its own publish stream with its own sequence, so the key is only meaningful inside one stream. The defect is real, and demonstrated by the failing scripts below:

  • Collision: two topics whose first messages are the same size both key to the same value (7 in the scripts). The second put overwrote the first, and one stream's window acknowledgement then completed the other stream's entry. The first message's PUBACK was never sent.
  • From reading the code (not separately demonstrated): acknowledgePublishPackets iterated the map in hash order, breaked at the first key above the acknowledgement, and removed entries while iterating.

What changed:

  • Each MqttPublishStream now keeps its own pending acknowledgements: two parallel Agrona lists (pendingAckSequences, pendingAckPacketIds) in arrival order, plus the stream's qos (a stream is already keyed per topic and qos by topicKey).
  • acknowledgePublishPackets drains the lists from the front while the head sequence is at or below the stream's own acknowledgement, so within one topic packets are acknowledged in the order received, nothing is skipped and nothing is removed while iterating.
  • PUBREC still adds the packet id to unreleasedPacketIds, which fix(binding-mqtt): answer an acknowledgement with an unknown packet id without failing #3179's onDecodePubrel checks.
  • The two connection-level maps and their Long2LongHashMap import are gone.

Fixes #3166

This PR is based on #3179's branch (fix/3165-mqtt-ack-unknown-packet-id, head 56994b6ea), stacked on #3176 and develop. It will be retargeted as the lower PRs merge.

Rebase onto #3179

Two rebases so far, both mechanical. The first onto f41fb39de had one import-line conflict. The second onto 56994b6ea had no conflicts. The lower PRs changed the subscribe-side handlers (onDecodePuback, onDecodePubcomp, onDecodePubrec) and onDecodePubrel's unreleasedPacketIds check, none of which this change touches. #3179's last two commits only add and drop test scenarios and change no production code. Its new recovery.pubrel.unknown.packet.id scenario sends a QoS 2 PUBLISH (PUBREC goes through the per-stream lists and adds the id to unreleasedPacketIds), reconnects, then sends a PUBREL for an unknown id on the new connection, which starts with empty lists. It passes with this change.

MQTT specification

I downloaded the full OASIS pages (mqtt-v5.0-os.html and mqtt-v3.1.1-os.html), converted them to text and read these sections myself: v5.0 2.2.1, 4.2, 4.3.2, 4.3.3, 4.4, 4.5, 4.6; v3.1.1 2.3.1, 4.2, 4.3.2, 4.3.3, 4.4, 4.5, 4.6. Quotes below are from that text.

What the specification requires of the receiver of a PUBLISH, which is what this change is anchored to:

  • QoS 1, receiver: "MUST respond with a PUBACK packet containing the Packet Identifier from the incoming PUBLISH packet, having accepted ownership of the Application Message" (v5 [MQTT-4.3.2-4]; v3.1.1 4.3.2, the sentence is tagged [MQTT-4.3.2-2]). Before the fix a PUBACK was never sent for the first of two same-sized messages on different topics.
  • QoS 2, receiver: "MUST respond with a PUBREC containing the Packet Identifier from the incoming PUBLISH packet" (v5 [MQTT-4.3.3-8]; v3.1.1 4.3.3 tagged [MQTT-4.3.3-2]).
  • "A PUBACK, PUBREC, PUBREL, or PUBCOMP packet MUST contain the same Packet Identifier as the PUBLISH packet that was originally sent" (v5 [MQTT-2.2.1-5]; v3.1.1 [MQTT-2.3.1-6], which lists PUBACK, PUBREC and PUBREL). The acknowledgement for a packet id must never be sent in place of another.
  • v5 2.2.1: "Packet Identifiers used with PUBLISH, SUBSCRIBE and UNSUBSCRIBE packets form a single, unified set of identifiers separately for the Client and the Server in a Session." The v3.1.1 2.3.1 text has no such sentence; there the rule is only that the sender assigns "a currently unused Packet Identifier" ([MQTT-2.3.1-2]) per Client or Server sender. So packet ids are per session, not per topic, in v5, and the per-topic stream sequence is not an identifier in either version.
  • Both versions say, in 4.3.2 and 4.3.3, that "a sender is permitted to send further PUBLISH packets with different Packet Identifiers while it is waiting to receive acknowledgements": packet ids let several messages be in flight and each acknowledgement be matched by id. I found no text stating that acknowledgements are intended to arrive in a different order.

What the specification says about acknowledgement order (4.6 "Message ordering"):

  • v5 4.6 opens "The following these rules apply to the Client when implementing the protocol flows defined in section 4.3", and then: "The Client MUST send PUBACK packets in the order in which the corresponding PUBLISH packets were received (QoS 1 messages) [MQTT-4.6.0-2]", likewise PUBREC in order of PUBLISH received [MQTT-4.6.0-3] and PUBREL in order of PUBREC received [MQTT-4.6.0-4].
  • v3.1.1 4.6 opens "A Client MUST follow these rules when implementing the protocol flows defined elsewhere in this chapter" with the same three rules ([MQTT-4.6.0-2], [MQTT-4.6.0-3], [MQTT-4.6.0-4]). It adds that "When a Server processes a message that has been published to an Ordered Topic, it MUST follow the rules listed above when delivering messages to each of its subscribers", that is, as sender to subscribers, plus the Ordered Topic rule for PUBLISH forwarding order ([MQTT-4.6.0-6]).
  • The Server rules in v5 are the Ordered Topic rules about the order of PUBLISH packets forwarded to consumers ([MQTT-4.6.0-5], [MQTT-4.6.0-6]).
  • So neither version, in the text I read, requires a Server to send PUBACK or PUBREC to a publisher in the order it received the PUBLISH packets. 4.6 does not justify this change. This change is justified by the packet identifier and acknowledgement rules above (a PUBACK is never lost and never sent for the wrong packet).
  • Consequence for the scripts: publish.qos1.two.topics.interleaved.acks expects PUBACK 2 before PUBACK 1 because the application acknowledged sensor/two first and each topic's stream is acknowledged independently. That order is a choice of this implementation, not something the specification demands or forbids for a Server. The same-topic script expects PUBACK 1 then PUBACK 2, which is the order of arrival; the specification does not demand it of a Server either, though the Client rule above is the same shape.
  • The claim that PUBACKs could be reordered by hash-order iteration (raised in mqtt: a failed PUBLISH on MQTT 5 should get a failure PUBACK or PUBREC, not close the connection #3175 and by the audit) is not supported by anything I read in the specification as a requirement on a Server, and I have not separately demonstrated it (see Test evidence).

Not read: v5 2.4 reason code table, 3.4 to 3.7 packet sections, 4.9 flow control, 4.12. I did not check other brokers.

Test evidence

Test-first: the first commit (ebc963842) holds scripts and IT methods only.

Scripts added (client and server pair each), with IT methods in the existing PublishIT classes (spec network v4 and v5, spec application, runtime server v4 and v5), no new IT class:

  • publish.qos1.two.topics.interleaved.acks (network v4, v5; application): two QoS 1 publishes on sensor/one (packet id 1) and sensor/two (packet id 2). The application side holds acknowledgements back (option zilla:update "handshake" and read option zilla:ack) and acknowledges sensor/two first. Expected PUBACK 2 then PUBACK 1.
  • publish.qos1.two.messages.one.ack (network v4, v5; application): two QoS 1 messages on one topic acknowledged by a single window advance (read option zilla:ack 14). Expected PUBACK 1 then PUBACK 2.

What failed first, against the unchanged production code of the base (56994b6ea, test commit only):

  • shouldPublishQoS1MessagesToTwoTopicsWithInterleavedAcks failed in runtime v4 and v5: PUBACK 2 arrived and PUBACK 1 was never sent, then the test timed out.
  • shouldPublishTwoQoS1MessagesAcknowledgedTogether failed in runtime v4 and v5 with a script mismatch on the PUBACK. I did not dig into whether the cause was order or a missing PUBACK, so the hash-order claim is not independently confirmed; I only know the unchanged code fails this scenario and the fixed code passes it.
  • The spec network and application ITs passed with the scripts alone (123 tests), so the scripts agree with each other.
  • The first scenario was first written against develop at b1a53e98f and failed there the same way before the rebase.
  • One unrelated runtime failure (shouldPublishSubscribeBatched, v5) appeared once on an early base-only run and did not appear in any later run, including this base. I did not investigate it, so I cannot say whether it is a flake.

After the fix, final verification on head 4f75caa34: clean install with checkstyle, license and coverage checks on, build cache disabled (-Dmaven.build.cache.skipCache=true), over specs/binding-mqtt.spec and runtime/binding-mqtt. Upstream modules were installed earlier from develop; the mqtt-kafka modules are not touched by this change and were not run. BUILD SUCCESS, 0 failures, 0 errors:

  • specs/binding-mqtt.spec: 487 ITs (2 skipped) + 75 unit and schema tests (4 skipped)
  • runtime/binding-mqtt: 401 ITs (6 skipped) + 44 unit tests

I ran this once on the final head, so I have no flake data.

CI: the checks that run on this PR are reported by the PR's check runs, not by this description.

Not delivered / follow-ups

  • No QoS 2 variant of the interleaved-ack scenario. The QoS 2 path (PUBREC and unreleasedPacketIds) uses the same per-stream lists and is covered by the existing QoS 2 ITs and fix(binding-mqtt): answer an acknowledgement with an unknown packet id without failing #3179's recovery scenario, which pass, but there is no new test that fails without the change.
  • The scripts assume both messages reserve 7 bytes (payload message, no padding), so the acknowledgement values 7 and 14 depend on that.
  • Not looked at: mqttPublishHelper is a single factory-level object shared by all connections' decoders; this change no longer reads its qos for acknowledgement, but I did not audit its other uses.
  • Open question, not changed: whether a Server should acknowledge PUBLISH packets from one publisher across topics in arrival order regardless of which application stream finishes first. The text I read does not require it of a Server; if it is wanted it needs a connection-level order, which is a design decision for the maintainers.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoY9w3hMTTDhfpoWhLG

@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 00:47
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: pending acknowledgements are tracked per stream sequence, not per connection packet id

2 participants