Skip to content

mqtt: a failed PUBLISH on MQTT 5 should get a failure PUBACK or PUBREC, not close the connection #3175

Closed

Description

@jfallows

Problem

When a PUBLISH cannot be accepted on MQTT 5, the server sends DISCONNECT with reason code 0x83 and closes the connection, and an unauthorized publish is reported as 0x83, never 0x87 (Not authorized). onPublishReset and resolvePublishStream in MqttServerFactory end in onDecodeError; the acknowledgements the server encodes always carry reason code 0x00 (doEncodePubackV5, doEncodePubrecV5). The Kafka-side mapping only maps two Kafka errors to a specific code (0x95); every other error becomes 0x83 (read on develop at b1a53e9; the full analysis is the table posted on #3171).

Closing the connection drops every other in-flight packet on it. MQTT 5.0 section 4.13.2: an acknowledgement with a reason code of 0x80 or greater indicates that the received packet was in error, with no consequences for other Sessions or other Packets flowing on the same Session. Section 3.4.2.1 (PUBACK) and 3.5.2.1 (PUBREC) define 0x80, 0x83, 0x87, 0x90, 0x91, 0x97 and 0x99 for this. Only 0x99 is explicitly allowed either way, so neither form is mandated; the decision (recorded on #3171) is to use the acknowledgement form for MQTT 5.

Expected

  • A QoS 1 PUBLISH that fails gets a PUBACK, and a QoS 2 PUBLISH that fails gets a PUBREC, with a failure reason code (0x83 for errors, 0x87 for an unauthorized or unroutable publish, 0x99 as today for an invalid payload format), and the connection stays open.
  • MQTT 3.1.1 is unchanged (it has no reason codes and closes the connection).
  • QoS 0 is handled as the design decides: it has no acknowledgement, so the form is decided in the design step below.

Design first

How the application stream tells the MQTT server that one message failed, as opposed to the whole topic stream, is not defined today (a failed publish ends the stream). Before any code is written, post a short design on this issue: where the per-message failure is signalled in the application protocol (an existing extension, or a change to the IDL, which makes it a shared change), how the mqtt-kafka publish proxy maps a Kafka error to it, what happens to the other messages in flight on the same publish stream, and what QoS 0 does. The pattern to mirror must be an existing implementation in the same modules; if none fits, ask before inventing one.

Test plan (test-first)

After the design is accepted: scripts for v5 in specs/binding-mqtt.spec and specs/binding-mqtt-kafka.spec for an unauthorized publish, a routed-but-rejected publish (Kafka error), a QoS 2 failure, and a failing publish with another QoS 1 message in flight that still gets its PUBACK; the v3.1.1 scripts stay as they are. The first commit contains the failing scripts.

Notes

Found by a clause-by-clause audit of MQTT 3.1.1 and 5.0 conformance and the reason code table on #3171. Part of the MQTT conformance work under #512.

Activity

  1. jfallows commented on Nov 12, 2026

    @jfallows
    ContributorAuthor

    Design: per-message publish failure on MQTT 5

    Decision taken (recorded on #3171): on MQTT 5 a failed PUBLISH gets a failure PUBACK (QoS 1) or PUBREC (QoS 2) and the connection stays open; MQTT 3.1.1 is unchanged. Read on develop at b1a53e9. Nothing has been run and no code or script is written yet.

    How a publish flows today (read in code)

    • MqttServerFactory keeps ONE application publish stream per (topic, QoS) per connection (publishes, keyed by topicKey(topic, qos); resolvePublishStream). Every message for that key goes on it as DATA frames (MqttDataEx.publish carries packetId, qos, ...) delimited by INIT/COMPLETE/FIN.
    • On the FIN of a message, MqttPublishStream.doPublishData records initialSeq -> packetId in unAckedReceivedQos1PacketIds / unAckedReceivedQos2PacketIds. Success is signalled only implicitly: the application's WINDOW acknowledge advances, and acknowledgePublishPackets answers every entry at or below it, in order, with doEncodePubackV5 / doEncodePubrecV5, both hard-coded to SUCCESS. QoS 2 additionally adds the id to unreleasedPacketIds.
    • Failure is signalled only by RESET (onPublishReset, reason code from MqttResetEx), which runs onDecodeError and so closes the whole connection. If nothing routes, resolvePublishStream itself calls onDecodeError(0x83).
    • mqtt-kafka (MqttKafkaPublishFactory): all messages of the stream go over one Kafka merged-produce stream (plus one for retained). A Kafka error arrives as a RESET of that Kafka stream (KafkaResetEx.error only, no offset or record identity); MQTT_REASON_CODES maps it (default 0x83, errors 10 and 18 to 0x95) and doMqttReset resets the MQTT stream. Kafka acks are cumulative window acknowledges (QoS 2 additionally uses the offset-commit stream and KafkaMergedProduceFlushEx.partitionId).

    The established mechanism to mirror, and what is missing

    No existing frame fails ONE message and leaves the stream open, with one partial exception:

    • MqttSessionFlushEx { packetId }, application to server on the session reply stream, drives the per-packet-id PUBCOMP in onSessionFlush. It is per-message on a shared stream and carries no failure.
    • MqttSubscribeFlushEx { qos, packetId, state } is the server to application per-packet-id ack on subscribe.
    • KafkaProduceFlushEx { error, ackOffset } (kafka cache produce) is a per-message error FLUSH, but its consumer (onMessageError) turns it into a stream RESET, so it does not help either.
    • MqttResetEx.reasonCode and MqttTopicFilter.reasonCode are the existing places MQTT reason codes travel between server and application.

    union MqttFlushEx has only subscribe (1) and session (2); case 0 (matching MqttExtensionKind.PUBLISH = 0) is unused. So the closest established pattern is a packet-id-addressed FLUSH extension. Using it needs an IDL change to mqtt.idl, which is a shared change.

    Proposed signal (IDL change)

    case 0: MqttPublishFlushEx publish;
    
    struct MqttPublishFlushEx extends MqttFlushEx   // fields as the sibling kinds
    {
        uint16 packetId = 0;
        uint8 reasonCode = 0;
    }
    

    Sent on the publish stream's reply direction (application to server). Contract for the publish stream: a message the application cannot accept is reported by a FLUSH carrying that message's packetId and a failure reasonCode (0x80 or greater); the stream stays open and other messages are unaffected. The application still advances acknowledge past the failed message's bytes, so flow-control credit is returned exactly as for a successful message (credit is not special-cased). The FLUSH must precede the WINDOW that acknowledges those bytes (ordered stream, so no race).

    Justification for the shared IDL, in terms of the publish stream's own contract: today the stream can report only cumulative success or terminate. It cannot express "this message failed" even though MQTT 5.0 4.13.2 defines the failed acknowledgement as having no consequences for other packets on the same Session.

    Server handling (MqttServerFactory, v5 only): on the FLUSH, record packetId -> reasonCode for the pending entry; when the cumulative acknowledge reaches that entry, acknowledgePublishPackets encodes the PUBACK/PUBREC with that reason code instead of SUCCESS. Answering at the cumulative position (rather than immediately) keeps acknowledgements in the order of the PUBLISH packets, the same order as today. Other entries on the stream are answered with SUCCESS as before. A failed QoS 2 message is not added to unreleasedPacketIds (4.3.3: the id is reusable after a PUBREC of 0x80 or greater, and no PUBREL follows). v3.1.1 never receives or acts on this FLUSH (the server never sends v3.1.1 a failure ack; a RESET still closes as today).

    Unauthorized or unroutable publish (no application stream)

    resolvePublishStream returns before any stream exists, so there is nothing to signal on. For v5 QoS 1/2 the server answers directly with PUBACK/PUBREC 0x87, discards that packet's payload (fragments included) with a decode state that skips the rest of the packet, and keeps the connection and decoder running; no entry is added to unreleasedPacketIds. Zilla cannot today distinguish "guard denied" from "no route" (both give no resolved binding), so both report 0x87 (issue text). Invalid payload format (0x99) keeps working as a DISCONNECT today; moving it to the acknowledgement form is optional and not planned here. 0x90 is out of scope (#3171 Q3).

    mqtt-kafka mapping

    • An unrouted/unauthorized publish is handled in the MQTT server as above; mqtt-kafka never sees it.
    • A Kafka error: on RESET of the Kafka merged stream, mqtt-kafka no longer resets the MQTT stream for QoS 1/2 on v5. It sends MqttPublishFlushEx(packetId, MQTT_REASON_CODES.get(error)) for every message not yet acknowledged to MQTT on that stream, then opens a new Kafka stream when the next message arrives. Messages Kafka had already acknowledged (cumulative acknowledge reached) keep their success PUBACK. To do this mqtt-kafka tracks end sequence -> packetId per message, mirroring what MqttServerFactory does. For QoS 2 a failed message writes no INCOMPLETE offset-commit record.
    • Limitation, stated plainly: KafkaResetEx identifies the error but not the record, so mqtt-kafka cannot tell which unacknowledged message caused it. All messages not yet acknowledged by Kafka on that Kafka stream fail with the same code (conservative: a failure acknowledgement is allowed for a message that may have been stored, and the client retries per its QoS). Attributing to the single offending record would need error and a record position on KafkaMergedProduceFlushEx (a second, kafka-side shared IDL change). I do not propose it in this issue; it would be a follow-up.
    • A message fragmented across several DATA frames when the Kafka stream fails: the remaining fragments are discarded until its FIN, and the message is failed as above.
    • Reason codes keep the existing map (0x95 for 10/18, otherwise 0x83); no new codes are invented.

    Other messages in flight on the same stream

    They are unaffected: the failure addresses one packetId; later messages on the stream keep being forwarded and are answered at their cumulative position. The one exception is the mqtt-kafka limitation above (Kafka-side unacknowledged messages share the Kafka error). A QoS 1 and a QoS 2 stream for the same topic are separate streams, so an error on one does not touch the other.

    QoS 0

    No acknowledgement exists, so no failure acknowledgement can be sent. Proposal: unchanged. A failed QoS 0 publish keeps ending the stream via RESET and the connection is closed with DISCONNECT as today, because silently dropping it would be indistinguishable from success to the client. I have not been able to verify the MQTT 5.0 clause on a Server that does not authorize a PUBLISH (3.3.4/3.3.5 area) from the spec text, so this stays proposed until checked; I will quote it in the PR. Decision needed if you would rather drop QoS 0 silently.

    Interaction with other open work

    Test plan (test-first; first commit is the failing scripts and IT methods only)

    Scripts, paired client/server, v5, one IT method per scenario:

    • specs/binding-mqtt.spec: network and application scripts for (1) unauthorized/unroutable publish, QoS 1 gets PUBACK 0x87 and the connection stays open (a later publish on the same connection still succeeds); (2) the same for QoS 2, PUBREC 0x87 and the id is reusable; (3) application FLUSH failure for one QoS 1 message gets PUBACK 0x83 (and a distinct code such as 0x95 as a second value) with the stream open; (4) QoS 2 failure FLUSH gets PUBREC failure and no PUBREL expectations; (5) a failing QoS 1 message with another QoS 1 message in flight, which still gets its PUBACK 0x00, in publish order.
    • specs/binding-mqtt-kafka.spec: (6) Kafka reset while a QoS 1 message is unacknowledged gives the failure FLUSH with the mapped code, and a following publish opens a fresh Kafka stream; (7) QoS 2 variant; (8) an earlier message already acknowledged by Kafka keeps PUBACK 0x00 while the later one fails.
    • The v3.1.1 scripts are unchanged and must stay green.
    • ITs: MqttServerIT (binding-mqtt), NetworkIT/ApplicationIT (spec), MqttKafkaPublishProxyIT and its spec ITs; MqttFunctionsTest for the new builder and matcher. Run with verify; modules touched: both specs and runtimes, plus the full build after the .idl change.
    • Checked against the spec so far from fetched text: 3.3.4 [MQTT-3.3.4-1], [MQTT-3.4.2-1], [MQTT-3.5.2-1], 2.2.1 packet id reuse after a PUBREC of 128 or greater. Not yet checked myself: 4.3.2/4.3.3 text, [MQTT-4.6.0-x] ordering and the unauthorized-PUBLISH clause for QoS 0; I will check them before writing the PR description.

    Decisions I need

    1. Confirm adding MqttPublishFlushEx as case 0 of MqttFlushEx (shared IDL) is acceptable as the mirrored pattern.
    2. Confirm the conservative mqtt-kafka semantics (all Kafka-unacknowledged messages share the error) for this issue, with record-level attribution as a follow-up.
    3. Confirm QoS 0 stays unchanged.

    Generated by Claude Code

  2. jfallows commented on Nov 13, 2026

    @jfallows
    ContributorAuthor

    Spec check for the design above

    I read the full OASIS texts (MQTT 5.0 os and MQTT 3.1.1 os, downloaded as HTML and converted to text) and checked each MQTT statement the design and the tests rely on. Clause ids are as they appear.

    MQTT 5.0

    • 3.3.4 PUBLISH Actions: "The receiver of a PUBLISH Packet MUST respond with the packet as determined by the QoS in the PUBLISH Packet [MQTT-3.3.4-1]"; Table 3-3: QoS 0 expected response is "None", QoS 1 PUBACK, QoS 2 PUBREC.
    • 3.4.2.1 / 3.5.2.1 (Tables 3-4 and 3-5): the PUBACK and PUBREC reason codes are 0x00, 0x10, 0x80, 0x83, 0x87, 0x90, 0x91, 0x97, 0x99, and the sender "MUST use one of the PUBACK Reason Codes [MQTT-3.4.2-1]" / "one of the PUBREC Reason Code values [MQTT-3.5.2-1]". 0x83 is "The PUBLISH is valid but the receiver is not willing to accept it", 0x87 is "The PUBLISH is not authorized", 0x97 is "An implementation or administrative imposed limit has been exceeded".
    • 4.13.2 Other errors: acknowledgements with a Reason Code of 0x80 or greater "indicate that the received packet, identified by a Packet Identifier, was in error. There are no consequences for other Sessions or other Packets flowing on the same Session." For CONNACK and DISCONNECT the connection "MUST be closed whether or not the CONNACK or DISCONNECT is sent [MQTT-4.13.2-1]".
    • 4.13.1: lists the Malformed Packet and Protocol Error codes (0x81, 0x82, 0x93, 0x95, 0x9A, 0x9B, 0x9E, 0xA1, 0xA2); a Server detecting one "MUST close the Network Connection [MQTT-4.13.1-1]" and SHOULD send DISCONNECT.
    • 4.3.2 QoS 1: the receiver "MUST respond with a PUBACK ... having accepted ownership [MQTT-4.3.2-4]"; after it, an incoming PUBLISH with the same Packet Identifier is a new message [MQTT-4.3.2-5]; "a sender is permitted to send further PUBLISH packets with different Packet Identifiers while it is waiting to receive acknowledgements".
    • 4.3.3 QoS 2: the sender sends PUBREL only for a PUBREC "with a Reason Code value less than 0x80 [MQTT-4.3.3-4]"; the receiver "MUST respond with a PUBREC" [MQTT-4.3.3-8]; after a PUBREC of 0x80 or greater a later PUBLISH with that id "as being a new Application Message [MQTT-4.3.3-9]"; the id is reusable after "a PUBREC with a Reason Code of 0x80 or greater". So a failed QoS 2 id gets no PUBREL and is not kept as an unreleased id.
    • 4.4: a PUBACK or PUBREC of 0x80 or greater means the PUBLISH "is treated as acknowledged, and MUST NOT be retransmitted [MQTT-4.4.0-2]".
    • 4.6 Message ordering: the rules [MQTT-4.6.0-2] and [MQTT-4.6.0-3] (PUBACK and PUBREC in the order of the PUBLISH packets) are worded for the Client only. The spec does not literally require a Server to acknowledge in order. Sending the failure acknowledgement at the failed message's position in publish order is therefore a design choice for correctness and predictability, not a requirement; the in-flight test expects that order for that reason.
    • 3.14.2.1 DISCONNECT reason codes: a Server may send 0x83, 0x87, 0x95, 0x97 (and 0x80, 0x90, 0x99) in DISCONNECT. For QoS 0 nothing in the text requires a particular response; closing with DISCONNECT is allowed, and no clause requires or forbids dropping silently.

    MQTT 3.1.1

    • 3.3.5: "If a Server implementation does not authorize a PUBLISH to be performed by a Client; it has no way of informing that Client. It MUST either make a positive acknowledgement, according to the normal QoS rules, or close the Network Connection [MQTT-3.3.5-2]."
    • 4.8: a protocol violation [MQTT-4.8.0-1] or a Transient Error [MQTT-4.8.0-2] means the Server "MUST close the Network Connection".
    • So for v3.1.1 closing the connection (unchanged) is what the text requires when it will not acknowledge.

    Where the accepted design must change

    One place: 0x95 cannot be sent in a PUBACK or PUBREC. MQTT_REASON_CODES in the mqtt-kafka publish proxy maps Kafka errors 10 and 18 (record too large) to 0x95, which is in the Table 3-10 DISCONNECT list but is not one of the PUBACK or PUBREC reason codes, so [MQTT-3.4.2-1] and [MQTT-3.5.2-1] forbid it there. My publish.qos1.reject.large.message scenario expected PUBACK 0x95, which disagrees with the spec. Proposed change: the acknowledgement form maps those two Kafka errors to a code from Tables 3-4/3-5, and every other Kafka error stays 0x83. The choice is between 0x97 (a limit has been exceeded, which is what a broker record size limit is) and 0x83 (valid but not accepted). Decision requested; I will not change the script or code until it is made. The QoS 0 path (RESET, then DISCONNECT) keeps its existing mapping and is not touched here.

    Everything else in the design stands as posted: failure acknowledgement for QoS 1/2 on v5 ([MQTT-4.13.2], Tables 3-4/3-5), 0x87 for an unroutable or unauthorized publish (Table 3-4: "The PUBLISH is not authorized"), a failed QoS 2 id not kept as unreleased ([MQTT-4.3.3-4], [MQTT-4.3.3-9]), and v3.1.1 closing the connection ([MQTT-3.3.5-2], [MQTT-4.8.0-2]).


    Generated by Claude Code

  3. 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