Skip to content

mqtt-kafka: zero-byte retained PUBLISH does not remove the retained message #3132

Closed

Description

@jfallows

Problem

MqttKafkaPublishFactory (lines ~601-631, develop at 1e802cb) writes the retained-topic record for a PUBLISH with RETAIN=1 using the payload exactly as received. For a zero-length payload that is an empty value, not a Kafka tombstone (null value). The retained topic is log-compacted and keyed by the MQTT topic name, so compaction keeps the empty value as the latest record for the key instead of deleting it, and a later subscriber's retained replay can be sent a zero-length message. (The produce path in binding-kafka distinguishes null from empty values; the first step is a test that confirms the record written today.)

MQTT 5.0 section 3.3.1.3 and [MQTT-3.3.1-7]: a retained message with a payload of zero bytes MUST NOT be stored as a retained message, and an existing retained message for that topic is removed. MQTT 3.1.1 [MQTT-3.3.1-10] and [MQTT-3.3.1-11] say the same.

Expected

  • A PUBLISH with RETAIN=1 and a zero-byte payload writes a tombstone (null value) keyed by the topic to the retained topic.
  • It is still delivered as a normal message, with a zero-length payload and the usual RETAIN rules for live delivery, to live subscribers through the messages topic.
  • Retained replay never delivers a zero-length message, including for empty-valued records already present in an existing retained topic.

Test plan (test-first)

Spec scripts for v4 and v5 on both sides of mqtt-kafka:

  • publish retained then publish retained with empty payload: the Kafka script asserts a null value on the retained topic and the normal record on the messages topic;
  • a subscriber after the removal receives no retained message;
  • retained replay skips an empty-valued record.

Today only the messages-topic variant of the empty publish has a script, so the retained-topic behavior is untested. The first commit contains the failing scripts.

Notes

Relies on the existing design of a compacted retained topic keyed by the MQTT topic name. Found by a clause-by-clause conformance audit, by reading the code. Part of the MQTT conformance work under #512.

Activity

  1. self-assigned this
    on Nov 12, 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