Skip to content

fix(binding-mqtt): send PUBACK and PUBREC in publish receive order - #3106

Merged
jfallows merged 3 commits into
developfrom
fix/3102-mqtt-puback-order
Nov 17, 2026
Merged

jfallows merged 3 commits into
developfrom
fix/3102-mqtt-puback-order

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

Pipelined QoS 1 PUBLISH packets were acknowledged out of order, violating MQTT 5.0 [MQTT-4.6.0-2]; mosquitto_pub -l exits early and loses messages.

Root cause. MqttServerFactory kept pending QoS 1 / QoS 2 packet ids in two Long2LongHashMaps keyed by the publish stream's own initialSeq, then iterated them on each application window update:

  • iteration order of a hash map is not insertion order, so PUBACKs came back shuffled even on a single topic;
  • streams for different topics share the same maps, so equal sequences collided and overwrote each other, and the break on the first key above the acknowledge could leave acknowledgements unsent.

Reordering originates in the mqtt server, not in the mqtt-kafka produce path.

Fix. Each publish stream keeps its pending (sequence, packet id) pairs in a LongArrayQueue, and the connection keeps an ArrayDeque of the streams in publish receive order (mirroring the ordered stream queue in KafkaCacheClientConsumerFactory). Acknowledgements are emitted from the head only while the head's stream has been acknowledged, so PUBACK (QoS 1) and PUBREC (QoS 2) follow PUBLISH order across topics. PUBCOMP already follows PUBREL order since it is sent on receipt.

Tests (test-first).

  • New spec pair publish.pipelined.qos1.ack.out.of.order (application + network v4/v5), where the application acknowledges the second publish before the first, with peer-to-peer PublishIT methods.
  • Runtime PublishIT.shouldPublishPipelinedQos1AckOutOfOrder (v4, v5): fails on the old code (Specified behavior did not match expected on PUBACK), passes with the fix.
  • ./mvnw clean install -pl specs/binding-mqtt.spec,runtime/binding-mqtt (checkstyle, all ITs, coverage) passes.
  • Real Kafka (examples/mqtt.kafka.proxy, raw MQTT 5 client sending N back-to-back QoS 1 PUBLISH): before, 5 messages on one topic acked [5, 3, 1, 2, 4], 200 messages fully shuffled, and with 3 topics some acks were missing; after, 5 and 200 messages on 1 and 3 topics are acked in order.

Fixes #3102


Generated by Claude Code

Pending QoS 1 and QoS 2 packet ids were tracked in hash maps keyed by the
publish stream's own sequence, so acknowledgements were emitted in hash
iteration order, collided across streams with equal sequences, and could be
skipped by the early break. Track them in an ordered per-connection queue and
acknowledge from the head only once its stream has been acknowledged
([MQTT-4.6.0-2]).

Fixes #3102

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVggtnCBMjAczLoou4
…erleaved acks

Correct four scenarios that acknowledged across two topics out of receive
order, which violates [MQTT-4.6.0-2] and [MQTT-4.6.0-3] (a Server MUST send
PUBACK and PUBREC packets in the order the PUBLISH packets were received).
Each expected the acknowledgement of packet id 2 before packet id 1:

  network/v4/publish.qos1.two.topics.interleaved.acks
  network/v4/publish.qos2.two.topics.interleaved.acks
  network/v5/publish.qos1.two.topics.interleaved.acks
  network/v5/publish.qos2.two.topics.interleaved.acks

Only the order of the two acknowledgements changes, in each client.rpt and
server.rpt; the application scripts still acknowledge sensor/two first.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVggtnCBMjAczLoou4
@jfallows
jfallows force-pushed the fix/3102-mqtt-puback-order branch from 7345d4e to badddc6 Compare November 17, 2026 01:17
@jfallows
jfallows merged commit 3707898 into develop Nov 17, 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-kafka: PUBACKs for pipelined QoS 1 PUBLISH packets are returned out of order

2 participants