Skip to content

mqtt-kafka: messages published between SUBSCRIBE and the first offset commit can be lost for an offline QoS 1 or 2 subscriber #3169

Closed as not planned

Description

@jfallows

Problem

For a QoS 1 or 2 subscription, delivery state is the Kafka consumer group's committed offset (<prefix>-<clientId>), and the subscribe fetch starts at the live end of the topic (MqttKafkaSubscribeFactory, line numbers on develop at b1a53e9 not given; found by reading, not run). If a client subscribes, disconnects, and a message is published before the first offset has been committed for that group, the group has no committed position, so on resume the fetch starts at the live end and the message published in that window is never delivered.

MQTT 5.0 section 4.1.1 and MQTT 3.1.1 section 4.1.1: the server's session state includes the QoS 1 and 2 messages pending transmission to the client, so a message published after a subscription exists must be delivered when the client resumes (within the session expiry and the topic's retention).

Expected

  • Once a QoS 1 or 2 SUBSCRIBE has been acknowledged, a message published afterwards to a matching topic is delivered when the client reconnects with its session, even if no offset was ever committed for that subscription before the client went offline.
  • Subscriptions that already have a committed offset behave as today.

Test plan (test-first)

The first step is to show the loss: a script through mqtt-kafka with QoS 1 (and QoS 2) SUBSCRIBE, a disconnect before any message was acknowledged, a Kafka record published for the topic, and a reconnect with the session, expecting the record to be delivered. The first commit contains the failing scripts. If the loss cannot be shown, stop and record the analysis on this issue instead of changing code.

The intended fix is to establish the group's starting offset when the subscription is made, not at the first acknowledgement; the pattern to mirror is how the module already commits and resumes consumer-group offsets for acknowledged messages, and the implementation must follow it rather than add a new mechanism. If no established pattern fits, ask before inventing one.

Notes

Found by a clause-by-clause audit of MQTT 3.1.1 and 5.0 conformance, by reading the code. Clean Start not resetting these consumer-group offsets is a separate question (decision pending). Part of the MQTT conformance work under #512.

Activity

  1. jfallows commented on Nov 12, 2026

    @jfallows
    ContributorAuthor

    Analysis: the loss cannot be shown through mqtt-kafka, and the code suggests the premise does not hold

    I read the code at b1a53e98f (develop) and did not run a reproduction. No production code was changed.

    What mqtt-kafka does. For a QoS 1 or 2 subscription, newStream(...) in MqttKafkaSubscribeFactory sends a merged BEGIN with groupId = <prefix>-<clientId> and a single partition entry partitionOffset = KafkaOffsetType.LIVE (-1). LIVE is only the default for partitions that the group has no position for. The module does not choose the resume position. It issues consumer flushes with progress(partitionOffset + 1) after acknowledgement, and it reads committed offsets and metadata back from the merged reply BEGIN.

    What the merged binding does with a group. In KafkaMergedFactory.onTopicOffsetFetchDataChanged, each partition offset returned by the group's offset fetch is stored with p.partitionOffset() == LIVE.value() ? HISTORICAL.value() : p.partitionOffset(). Kafka's OffsetFetch returns -1 for a partition with no committed offset, which equals LIVE. nextFetchPartitionOffset then prefers offsetsByPartitionId over defaultOffset when it is non-empty. So for a group with no committed offset the fetch starts at HISTORICAL, the earliest retained record, not at the live end.

    If that reading is right, a record published between SUBSCRIBE and the first commit is still delivered on resume. The suspected loss would only occur if the fetch started at the live end, and with a group it does not.

    Why I did not write a failing script. A script through mqtt-kafka runs against a scripted Kafka peer, so it only sees the BEGIN that mqtt-kafka sends and the offsets the peer chooses to return. The behaviour in the issue lives below mqtt-kafka, in the KafkaMergedFactory group and offset-fetch path. A scripted peer could hand back whatever offset I want, which would only demonstrate my assumption. A failing script would have to bend a script to fit an implementation detail, which AGENTS.md rules out.

    Related behaviour, outside this issue and not changed here. The same mapping means a first-time QoS 1 or 2 subscriber to a group with no commit reads from the earliest retained record, so it is delivered history that predates its SUBSCRIBE. That may deserve its own issue; I have not confirmed it end to end.

    Still open. A real Kafka broker would settle this: subscribe at QoS 1 with a new client id, disconnect, publish, reconnect with the session, and check that the record arrives. I did not run that. If it shows a loss, the fix would be in the kafka merged and group path, not in MqttKafkaSubscribeFactory, and it would need a separate design decision.


    Generated by Claude Code

  2. self-assigned this
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions