Skip to content

mqtt-kafka: some of the first clients after start-up are disconnected right after CONNACK, with nothing logged #3308

Open

Description

@ankitk-me

Describe the bug

In a 10,000-connection test against a freshly started Zilla 2.7.2-rc1, 26 clients got CONNACK and were then
disconnected by Zilla (TCP closed by the server). All 26 were in the first 5 seconds of the ramp. Every client that
connected after that stayed connected for the full 30 minutes.

Zilla logged BINDING_MQTT_CLIENT_CONNECTED for all 10,000 clients and nothing else: no disconnect event, no error,
no exception.

To Reproduce

Setup (AWS, us-east-1, one subnet and cluster placement group):

  • Zilla ghcr.io/aklivity/zilla:2.7.2-rc1 on c6i.8xlarge, start -e -w 31. Config: examples/mqtt.kafka.proxy
    bindings with only the stdout log exporter, routing device/# to mqtt-devices.
  • Kafka apache/kafka:4.1.1, single broker on a separate c6i.8xlarge. Topics: mqtt-devices and mqtt-messages
    (16 partitions), mqtt-retained and mqtt-sessions (1 partition, compacted).
  • Two load generators (c6i.2xlarge), each running:
    docker run --network host emqx/emqtt-bench:0.6.3 conn -h <zilla-ip> -p 7183 -V 4 -k 300 -C true \
      -c 5000 -R 50 -n <1 | 5001> --prefix <run>-c-
    (MQTT 3.1.1, clean session, 100 connections/s in total.)

Zilla had been running idle for about 8.5 minutes before the first client connected.

Expected behavior

Every client that gets CONNACK 0 stays connected (keep-alive 300 s, the clients send nothing else).

Actual behavior

Count
Clients that got CONNACK (emqtt-bench connect_succ) 10,000
Zilla BINDING_MQTT_CLIENT_CONNECTED events 10,000
Established TCP connections on the Zilla host (ss, sport 7183), stable for 30 min 9,974
Established TCP connections on the two clients (ss, dport 7183) 4,981 + 4,993 = 9,974
Client exits 26 × EXIT for {shutdown,tcp_closed}

Client exits per second (UTC):

09:13:36  2
09:13:37  5
09:13:38  5
09:13:39  5
09:13:40  7
09:13:41  2

No listen-queue overflows or drops on the Zilla host (TcpExtListenOverflows, TcpExtListenDrops,
TcpExtTCPBacklogDrop all 0). No connect_fail, connection_refused or connection_timeout on the clients.

Notes

Update: 100,000-connection run

In a later run (fresh Zilla, 100,000 clients connecting at 100/s), Zilla closed 3,134 client connections within one
second
(11:05:56), near the end of the ramp and before any client published. All were from the same load generator,
and the clients saw EXIT for {shutdown,tcp_closed}. Zilla again logged nothing besides the connect events. 3,134 is
close to one worker's share of 100,000 clients over 31 workers (about 3,200).

Environment

Activity

  1. added
    bug
    on Nov 17, 2026
  2. jfallows commented on Nov 17, 2026

    @jfallows
    Contributor

    Analysis from a first pass at making this diagnosable. I did not find the cause of (a) or (b), and I have no fix to propose yet.

    1. Diagnosability: PR #3313

    #3313 adds BINDING_MQTT_KAFKA_STREAM_FAILED (binding.mqtt.kafka.stream.failed). It is emitted when a failed Kafka stream (session, will signal, meta, describe, offset fetch, offset commit, init producer id, produce, fetch) closes the MQTT session, publish or subscribe stream. It carries the stream kind, the client id, the topic or group, and whether the MQTT reply had already begun. Example from an IT:

    The Kafka meta stream for topic (mqtt-messages) failed before MQTT client (client-1) was connected, the MQTT stream was closed.

    If either failure reproduces with that build, the event names the failing Kafka stream. If it does not appear, the close does not come from a Kafka stream failure.

    2. What I could reproduce (4 vCPU, 16 GB, Docker, branch image, start -e -w 31, single Kafka 4.1.1, topics as in the issue)

    • Not reproduced: (a) at 100 conn/s with emqtt-bench 0.6.3 -V 4 -k 300 -C true. Five 2,000 to 5,000 client runs on fresh and idle stacks gave 0 closes and 0 stream-failed events. One of those runs used client.max.idle.ms=6000 and client.meta.max.age.ms and client.describe.max.age.ms set to 3000, with idle gaps over 20 s.
    • Not reproduced: (b). In one 500 conn/s run through the published port with --network host, Zilla stopped accepting after about 850 sessions. Over 3,000 clients exited with tcp_closed and 1,600 with econnrefused, and the log showed 3 offset-commit stream-failed events at the same second. The same ramp from the compose network connected all 5,000 clients, so I attribute the stall to the host NAT path and do not count it as a Zilla result.
    • Reproduced: silent closes by worker capacity. With -w 4 -P zilla.engine.worker.capacity.limit=64 (256 slots in total), 248 and 400 client ramps connected about 218 clients. About 30 and 182 clients were closed by Zilla, mostly with tcp_closed before CONNACK and a few with connect error - closed. Zilla logged nothing. The mechanism is in the code:
      • TcpBindingController.handleAccept tries every worker's TcpBindingContext.accepted(...), which refuses when usage.available() <= 0. If none accepts, it calls quietClose(channel), with no event.
      • TcpClientFactory (line 176) rejects an outbound connect on the same condition, so a Kafka connect on a full worker is reset as well.

    3. Hypotheses checked against the code

    • A shared per-worker meta or describe fan-out failing and resetting every session on the worker: not supported. KafkaCacheServerMetaFanout (cache.server.reconnect defaults to 5) reconnects on an upstream end, abort or reset when it still has members. It notifies members only when the reconnect delay is 0. The Kafka client refreshes metadata every min(client.meta.max.age.ms, client.max.idle.ms / 2), which is 30 s by default, and that does not close members.
    • The worker capacity limit: possible, unconfirmed. The default worker.capacity is a power of two derived from memory. On this host it is 2048 per worker, matching the 131072-byte budgets* files (131072 / 64). It counts every accepted TCP connection and every outbound connect on that worker, so Kafka connections share the budget. For 31 workers on a 64 GB host I estimate 4096 per worker, about 127k in total, which is above 100k clients. That would not explain closes at about 97k, but the estimate depends on the actual memory and disk of the issue's host.

    4. Questions and next steps

    1. Please re-run with the image or jar built from feat(binding-mqtt-kafka): emit an event when a Kafka stream failure closes an MQTT stream #3313. A BINDING_MQTT_KAFKA_STREAM_FAILED line for the closed clients says which Kafka stream failed and whether CONNACK had been sent.
    2. Please report the capacity on the issue's host: ls -l /var/run/zilla/budgets0, size divided by 64. Please also check how many Kafka connections Zilla holds when the closes start.
    3. Proposed follow-up if (2) shows the limit is reached: emit an engine or tcp event when a socket is refused or closed because of worker capacity, instead of quietClose. This is a tcp binding change, not mqtt-kafka. I have not implemented it.

    I am not opening a fix PR for (a) or (b) until the event output or the capacity numbers point at a cause.


    Generated by Claude Code

  3. jfallows commented on Nov 18, 2026

    @jfallows
    Contributor

    Correction to my earlier analysis: please disregard the request to re-run with the build from #3313. That PR is closed. A Kafka stream reset or abort that closes an MQTT stream is a normal reaction to backend teardown, and Zilla does not log it as an event.

    To diagnose this, please capture zilla dump while the closes happen:

    1. Start zilla dump -w <file>.pcap on the Zilla host before the ramp and keep it running until a minute after the first mass close. -b <namespace.binding,...> limits the capture to specific bindings, for example the mqtt-kafka proxy and the Kafka cache and client bindings. -i installs the Wireshark dissector.
    2. Open the capture in Wireshark with the Zilla dissector. For a client that got CONNACK and was then closed, the stream order shows which Kafka-side stream (meta, describe, offset fetch, init producer id, offset commit, produce, fetch, will) ended or reset just before the MQTT close.
    3. If whole batches of clients close in the same second (as in the 100,000-client run), check whether their Kafka-side streams all end together, and whether they sit on one worker.

    From that capture we can decide whether a code fix is needed.

    Two earlier points still stand:

    • The capacity estimate is only arithmetic from the code and is unconfirmed. Per-worker capacity is the size of /var/run/zilla/budgets0 divided by 64.
    • A replica of the benchmark setup on a small host (Kafka on the same host, 8,000 clients at 100/s) gave 0 closes, so I have not reproduced either case.

    Generated by Claude Code

  4. ankitk-me commented on Nov 19, 2026

    @ankitk-me
    ContributorAuthor

    Not reproduced on 2.7.2-rc2, on a single Docker Desktop host with Zilla -w 4. In 3 runs of 1,000 QoS 1
    publishers at 500 connections/s, and 1 run of 10,000 clients at 96/s held for 30 minutes, no client was disconnected
    after CONNACK.

  5. ankitk-me commented on Nov 19, 2026

    @ankitk-me
    ContributorAuthor

    Reran both cases on AWS with 2.7.2-rc2 (sha256:e197b1bb…). The setup is the same as above: c6i.8xlarge broker, start -e -w 31, separate Kafka 4.1.1 host, two c6i.2xlarge load generators, a fresh Zilla left idle for 8.5 minutes before the first client.

    10,000 clients at 100/s (emqtt-bench conn), held 10 minutes: not reproduced. All 10,000 connected in 109 s and were still established on the Zilla host at the end. Neither load generator logged an exit, and Zilla logged only the 10,000 connect events.

    100,000 publishers at 100/s (emqtt-bench pub, QoS 1, 1 msg/s each): reproduced, and this time Zilla logged something.

    • At 05:17:54, Zilla closed 3,090 client connections from one load generator within one second. The clients saw publish error - tcp_closed and EXIT for {shutdown,tcp_closed}. The other load generator lost none. This was at the end of the ramp, as on rc1 (3,134), and still close to one worker's share (100,000 / 31 ≈ 3,226).
    • In the same second, Zilla logged one Kafka produce error:
      2026-11-19T05:17:54.551211029Z south_kafka_client [19/Nov/2026:05:17:54 +0000] [1e000000000ab2d8d] BINDING_KAFKA_PRODUCE_ERROR Produce (Version: 3) Produce error (87) for topic (mqtt-devices).
      
      That was the only non-connect line in the log. On rc1 nothing was logged.
    • On the load generator that lost them, the closes come between the last ramp line (49,980 connected) and its first publish count. So they happened as that host's clients started publishing.
    • Zilla host at the end: 96,910 established connections (100,000 − 3,090).

    So it looks like one produce request rejected by Kafka closes every MQTT client whose publishes go through that Kafka produce stream. Error 87 is INVALID_RECORD. mqtt-devices uses cleanup.policy=delete, so a missing key isn't the reason; it points to a record batch Kafka considers malformed. Kafka logged nothing at INFO, and Produce v3 responses carry no error message. A rerun with Kafka's kafka.server.ReplicaManager logger at DEBUG is in progress to capture Kafka's reason, and I'll add it here.

    For the earlier local "not reproduced" note above: those runs had at most 10,000 clients, which matches the 10K result here.

  6. ankitk-me commented on Nov 19, 2026

    @ankitk-me
    ContributorAuthor

    Follow-up: a second run on a fresh deploy with the same setup reproduced it.

    • 3,154 clients from one load generator were closed in one second at the end of the ramp, none from the other. The Zilla host had 96,846 connections left.
    • In the same second Zilla logged:
      2026-11-19T05:56:50.013619223Z south_kafka_client [19/Nov/2026:05:56:49 +0000] [1e000000000ac8f2d] BINDING_KAFKA_PRODUCE_ERROR Produce (Version: 3) Produce error (87) for topic (mqtt-devices).
      

    So in both rc2 runs, one INVALID_RECORD produce error on mqtt-devices came with about 3,100 clients closed (3,090 and 3,154).

    Kafka's reason isn't in its log. With kafka.server.ReplicaManager at DEBUG, Kafka 4.1.1 logs each produce ("Produce to local log in N ms") but not why a batch was rejected. That reason is only returned in the produce response, as record_errors and error_message, from Produce v8 onward. Zilla sends Produce v3, which has neither field. Using v8 or later would let the BINDING_KAFKA_PRODUCE_ERROR event include Kafka's reason.

  7. jfallows commented on Nov 19, 2026

    @jfallows
    Contributor

    Cause found: the produce client appends records to a record batch whose request has already been encoded, so Kafka receives a batch that disagrees with its own record set. I reproduced both error codes locally and decoded the rejected requests.

    Reproduction. 2.7.2-rc2, examples/mqtt.kafka.proxy with -w 4 and 16-partition mqtt-devices and mqtt-messages, Kafka 4.1.1, emqtt-bench 0.6.3 QoS 2 publishers at 1 msg/s (-F 1). With 1,000 publishers, 843 were closed after about 2 minutes; with 3,000, 508 were closed after about 3.5 minutes. Each time Zilla logged one error 2 and one error 87 on mqtt-sessions. I captured the Zilla to Kafka traffic and decoded every Produce request against the record batch format (batch length, record count, last offset delta, record sizes, CRC32C). 165,726 requests were checked and exactly 2 were inconsistent, the 2 that Kafka rejected:

    • Error 87 (INVALID_RECORD): the request's record set is 23,261 bytes, but the batch header inside it declares a length of 31,949 bytes and 22 records, while only 16 records are present. The header was updated after the request had been encoded.
    • Error 2 (CORRUPT_MESSAGE): record set and batch length agree (15 records), but the batch CRC does not match its contents. Some bytes inside the batch were overwritten after the CRC was computed.

    Mechanism (KafkaClientProduceFactory, doEncodeRecordInit). A request carries the first batch in the encode slot, and it can wait there unsent when the network window is short. A record that arrives in that state is still added to the last batch in the slot, and the batch header (length, record count, last offset delta, max timestamp) is updated in place. The request header and the CRC were fixed when the request was encoded, so the broker sees a batch larger than its record set (87). When part of the request has already been sent, the slot is compacted to the left, but the saved position of the open batch is not moved with it. It then points into record data, and if those bytes look like a batch header (the #offsets records on mqtt-sessions contain runs of -1), the header update overwrites record bytes (2). More publishers on one produce stream make a short network window, and so this state, more likely. This also fits the 100,000-publisher runs: the first requests of a produce stream carry many records at once.

    Whole-stream effect. One rejected request resets the produce stream, and every MQTT publisher multiplexed on it is closed. That reaction is unchanged. One rejected batch therefore closes every publisher on that stream, which matches the roughly 3,100 closes in one second.

    Fix in progress. A batch is open for new records only after the end of the request already encoded from the slot, and its position follows the slot when it is compacted. The test is a spec scenario in which a second record arrives while the first request is encoded but unsent: on the current code the first request carries a 2-record batch header inside a 1-record record set. PR to follow.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions