Skip to content

mqtt-kafka: Clean Start and session expiry do not discard the per-session offsets #3170

Closed

Description

@jfallows

Problem

Subscribe-side QoS 1 and 2 state and inbound QoS 2 packet ids are kept outside the main session record: in a per-session #offsets record on the sessions topic and in Kafka consumer-group offsets (<prefix>-<clientId>). By reading MqttKafkaSessionFactory and MqttKafkaSubscribeFactory on develop at b1a53e9 (approximate lines ~896 and ~1830-1850 in the session factory; not run):

  • At session expiry only the main session record is removed. The #offsets record and the consumer-group offsets survive, so a later connect with Clean Start=0 (or Clean Session=0) restores stale inbound QoS 2 packet ids and a stale backlog.
  • A connect with Clean Start=1 (or Clean Session=1) does not reset the consumer-group offsets or the #offsets record either, so a client that asked for a fresh session can still be sent messages from its previous session's backlog.

MQTT 5.0 section 3.1.2.4 and MQTT 3.1.1 section 3.1.2.4: with Clean Start (Clean Session) set to 1 the client and server discard any existing session and start a new one; a session that has expired is discarded the same way.

Sessions that never expire (Session Expiry Interval 0xFFFFFFFF, and 3.1.1 Clean Session=0 after the recent session persistence fix) make this matter more.

Expected

  • A Clean Start=1 (Clean Session=1) connect leaves no consumer-group offsets and no #offsets record from the previous session; the first subscribe fetch of the new session starts as for a new client.
  • Session expiry removes the #offsets record and the consumer-group offsets together with the main session record.
  • A Clean Start=0 connect to a live session is unchanged.

Test plan (test-first)

Scripts through mqtt-kafka (not a mocked application) for v5 and v4:

  • subscribe at QoS 1 and 2, receive and acknowledge part of the backlog, disconnect, reconnect with Clean Start=1: no replay of the old backlog, no stale packet ids, and the Kafka side shows the offsets reset;
  • the same, but with the session expiring instead of a Clean Start=1 reconnect;
  • Clean Start=0 reconnect to a live session still resumes from the committed offsets.

The first commit contains the failing scripts. If any part of the described behavior cannot be shown to fail, say which and why on this issue instead of changing code for it.

The implementation must follow how the module already creates, commits and resets consumer-group offsets and session records; if no established pattern fits for resetting group offsets, 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. Consumer groups remain the home of subscribe-side QoS state for now. Part of the MQTT conformance work under #512.

Activity

  1. jfallows commented on Nov 12, 2026

    @jfallows
    ContributorAuthor

    Findings so far on this issue, from scripts through mqtt-kafka against unchanged code at b1a53e9:

    • Session expiry leaves <clientId>#offsets behind: confirmed. The four expiry scenarios (session.close.expire.session.state, session.abort.expire.session.state, session.expiry.after.signal.stream.restart, session.ignore.non.fetch.signal.stream.flush) now expect a tombstone for <clientId>#offsets after the session tombstone. All four fail in MqttKafkaSessionProxyIT because that tombstone is never produced. The fix is a second tombstone in the expiry path.
    • Clean Start=1 and the #offsets record: not failing. A new scenario session.clean.start.discard.offsets serves a stale client#offsets record with packet ids 1 to 3 and producer 12345. A Clean Start=1 connect ignores it, initializes a new producer, and writes a fresh #offsets record with new offsets and no packet ids. It passes on unchanged code, so no code changes for this part.
    • Consumer-group offsets: not covered yet. Resetting them at expiry and on Clean Start=1 needs a binding-kafka capability that does not exist today (tracked separately in binding-kafka: add OffsetDelete (v0) wire codecs to the public api package #3173). That part follows once it lands.

    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