Skip to content

fix(binding-kafka): retry init producer id on retriable errors - #3104

Merged
jfallows merged 8 commits into
developfrom
fix/3101-mqtt-kafka-concurrent-session-takeover
Nov 16, 2026
Merged

jfallows merged 8 commits into
developfrom
fix/3101-mqtt-kafka-concurrent-session-takeover

Conversation

@jfallows

@jfallows jfallows commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Root cause. A freshly started Kafka broker answers the first InitProducerId request with a retriable error, COORDINATOR_LOAD_IN_PROGRESS (14) or COORDINATOR_NOT_AVAILABLE (15), until its producer id state is loaded; the same request succeeds a few hundred milliseconds later (observed against apache/kafka:4.1.1: error 14 on the first attempt, success on the next, about 0.3s later). KafkaClientInitProducerIdFactory treated every non-zero error code as fatal and reset the application stream. In mqtt-kafka, that reset on the session's init-producer stream carries no reason code, so the MQTT server maps it to 0x8E (session taken over), and that code was even sent in a CONNACK where it is not valid. The client ids were never in conflict; the session was simply failed by a transient broker error.

Fix.

  • The init producer id client now re-sends the request when KafkaError reports the response error as retriable, instead of a fixed pair of codes. The retry is delayed with the exponential backoff the other clients use (50 << attempt, capped by the existing zilla.binding.kafka.client.reconnect delay, mirroring KafkaClientApiFactory) and is cancelled when the stream closes. Every non-retriable error still resets the application stream with the Kafka error.
  • A session stream reset no longer maps to 0x8E. The mqtt-kafka session factory resets the MQTT session with implementation specific error (0x83) when a Kafka stream fails, mirroring the existing compact-policy path. The MQTT server defaults a reset without a reason code to 0x83 instead of 0x8E, never encodes a reason code that is invalid in a CONNACK, and keeps 0x8E for a real session takeover. A v4 client is answered with return code 3 (server unavailable), since a v4 CONNACK only carries return codes 0 to 5.
  • The branch is merged with the current develop.

Tests (test-first).

  • Spec pairs init.producer.id.v4/produce.new.id.coordinator.load.in.progress, .coordinator.not.available, .request.timed.out (a retriable error beyond 14 and 15) and .cluster.authorization.failed (non-retriable, reset immediately, with an application scenario too), plus InitProducerIdIT and ClientInitProducerIdIT methods for each.
  • Spec pairs and ITs for the reset reason codes: session.server.sent.reset.before.connack and after.connack (MQTT server, v5, plus v4 before CONNACK), and session.qos2.meta.reset, session.qos2.init.producer.reset and session.qos2.offset.fetch.reset (mqtt-kafka). The existing v4 session.reject.non.compacted.sessions.topic script now expects return code 3.
  • ./mvnw verify (checkstyle, license, jacoco and all ITs) on specs/binding-kafka.spec, specs/binding-mqtt.spec, specs/binding-mqtt-kafka.spec, runtime/binding-kafka, runtime/binding-mqtt and runtime/binding-mqtt-kafka: BUILD SUCCESS, 0 failures.
  • Real Kafka (examples/mqtt.kafka.proxy, apache/kafka:4.1.1, fresh stack each run): on develop 62 of 63 connects were rejected with 0x8E (single client and 20 concurrent clients, three runs each); on this branch all 66 connected and subscribed with CONNACK 0, including with the backoff retry.

Reused-stack rejections in a QoS 1 ramp traced to a separate group assignment crash, fixed by #3292.

Fixes #3101


Generated by Claude Code

A Kafka broker that has just started answers the first InitProducerId
request with COORDINATOR_LOAD_IN_PROGRESS (14), and may answer
COORDINATOR_NOT_AVAILABLE (15), until its producer id state is loaded.
Both are transient, but the init producer id client treated every
non-zero error as fatal and reset the application stream, so callers saw
a failure for a request that succeeds moments later.

Retry the request on these two errors, as the group client already does
for FindCoordinator, and keep every other error code fatal.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoU937V4jRec4nwtVJe
…eason code valid for the packet

A reset of the mqtt session stream with no reason code was mapped to 0x8E
(session taken over), which is not a valid CONNACK reason code. The
mqtt-kafka session factory now resets with implementation specific error
(0x83) when a kafka stream fails, and the mqtt server defaults a bare reset
to 0x83, never encodes a reason code invalid in CONNACK, and keeps 0x8E for a
real takeover.

Refs #3101

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV4YmVnkN6i2vWSkDo
…ble return code

A v4 CONNACK only carries return codes 0 to 5, so a session stream reset before
CONNACK is answered with server unavailable (3).

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

The init producer id client retries the request when KafkaError reports the
response error as retriable, delayed with the exponential backoff capped by
the client reconnect delay, and resets the application stream with the error
for any other error.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV4YmVnkN6i2vWSkDo
@jfallows jfallows changed the title fix(binding-kafka): retry init producer id while coordinator is loading fix(binding-kafka): retry init producer id on retriable errors Nov 16, 2026
@jfallows
jfallows merged commit be68c7f into develop Nov 16, 2026
31 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: concurrent first connects after a fresh Kafka are disconnected with 0x8E (session taken over)

2 participants