Skip to content

fix(binding-kafka): abort an api request that ends before its body is sent - #3316

Merged
jfallows merged 1 commit into
developfrom
fix/3306-mqtt-kafka-abandoned-connect-stall
Nov 18, 2026
Merged

jfallows merged 1 commit into
developfrom
fix/3306-mqtt-kafka-abandoned-connect-stall

Conversation

@jfallows

@jfallows jfallows commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #3306

Fixes the stall: after clients abandon their CONNECT while Kafka is slow, Zilla stops completing every later session. The 31-worker/emqtt-bench setup and the unread-data observation on Zilla's Kafka connections were not reproduced here (see Not delivered), so a rerun of the original load against a build with this fix is worth doing.

Root cause

KafkaClientApiFactory.KafkaApiStream.onAppEnd only set closeInitial, so a stream that ended without sending its request body stayed in the shared KafkaApiClient.pending queue. When it reached the head, doEncodeRequest wrote only the request header to the broker and left requestInFlight set, so the broker waited for the rest of the frame, no response ever came, and every later request on that client queued behind it forever.

mqtt-kafka triggers it because the TCP close of a pending CONNECT reaches MqttSessionProxy.onMqttEnd, which calls doKafkaEnd on its KafkaFindCoordinatorStream; that stream only sends its body after the binding grants a window, which a stream queued behind a slow request has not yet received.

Fix

An END before requestBytesSent reaches requestLength is now handled like an abort, mirroring onAppAbort/onAppReset in the same class: a queued stream leaves pending and gets cleanupApp; the in-flight head recycles the connection with cleanupNet (the only case with a half-written frame on the wire), and onNetClosed then notifies the app through cleanupAppActive. No flag was added. Unlike the abort path, a queued stream does not recycle the shared connection.

Evidence (tests first)

  • New scenario create.topics.v7.request.ended in the existing KafkaCreateTopicsIT classes (application and network spec ITs, and the runtime IT), paired client/server scripts: a request ends without its body, the binding must abort it and recycle the connection, and the next request on the same client must complete.
  • On develop the runtime IT fails: TestTimedOutException, the binding never aborts the ended stream (a traced run showed onAppEnd sent=0 head=true and nothing further). With the fix it passes. The spec-level pairs pass.
  • Not expressible in k3po: a stream ending before its window (the queued case, which is the real trigger). The parser rejects write close before connected. That path is covered only by the real-broker run below.
  • ./mvnw license:format, then verify on specs/binding-kafka.spec (631 tests, 0 failures) and runtime/binding-kafka (538 tests, 0 failures), with checkstyle and jacoco, run with -Dmaven.build.cache.enabled=false.

Real broker, before and after

Setup: examples/mqtt.kafka.proxy (apache/kafka:4.1.1, Zilla image built from develop, -w 4), Kafka throttled with docker update --cpus 0.3, four processes sending MQTT 3.1.1 clean-session CONNECTs at 150/s for 30 s and closing the socket after 3 s without reading CONNACK (about 16,500 abandoned connects). Kafka then restored to 3 CPUs.

Develop This branch
CONNACKs during flood about 39 about 76
Fresh CONNECT after restore no CONNACK in 15 s, repeated for minutes CONNACK in 0.25–0.54 s (5/5)
Second heavier flood (800/s requested) n/a 3/3 fresh probes CONNACK in 0.22–0.34 s

On develop the broker request log showed Zilla sending only DESCRIBE_CONFIGS and METADATA, never FIND_COORDINATOR, and a live jdb dump of the FIND_COORDINATOR KafkaApiClient showed requestInFlight = true, 16,431 streams in pending, and a head stream with initialClosed, zero request bytes sent and no reply, on a healthy idle connection. Disabling client.connection.pool did not change the stall, so KafkaClientConnectionPool is not involved.

Not delivered

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoW7sUGNbaTd4o7Mcem

… sent

A stream queued on the shared KafkaApiClient that ended without sending
its request body was left in pending. When it reached the head, only the
request header was written to the broker and requestInFlight stayed set,
so no response ever arrived and every later request on that client
waited behind it. An END before the request is complete now removes a
queued stream, or recycles the connection when the stream is the
in-flight head, and aborts the stream's reply.

Refs #3306

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoW7sUGNbaTd4o7Mcem
@jfallows
jfallows merged commit 502545c into develop Nov 18, 2026
30 checks passed
@jfallows
jfallows deleted the fix/3306-mqtt-kafka-abandoned-connect-stall branch November 18, 2026 22:26
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: Zilla stops sending CONNACK to every client once pending clients start giving up

2 participants