Skip to content

fix(binding-mqtt-kafka): guard expiry signal when the kafka session stream is not established - #3138

Merged
jfallows merged 3 commits into
developfrom
fix/3120-mqtt-kafka-expiry-signal-guard
Nov 12, 2026
Merged

jfallows merged 3 commits into
developfrom
fix/3120-mqtt-kafka-expiry-signal-guard

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

An MQTT client that connects and then disconnects while the Kafka session stream was never established terminated an engine worker with a NullPointerException:

Cannot invoke "...MessageConsumer.accept(int, DirectBufferEx, int, int)" because "receiver" is null

MqttSessionProxy.onMqttEnd
  -> KafkaSessionStream.sendExpirySignal
  -> KafkaSessionStream.doKafkaData
  -> doData   (receiver is null)

The exception escaped EngineWorker.doWork, so the worker thread was lost.

Cause

On the QoS 2 path without a Will flag, doEstablishSession opens the meta streams but never begins the KafkaSessionStream, so its kafka receiver stays null. onMqttEnd and onMqttAbort then call sendExpirySignal, which wrote to the missing receiver.

Change

KafkaSessionStream now checks isKafkaOpen() (the receiver exists, the initial side is opening and not closed, derived from the existing StreamState bitmask, with no new fields) before the three doKafkaData variants and doKafkaFlush. This covers sendExpirySignal, cancelExpirySignal, the will-signal sends and the data forwarded from the MQTT side, since all go through those helpers.

The doKafkaData and doKafkaBegin helpers of the meta, offset-fetch, init-producer and offset-commit streams were checked and left unchanged: they are only reached from window or data callbacks on streams that already have a Kafka stream.

Fixes #3120

Tests

Written first. A paired client.rpt / server.rpt scenario session.qos2.end.before.session.established (MQTT side and Kafka side) covers "the MQTT client connects, the Kafka session stream is not established, the MQTT client ends", with a spec-level method in MqttIT and KafkaIT and shouldEndQos2SessionBeforeSessionStreamEstablished in the existing MqttKafkaSessionProxyIT. No new IT class.

Before the fix the runtime IT failed with the NPE above (receiver is null, worker terminated). The spec-level ITs passed before the fix, because they exercise only the protocol scripts.

Run in the session that wrote the change, with checkstyle and license checks:

  • specs/binding-mqtt-kafka.spec with the build cache bypassed: 27 unit tests, MqttIT 93, KafkaIT 118, 0 failures.
  • runtime/binding-mqtt-kafka: 10 unit tests, MqttKafkaSessionProxyIT 28, MqttKafkaPublishProxyIT 39, MqttKafkaSubscribeProxyIT 55, 0 failures.

Not run: the manual zilla start reproduction from the issue; the k3po runtime IT is the equivalent.

Notes and follow-ups (not changed here)

  • KafkaSessionStream.onKafkaReset does not mark the initial side closed in state, so a stream reset by Kafka is not caught by the !initialClosed part of the guard. The kafka != null and opening checks still apply.
  • The doKafkaEnd and doKafkaAbort helpers of the meta streams rely on a kafka != null check only.
  • Other open PRs also edit MqttKafkaSessionFactory, so expect small rebase conflicts.

Generated by Claude Code

…ministic

The MQTT reply stream is never begun for a qos2 session that ends before the
session stream is established, so its throttle stays registered until the
kafka meta streams abort and the proxy aborts the MQTT reply. Without that
abort being part of the scenario, engine close could win the race and deliver
its synthetic reset (sequence Long.MAX_VALUE) to onMqttReset, failing its
sequence assertion. Kafka now aborts the meta streams after the proxy ends
them and the MQTT client reads the resulting abort, so the scenario completes
only after the proxy has torn down the MQTT reply.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoTc8VafVZ1K6X9hSwi
@jfallows
jfallows merged commit 6123f21 into develop Nov 12, 2026
16 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: NPE in MqttKafkaSessionFactory.doData via KafkaSessionStream.sendExpirySignal when a stalled first CONNECT disconnects

2 participants