Repository navigation
Description
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
jfallows commentedon Nov 12, 2026
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
developat b1a53e9. Nothing has been run and no code or script is written yet.How a publish flows today (read in code)
MqttServerFactorykeeps ONE application publish stream per (topic, QoS) per connection (publishes, keyed bytopicKey(topic, qos);resolvePublishStream). Every message for that key goes on it as DATA frames (MqttDataEx.publishcarriespacketId,qos, ...) delimited by INIT/COMPLETE/FIN.MqttPublishStream.doPublishDatarecordsinitialSeq -> packetIdinunAckedReceivedQos1PacketIds/unAckedReceivedQos2PacketIds. Success is signalled only implicitly: the application's WINDOWacknowledgeadvances, andacknowledgePublishPacketsanswers every entry at or below it, in order, withdoEncodePubackV5/doEncodePubrecV5, both hard-coded to SUCCESS. QoS 2 additionally adds the id tounreleasedPacketIds.onPublishReset, reason code fromMqttResetEx), which runsonDecodeErrorand so closes the whole connection. If nothing routes,resolvePublishStreamitself callsonDecodeError(0x83).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.erroronly, no offset or record identity);MQTT_REASON_CODESmaps it (default 0x83, errors 10 and 18 to 0x95) anddoMqttResetresets the MQTT stream. Kafka acks are cumulative window acknowledges (QoS 2 additionally uses the offset-commit stream andKafkaMergedProduceFlushEx.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 inonSessionFlush. 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.reasonCodeandMqttTopicFilter.reasonCodeare the existing places MQTT reason codes travel between server and application.union MqttFlushExhas only subscribe (1) and session (2); case 0 (matchingMqttExtensionKind.PUBLISH = 0) is unused. So the closest established pattern is a packet-id-addressed FLUSH extension. Using it needs an IDL change tomqtt.idl, which is a shared change.Proposed signal (IDL change)
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
packetIdand a failurereasonCode(0x80 or greater); the stream stays open and other messages are unaffected. The application still advancesacknowledgepast 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, recordpacketId -> reasonCodefor the pending entry; when the cumulative acknowledge reaches that entry,acknowledgePublishPacketsencodes 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 tounreleasedPacketIds(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)
resolvePublishStreamreturns 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 tounreleasedPacketIds. 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
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 tracksend sequence -> packetIdper message, mirroring whatMqttServerFactorydoes. For QoS 2 a failed message writes no INCOMPLETE offset-commit record.KafkaResetExidentifies 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 neederrorand a record position onKafkaMergedProduceFlushEx(a second, kafka-side shared IDL change). I do not propose it in this issue; it would be a follow-up.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
fix/3164-mqtt-ack-failure-reason-codes, read-only): its code change is in the subscribe direction only (onDecodePrecgains the received reason code, and a PUBREC at 0x80 or greater completes the QoS 2 flow). It does not touchacknowledgePublishPackets, the pending-ack maps oronPublishReset. Overlap with this work is textual inMqttServerFactoryonly.packetId -> reasonCode, applied where the entry is answered). I will not write that part until I have read it and rebased onto the stack.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.MqttServerIT(binding-mqtt),NetworkIT/ApplicationIT(spec),MqttKafkaPublishProxyITand its spec ITs;MqttFunctionsTestfor the new builder and matcher. Run withverify; modules touched: both specs and runtimes, plus the full build after the .idl change.[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
MqttPublishFlushExas case 0 ofMqttFlushEx(shared IDL) is acceptable as the mirrored pattern.Generated by Claude Code
jfallows commentedon Nov 13, 2026
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
MQTT 3.1.1
Where the accepted design must change
One place: 0x95 cannot be sent in a PUBACK or PUBREC.
MQTT_REASON_CODESin 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. Mypublish.qos1.reject.large.messagescenario 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