Skip to content

fix(binding-mqtt-kafka): complete a qos2 offset commit when kafka acknowledges it - #3309

Merged
jfallows merged 2 commits into
developfrom
fix/3303-mqtt-kafka-qos2-offset-commit-window
Nov 18, 2026
Merged

jfallows merged 2 commits into
developfrom
fix/3303-mqtt-kafka-qos2-offset-commit-window

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

Fixes #3303

Root cause

KafkaOffsetCommitStream.onKafkaWindow completed one queued commit for every WINDOW it received after the stream opened. The Kafka produce path grows the stream maximum while it holds the acknowledge, so a QoS 2 commit is followed by a credit-only window (ack=0 max=33419) and later by the acknowledging window (ack=651 max=32768). The credit-only window consumed the queued commit before Kafka had acknowledged it, and the acknowledging window found the queue empty and stopped the engine.

The fix mirrors MqttPublishProxy.unackedSeqs in the same class (the QoS 1 path): record the stream position at the end of each commit and complete the queued commits whose position the acknowledge has reached. A credit-only window completes nothing.

Introducing change: not established. The credit-only window comes from the Kafka produce path, which git log shows unchanged since 2.6.1. I reverted the KafkaCacheServerProduceFactory hunk of #3295 and the #3300 batching change in turn, rebuilt the image and re-ran the issue's command. Both still crashed with the identical window sequence, so neither is the cause. The released 2.7.1 image completes the same QoS 2 publish. I did not bisect further.

Tests first

  • 6fadc64fb adds the scenario publish.qos2.offset.commit.credit.window with KafkaIT.shouldPublishQoS2MessageOffsetCommitCreditWindow (client and server scripts run against each other) and MqttKafkaPublishProxyIT.shouldSendMessageQos2OffsetCommitCreditWindow.
  • Before the fix, the runtime IT fails with the issue's stack: NoSuchElementException from LinkedList.remove in MqttKafkaPublishFactory$KafkaOffsetCommitStream.onKafkaWindow:2136, then AgentTerminationException.
  • After the fix (554951889) it passes.
  • Why the existing QoS 2 publish ITs missed it: the scripts use option zilla:window 8192 in the default update mode. k3po answers each DATA with exactly one window whose acknowledge covers it, so every window was an acknowledgement and the queue and windows always matched 1:1. The new scenario uses option zilla:update "handshake" and sends read option zilla:window (maximum grows, acknowledge held) before read option zilla:ack. Under handshake mode the scenario also acknowledges the session stream and the messages stream explicitly (a QoS 2 data frame reserves the whole 8192 window, so its acks are 8192 and 16384).

Verification

  • ./mvnw clean install on specs/binding-mqtt-kafka.spec (checkstyle, jacoco): MqttIT 170 and KafkaIT 200 pass.
  • ./mvnw clean verify on runtime/binding-mqtt-kafka (checkstyle, jacoco): 212 ITs pass, including MqttKafkaPublishProxyIT 48 (QoS 0, QoS 1 and the existing QoS 2 scenarios unchanged) and the new method; unit tests 29 pass.
  • Real broker: examples/mqtt.kafka.proxy with an image built from this branch, apache/kafka:4.1.1, mosquitto_pub MQTT 5.
    • Before the fix, develop head (d8cac978c): mosquitto_pub -q 2 -t device/q2 gives NoSuchElementException at onKafkaWindow(MqttKafkaPublishFactory.java:2136) and ENGINE_STOPPED.
    • After the fix: PUBREC, PUBREL, PUBCOMP (RC:0) for the issue's command, and exactly one record with key device/q2 in mqtt-devices.
    • Five QoS 2 publishes in a row on one connection, each waiting for the previous to complete: every one gets PUBREC and PUBCOMP RC:0, and mqtt-devices holds 5 records for the key.
    • Eight concurrent connections, one QoS 2 publish each: 8 of 8 get PUBCOMP RC:0, one record per key, no engine stop.

Not delivered

  • Pipelined QoS 2 on one connection (mosquitto_pub -l sending several PUBLISHes without waiting for PUBREC): with this change the engine stops with an NPE in KafkaMergedFactory$KafkaMergedStream.onMergedInitialData (kafkaDataEx is null, a FIN-only frame arrives with no message in progress). The released 2.7.1 image does not crash here but also never sends PUBREC, so the client retransmits indefinitely. This looks like a separate defect: a QoS 2 message is sent to Kafka as an INIT fragment now and its FIN much later, so a second PUBLISH can interleave. I did not investigate it further and have not filed an issue.
  • Same idiom in MqttKafkaSessionFactory: onOffsetCommitAck treats every window after the stream opens as an acknowledgement of the oldest unackedPacketIds entry, guarded by isEmpty(), so it does not crash. A credit-only window there would release a PUBCOMP flush (and the seeded session stream) before Kafka acknowledged the commit. It needs the same position-based completion and its own failing script first.
  • Introducing change not identified (see above).
  • A first QoS 2 CONNECT right after the 2.7.1 image started returned CONNACK 142 "Session taken over" once. I did not investigate it, and it did not occur on this branch.

Generated by Claude Code

…edit-only window

The offset commit produce stream may grow its maximum while it holds its
acknowledge before it acknowledges the commit. The new scenario sends such a
window ahead of each acknowledgement on the publish offset commit stream. The
runtime IT fails against the current proxy with a NoSuchElementException from
KafkaOffsetCommitStream.onKafkaWindow, which stops the engine.

The existing publish.qos2 scenario never sent that window: it acknowledges each
commit with exactly one window, so every window was an acknowledgement.

Refs #3303

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoTtPnwEYaRNAuVJZAW
…nowledges it

The publish offset commit stream completed one queued commit for every window
it received after opening, so a window that only granted credit consumed a
queued commit before Kafka had acknowledged it, and the acknowledging window
that followed found the queue empty and stopped the engine.

Record the stream position at the end of each commit and complete the queued
commits whose position the acknowledge has reached, as the QoS 1 publish does
with its unacknowledged sequences. A credit-only window completes nothing.

Fixes #3303

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoTtPnwEYaRNAuVJZAW
@jfallows
jfallows merged commit 5db3314 into develop Nov 18, 2026
52 checks passed
@jfallows
jfallows deleted the fix/3303-mqtt-kafka-qos2-offset-commit-window branch November 18, 2026 22:26
jfallows pushed a commit to aklivity/mqtt-benchmark that referenced this pull request Nov 24, 2026
* Run Zilla 2.8.0-rc8 by default

2.8.0-rc8 includes aklivity/zilla#3540, which fixes the JVM crash in the Kafka cache's
produce-side segment compaction (aklivity/zilla#3536), and the QoS 2 fix from
aklivity/zilla#3309.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Remove Zilla issue notes from the README and the QoS 2 delivery check

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Drop the stale shared subscription note and reword the lower connection rate scenarios

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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: a QoS 2 PUBLISH crashes the engine (NoSuchElementException in KafkaOffsetCommitStream.onKafkaWindow)

2 participants