Skip to content

feat(binding-mqtt): answer a failed mqtt 5 publish with a failure PUBACK or PUBREC - #3190

Merged
jfallows merged 4 commits into
fix/3166-mqtt-pending-ack-keysfrom
fix/3175-mqtt-v5-failure-publish-acks
Nov 13, 2026
Merged

jfallows merged 4 commits into
fix/3166-mqtt-pending-ack-keysfrom
fix/3175-mqtt-v5-failure-publish-acks

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

On MQTT 5 a PUBLISH that fails no longer closes the connection. A QoS 1 publish gets a PUBACK and a QoS 2 publish gets a PUBREC with a failure reason code, and the connection and the other packets in flight stay as they are. MQTT 3.1.1 is unchanged and still closes the connection. QoS 0 is unchanged.

Design and spec check: design comment and spec check comment.

What changes

Application protocol (mqtt.idl, shared). MqttFlushEx gains case 0: MqttPublishFlushEx { uint16 packetId; uint8 reasonCode } (the unused slot for MqttExtensionKind.PUBLISH), sent on the reply direction of a publish stream, with a builder and matcher in MqttFunctions. Justification in the protocol's own terms: today a publish stream can report success only cumulatively, through the WINDOW acknowledge, or end. There is no way to fail one message and leave the stream and its other messages alone, although MQTT 5.0 4.13.2 defines the failed acknowledgement as having "no consequences for other Sessions or other Packets flowing on the same Session". The contract: a message the receiving side cannot accept is reported by a FLUSH carrying its packetId and a reason code of 0x80 or greater; the stream stays open; the sender still acknowledges the message's bytes, so credit is returned exactly as for a successful message.

binding-mqtt (QoS 1 and 2)

  • An unroutable or unauthorized publish has no application stream. On v5, QoS 1 and 2, the server answers PUBACK or PUBREC 0x87, skips that packet's payload and keeps decoding. QoS 0 and MQTT 3.1.1 still close the connection as before.
  • A MqttPublishFlushEx on a publish stream sets the reason code recorded for that packet in the per-stream pending list added by fix(binding-mqtt): key pending acknowledgements per publish stream #3186; the PUBACK or PUBREC carries it when the stream's cumulative acknowledge reaches the packet. A failed QoS 2 packet id is not added to unreleasedPacketIds.
  • On a v3.1.1 connection the same flush closes the connection, because the application stream cannot know the protocol version.

binding-mqtt-kafka (QoS 1)

  • When Kafka resets the messages stream or the retained stream of a QoS 1 publish after it opened, the messages Kafka has not yet acknowledged are failed with the mapped reason code, the Kafka stream is replaced, and the mqtt stream stays open.
  • The failure flushes are sent when the replacement stream has opened (that is the point at which the window returning the credit of the failed bytes can follow). Messages that arrive while a Kafka stream is being replaced are failed with the same code, without buffering them.
  • A Kafka error carries no record identity (KafkaResetEx has only the error), so every message not yet acknowledged on that stream fails with the same code. Attribution to one record is a binding-kafka capability gap, tracked in binding-kafka: a failed produce stream reset does not identify the record that failed #3178.
  • A reset before the stream has opened, QoS 0 and QoS 2 keep the previous behavior (the mqtt stream is reset).

Decisions to review

  • Kafka errors 10 and 18 (record too large) map to 0x97 (Quota exceeded) in the acknowledgement form; every other Kafka error maps to 0x83. 0x95 (Packet too large) is not a PUBACK or PUBREC reason code (Tables 3-4 and 3-5, [MQTT-3.4.2-1], [MQTT-3.5.2-1]); it is a DISCONNECT code (3.14.2.1, 4.13.1). 0x97 is described as "An implementation or administrative imposed limit has been exceeded", which is what a broker record size limit is. The alternative is 0x83 ("The PUBLISH is valid but the receiver is not willing to accept it"). The existing mapping to 0x95, used when the mqtt stream is reset and the connection ends in DISCONNECT (QoS 0), is unchanged.
  • Failure acknowledgements are sent in publish order. [MQTT-4.6.0-2] and [MQTT-4.6.0-3] are worded for the Client; the specification does not require a Server to acknowledge in order. The order is a design choice, because the cumulative acknowledge of a publish stream answers its messages in order.
  • The Kafka error to reason code mapping for the acknowledgement form is a separate map from the reset mapping, so the QoS 0 behavior cannot change by accident.

MQTT statements checked

Read in the OASIS texts (MQTT 5.0 os and MQTT 3.1.1 os), clause ids as they appear:

  • 3.3.4 [MQTT-3.3.4-1] and Table 3-3 (QoS 0: no response, QoS 1: PUBACK, QoS 2: PUBREC)
  • 3.4.2.1 and 3.5.2.1, Tables 3-4 and 3-5: the permitted PUBACK and PUBREC reason codes (0x00, 0x10, 0x80, 0x83, 0x87, 0x90, 0x91, 0x97, 0x99) and the descriptions of 0x83, 0x87 and 0x97; [MQTT-3.4.2-1], [MQTT-3.5.2-1]
  • 4.13.2: acknowledgements with a reason code of 0x80 or greater have no consequences for other packets on the session; [MQTT-4.13.2-1] for CONNACK and DISCONNECT
  • 4.13.1: the Malformed Packet and Protocol Error codes (including 0x95) and [MQTT-4.13.1-1]
  • 4.3.2 [MQTT-4.3.2-4], [MQTT-4.3.2-5] and the sender may send further PUBLISH packets with other packet identifiers while waiting
  • 4.3.3 [MQTT-4.3.3-4] (PUBREL only after a PUBREC below 0x80), [MQTT-4.3.3-8], [MQTT-4.3.3-9] (a later PUBLISH with that packet id is a new message), [MQTT-4.3.3-10], and the packet id reuse text after a PUBREC of 0x80 or greater
  • 4.4 [MQTT-4.4.0-2] (a PUBACK or PUBREC of 0x80 or greater: the PUBLISH is treated as acknowledged and MUST NOT be retransmitted)
  • 4.6 [MQTT-4.6.0-2], [MQTT-4.6.0-3] (worded for the Client)
  • 3.14.2.1 DISCONNECT reason codes (0x83, 0x87, 0x95, 0x97 may be sent by a Server)
  • MQTT 3.1.1 3.3.5 [MQTT-3.3.5-2] (positive acknowledgement according to the normal QoS rules, or close the Network Connection) and 4.8 [MQTT-4.8.0-1], [MQTT-4.8.0-2]

For MQTT 5.0 nothing in the text requires a particular response to a QoS 0 publish a server cannot accept; closing with DISCONNECT (0x83, 0x87, 0x95, 0x97 are all listed) is allowed and is what happens today.

Every statement above was read by me in the OASIS texts; the reason code table on #3171 was not used as a source for any of them.

Test evidence

This PR is stacked on #3186 (head 4f75caa34). Test-first, in this order: test(binding-mqtt), test(binding-mqtt-kafka), feat(binding-mqtt), feat(binding-mqtt-kafka).

Failed first, against the unchanged production code (checked on this base, at the second test commit):

  • binding-mqtt, 5 v5 scenarios and 1 v3.1.1 scenario: publish.qos1.unroutable, publish.qos2.unroutable (DISCONNECT instead of PUBACK or PUBREC 0x87), publish.qos1.message.rejected, publish.qos2.message.rejected, publish.qos1.message.rejected.in.flight.acknowledged (PUBACK or PUBREC 0x00 instead of 0x83), and publish.qos1.message.rejected.close.connection (v3.1.1: the connection stays open).
  • binding-mqtt-kafka, 5 scenarios: publish.qos1.server.sent.reset, publish.qos1.server.sent.reset.in.flight, publish.qos1.server.sent.reset.after.acknowledged, publish.qos1.reject.large.message, publish.qos1.retained.server.sent.reset (the mqtt stream is aborted instead of the failure flush and a replacement Kafka stream).
  • The spec peer-to-peer ITs (NetworkIT, ApplicationIT, MqttIT, KafkaIT) and MqttFunctionsTest pass in the test commits.

After the feat(binding-mqtt) commit (the intermediate state): all binding-mqtt scenarios pass; of MqttKafkaPublishProxyIT (50 tests) only the 5 new scenarios fail, as expected, because they need feat(binding-mqtt-kafka).

After both feat commits, clean verify with checkstyle, license and notice checks on and the build cache skipped:

Module Unit tests ITs
specs/binding-mqtt.spec 78 (4 skipped) 499 (2 skipped)
specs/binding-mqtt-kafka.spec 27 260
runtime/binding-mqtt 44 407 (6 skipped)
runtime/binding-mqtt-kafka 10 148

0 failures, 0 errors. MqttKafkaSubscribeProxyIT.shouldReceiveServerSentRetainedReset did not flake in this run.

Observations to review

  • publish.qos1.message.rejected.in.flight.acknowledged was red on develop because PUBACK for packet 2 was sent before packet 1; it is green on the base of this PR (fix(binding-mqtt): key pending acknowledgements per publish stream #3186). I did not isolate why, and do not claim the cause.
  • Three mqtt-kafka scripts were corrected after they were first written, before this PR: the Kafka server holds its acknowledge in the scenarios that need a message to be unacknowledged when the reset arrives (the test Kafka server acknowledges every message as it reads it unless told otherwise), and two scenarios gained the replacement Kafka stream that the design requires. No script was changed to fit behavior the protocol does not allow. The v3.1.1 scripts were not changed.
  • The upstream build module fails its notice check when built from source in a fresh environment, unrelated to this change, so the upstream modules were installed without checks and the four modules above were verified with checks on.

Not delivered / follow-ups

  • QoS 2 failure in binding-mqtt-kafka is deferred to mqtt-kafka: a failed QoS 2 publish cannot be answered with a failure PUBREC (design) #3188 ("a failed QoS 2 publish cannot be answered with a failure PUBREC (design)"). Reason: onMqttData writes the INCOMPLETE offsets record (doCommitOffsetIncomplete) at the message's FIN, before Kafka acknowledges the produce, and records the packet id in the client's shared offset metadata (packetIds, partitions) that the session factory also uses. Failing a QoS 2 message needs a compensating commit and a rollback of that shared state, which is a design question for the exactly-once recovery state, not a mapping. So a v5 QoS 2 failure PUBREC exists in binding-mqtt (reported by the application with the flush) but binding-mqtt-kafka does not send it yet: a Kafka reset on a QoS 2 publish still resets the mqtt stream, and the connection ends as before.
  • Attribution of a Kafka error to a single record (binding-kafka: a failed produce stream reset does not identify the record that failed #3178).
  • The DISCONNECT 0x95 mapping for a record too large on QoS 0 is unchanged; whether 0x95 fits a broker record limit (its text refers to the Maximum Packet Size) is not decided here.
  • 0x90 (Topic Name invalid) and 0x91 (Packet identifier in use) acknowledgement forms are out of scope (mqtt: server acknowledgement reason codes (design) #3171 Q3, Q4).

Fixes #3175


Generated by Claude Code

… on a failed mqtt 5 publish

Add the mqtt publish flush extension (packetId, reasonCode) to the
application protocol with its builder and matcher, and the v5 scripts and
IT methods for an unroutable publish, a rejected message, a QoS 2 rejected
message and a rejected message with another QoS 1 message in flight, plus
the v3.1.1 script where a rejected message still closes the connection.

Against the unchanged runtime these fail: the server closes the connection
with DISCONNECT, or acknowledges with reason code 0x00.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVMP35CVUCUo9s4vpd
…gement when kafka resets a qos 1 publish

Add the mqtt and kafka scripts and IT methods for a Kafka reset that fails
the unacknowledged QoS 1 messages of a publish stream with the mapped
reason code while the mqtt stream stays open and the Kafka stream is
replaced: one message, two messages in flight, a message already
acknowledged by Kafka that keeps its success acknowledgement, and a
record too large mapped to packet too large.

Against the unchanged runtime the mqtt stream is aborted instead.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVMP35CVUCUo9s4vpd
…nowledgement

Answer an unroutable QoS 1 or QoS 2 publish with PUBACK or PUBREC 0x87 and
skip its payload, and apply a per-message failure reported by the
application on the publish stream to the PUBACK or PUBREC of that packet
without closing the connection. A rejected QoS 2 packet id is reusable.
MQTT 3.1.1 still closes the connection.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVMP35CVUCUo9s4vpd
… kafka resets a publish stream

When Kafka resets the messages or the retained stream of a QoS 1 publish
after it opened, report the messages Kafka has not yet acknowledged to
the mqtt binding as failed, with reason code 0x97 for a record that is too
large and 0x83 otherwise, replace the Kafka stream, and fail the messages
that arrive meanwhile. The mqtt stream stays open. QoS 0 and QoS 2 keep
resetting the stream.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVMP35CVUCUo9s4vpd
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 01:51
@jfallows
jfallows merged commit 223985e into develop Nov 13, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants