Skip to content

fix(binding-mqtt): end the qos 2 flow on a pubrec failure reason code - #3176

Merged
jfallows merged 2 commits into
developfrom
fix/3164-mqtt-ack-failure-reason-codes
Nov 13, 2026
Merged

jfallows merged 2 commits into
developfrom
fix/3164-mqtt-ack-failure-reason-codes

Conversation

@jfallows

@jfallows jfallows commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

A PUBREC from an MQTT 5.0 client with a reason code of 0x80 or above was handled like a successful PUBREC. MqttServerFactory.onDecodePubrec ignored the reason code and always flushed state=INCOMPLETE to the application stream. With mqtt-kafka that is acked, the server sends a PUBREL, and the stream waits for a PUBCOMP the client will never send.

What changed:

  • decodePubrecV5 reads the PUBREC reason code (an omitted reason code, remaining length 2, is success, per 3.5.2.1) and passes it to onDecodePubrec.
  • For 0x80 or above, onDecodePubrec completes the message the way onDecodePubcomp does: the packet id is removed from qos2Subscribes, the window is granted and a COMPLETE flush goes to the application. No PUBREL is sent and no INCOMPLETE state is persisted. This mirrors the existing PUBCOMP path; there is no new flag and no IDL change, and the decision stays in the MQTT server, which already owned it.
  • decodePubrecV4 passes success, so MQTT 3.1.1 behavior is unchanged.
  • PUBACK with 0x80 or above already completed the message like a success (same flush, offset committed, nothing retried), which is the specified outcome ([MQTT-4.4.0-2], below). It needed no code change; a script now pins it.

Fixes #3164

This PR is based on develop (5556779, which includes #3159 to #3162). It was rebased onto it from the #3162 branch with no conflicts and nothing changed beyond the mechanical rebase.

Spec statements

Zilla is the sender of the PUBLISH to a subscriber here, and the client is the receiver that sends the PUBREC or PUBACK. All quotes are from the OASIS text, which I downloaded in full and read as text (MQTT 5.0 os: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html; MQTT 3.1.1 os: https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html). Clause ids are as they appear.

MQTT 5.0:

  • 4.3.3 QoS 2, sender. The sender "MUST send a PUBREL packet when it receives a PUBREC packet from the receiver with a Reason Code value less than 0x80. This PUBREL packet MUST contain the same Packet Identifier as the original PUBLISH packet [MQTT-4.3.3-4]." The same section: "The Packet Identifier becomes available for reuse once the sender has received the PUBCOMP packet or a PUBREC with a Reason Code of 0x80 or greater."
  • 4.4 Message delivery retry. "If PUBACK or PUBREC is received containing a Reason Code of 0x80 or greater the corresponding PUBLISH packet is treated as acknowledged, and MUST NOT be retransmitted [MQTT-4.4.0-2]." The same section: Clients and Servers "MUST NOT resend messages at any other time [MQTT-4.4.0-1]" than reconnect with Clean Start 0 and a session present, where they resend unacknowledged PUBLISH and PUBREL packets.
  • 2.2.1 Packet Identifier. It becomes available for reuse after the sender has processed the corresponding acknowledgement: "In the case of a QoS 1 PUBLISH, this is the corresponding PUBACK; in the case of QoS 2 PUBLISH it is PUBCOMP or a PUBREC with a Reason Code of 128 or greater."
  • 4.13.2 Other errors. "Acknowledgment packets PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK 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."
  • 3.5.2.1 PUBREC Reason Code and 3.4.2.1 PUBACK Reason Code: "If the Remaining Length is 2, then the Publish Reason Code has the value 0x00 (Success)" (PUBREC), and for PUBACK "there is no Reason Code and the value of 0x00 (Success) is used". 0x80 is "Unspecified error": "The receiver does not accept the publish but either does not want to reveal the reason, or it does not match one of the other values."

How they map to the change:

  • [MQTT-4.3.3-4] requires a PUBREL for a PUBREC below 0x80. For a PUBREC of 0x80 or above the exchange is over: the PUBLISH is acknowledged ([MQTT-4.4.0-2]) and the identifier is free (4.3.3, 2.2.1), so the server completes the message and sends no PUBREL. Note that [MQTT-4.3.3-4] states the PUBREL rule for reason codes below 0x80; that none is sent at 0x80 or above is my inference from it together with the sentences above, not a sentence I found that says "MUST NOT send PUBREL".
  • [MQTT-4.4.0-2] is why a PUBACK or PUBREC failure completes the message downstream (the Kafka offset is committed) instead of leaving it to be redelivered.
  • 4.13.2 is why the failure is handled for that packet identifier only, with the stream and the other in-flight packets continuing; the new scripts deliver a further message on the same stream after each failure.

[MQTT-4.3.3-9] (a later PUBLISH with that packet identifier is a new Application Message after the receiver has sent a PUBREC of 0x80 or above) is a receiver rule, so it does not apply to this change, where the server is the sender. An earlier version of this description cited it for the sender; that was wrong.

MQTT 3.1.1 (4.3.3 and 4.4): the PUBREC has only the Packet Identifier in its variable header and no payload (3.5.2, 3.5.3), so there is no reason code. The sender "MUST send a PUBREL packet when it receives a PUBREC packet from the receiver" with no qualification, and the identifier "becomes available for reuse once the Sender has received the PUBCOMP Packet". 4.4 requires resending unacknowledged PUBLISH and PUBREL packets only when a client reconnects with CleanSession 0. The 3.1.1 text has no equivalent of [MQTT-4.4.0-2], so the 3.1.1 path is unchanged: every PUBREC is a success PUBREC.

Test evidence

Test-first: the first commit holds the scripts and IT methods only. Against the unchanged production code (that commit alone, on develop 5556779), the new runtime SubscribeIT.shouldReceiveMessageQoS2AfterPubrecFailureReasonCodes failed (the server flushed INCOMPLETE where the script expects a plain flush; the retry timed out). shouldReceiveMessageQoS1AfterPubackFailureReasonCode passed, as expected. The spec peer-to-peer ITs for both scripts passed (network v5, network v4 and application SubscribeIT, 130 tests), so the scripts agree with each other.

After the fix, on this branch on develop 5556779, clean verify with checkstyle and license checks on, build cache skipped (-Dmaven.build.cache.skipCache=true), upstream modules installed with -am:

  • specs/binding-mqtt.spec: 469 ITs, 0 failures, 2 skipped (+ 75 unit and schema tests, 4 skipped)
  • runtime/binding-mqtt: 386 ITs, 0 failures, 6 skipped (+ 44 unit tests)

The mqtt-kafka modules are not touched and were not run.

Scripts added (network v5 and application, client and server each):

  • subscribe.receive.message.qos2.pubrec.failure.reason.codes: PUBREC 0x80 and PUBREC 0x97 each end the flow with no PUBREL, and a third message then completes the normal PUBREC, PUBREL, PUBCOMP exchange on the same stream.
  • subscribe.receive.message.qos1.puback.failure.reason.code: PUBACK 0x80, then a second message is delivered and acknowledged.

CI: no check runs had been reported for this head when this was written; the local runs above are the evidence until they are.

Not delivered

  • Observability: neither case is observable. The binding's event mechanism (MqttEventType) only has CLIENT_CONNECTED, so no existing event fits and I did not add a new event type, as the issue asks to raise that first.
  • The failure reason code is not carried to the application stream (the flush has no reason code field). Nothing downstream consumes it today.
  • A PUBREC or PUBACK for a packet identifier the server is not tracking is not handled: onDecodePubrec and onDecodePuback dereference the qos2Subscribes or qos1Subscribes lookup without a null check, as the success paths already did. I did not change this.
  • Other reason-code handling was not audited here; the reason code table on mqtt: server acknowledgement reason codes (design) #3171 covers that.

Follow-ups

  • Decide whether to add a PUBLISH-acknowledgement-failed event and which fields it carries.
  • Decide how an unknown packet identifier in an incoming PUBACK or PUBREC should be handled.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoVLaAwrBN1PSG42tFv

… reason codes

Adds mqtt v5 network and application scripts and IT methods for a QoS 2
PUBLISH answered with PUBREC reason codes 0x80 and 0x97 followed by a
successful message, and a QoS 1 PUBLISH answered with PUBACK 0x80
followed by a successful message (MQTT 5.0 sections 3.4.2.1, 3.5.2.1
and 4.3.3). The QoS 2 runtime IT fails against the current code.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVLaAwrBN1PSG42tFv
A PUBREC with a reason code of 0x80 or above completes the QoS 2 exchange
without a PUBREL (MQTT 5.0 section 4.3.3). Complete the message downstream
as for a PUBCOMP instead of persisting it as INCOMPLETE and waiting for a
PUBCOMP that never arrives. MQTT 3.1.1 PUBREC handling is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVLaAwrBN1PSG42tFv
@jfallows
jfallows force-pushed the fix/3164-mqtt-ack-failure-reason-codes branch from f5ac278 to 46ec36c Compare November 12, 2026 23:55
@jfallows
jfallows changed the base branch from fix/3158-mqtt-kafka-retained-topic-compaction to develop November 12, 2026 23:56
@jfallows
jfallows added this pull request to stack #3189 November 13, 2026 00:47
@jfallows
jfallows merged commit a17fed3 into develop Nov 13, 2026
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: PUBACK or PUBREC with a failure reason code is treated as success

2 participants