Skip to content

fix(binding-mqtt): complete in-flight deliveries when unsubscribing - #3113

Merged
jfallows merged 21 commits into
feature/mqtt-shared-subscriptions-4-mqtt-suback-gatingfrom
feature/mqtt-shared-subscriptions-5-mqtt-unsubscribe-drain
Nov 17, 2026
Merged

jfallows merged 21 commits into
feature/mqtt-shared-subscriptions-4-mqtt-suback-gatingfrom
feature/mqtt-shared-subscriptions-5-mqtt-unsubscribe-drain

Conversation

@jfallows

@jfallows jfallows commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

UNSUBSCRIBE in binding-mqtt stops new matching messages and lets the subscribe stream drain: the stream ENDs only after in-flight QoS 1 and 2 acknowledgements complete (MQTT 5.0 3.10.4-2 and 3.10.4-3). For a shared subscription this means a message already delivered to the client is acknowledged to the application before the subscription ends, rather than being dropped or redelivered to another member.

Part of #1801

Behavior

  • An unsubscribe of a granted shared subscription (QoS 1 or 2) succeeds and ends its stream, acknowledged with a success reason code.
  • Messages awaiting acknowledgement at the time of UNSUBSCRIBE are completed before the stream ends; filters removed by the UNSUBSCRIBE receive no further messages, and a resubscribe while unsubscribing is handled.
  • A subscribe stream that has ended and carries no filters is not unsubscribed again, and an ended stream is unsubscribed only for filters that are still subscribed, so a shared subscription gets a single unsubscribe.
  • Sequential subscriptions with messages on each complete through the server-to-client pipeline.

Stack

This is one layer of a stack of PRs for #1801. Each PR contains only the commits since the previous layer; this PR is based on the previous layer's branch.

# PR Layer Base
1 #3109 binding-kafka: share group stream develop
2 #3110 binding-mqtt: shared subscriptions, server and client #3109
3 #3111 binding-mqtt-kafka: shared subscriptions for QoS 0 and 1 #3110
4 #3112 binding-mqtt: SUBACK after application streams begin #3111
5 #3113 (this PR) binding-mqtt: complete in-flight deliveries when unsubscribing #3112
6 #3114 binding-mqtt-kafka: QoS 2 claim tickets (with engine test store assertions) #3113
7 #3115 binding-mqtt-kafka: epoch registry for the lifecycle of a shared subscription #3114
8 #3116 real-broker share stream fixes and mqtt.kafka.proxy example (top of the stack) #3115

Documentation: aklivity/aklivity-docs#181 (merge after the stack).

Test evidence

Run on this branch tip (checkstyle and license checks run in the validate phase of each build):

  • ./mvnw clean install -DskipTests -pl <modules> -am -Dmaven.build.cache.enabled=false: OK
  • ./mvnw clean verify -pl specs/binding-mqtt.spec,runtime/binding-mqtt,specs/binding-mqtt-kafka.spec,runtime/binding-mqtt-kafka -Dmaven.build.cache.enabled=false: OK (unit tests / ITs, 0 failures)
    • specs/binding-mqtt.spec: 96 / 601
    • specs/binding-mqtt-kafka.spec: 32 / 263
    • runtime/binding-mqtt: 63 / 482
    • runtime/binding-mqtt-kafka: 29 / 152

Notes for reviewers

  • The stack is linearized by rebase and force push, so expect SHA changes against any earlier view.
  • Every fix is preceded by a spec commit and an IT expectation commit, so the scenario list in the commit history doubles as the case list.
  • Known limitations are tracked in Support MQTT 5 Shared Subscriptions via Kafka share groups #1801.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2

After the last filter of a subscribe stream is unsubscribed, the application may
reset or abort the stream. The client connection must stay alive and keep
answering pings.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ave the connection alive

A reset of a subscribe stream that no longer carries filters is cleanup, not a
reason to disconnect the client.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
A subscribe stream that no longer carries filters has nothing to reject. When
the application resets it, release the stream without disconnecting the
client, as only a stream that still carries filters can be rejected.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
… 1 or 2

Unsubscribing a shared subscription acknowledges with success and ends its
stream even when the application state granted the subscription a QoS of 1 or
2. Cover the network, application and upstream sides.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…on to succeed and end its stream

Run the scenarios through the server binding and through the server to client
pipeline.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…cription was granted a qos

The reason code of a subscription records the QoS it was granted, which is not
a valid unsubscribe reason code and does not mean the unsubscribe failed.
Acknowledge such an unsubscribe with success and remove the subscription from
its stream, ending a shared subscription stream that carries no other filter.

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

A subscribe stream keeps carrying acknowledgements for messages already sent to
the client after the last filter is unsubscribed, and only then ends. The
application receives a flush with no filters when the last filter is removed, a
new message after the unsubscribe is not delivered, a message in progress is
completed, a disconnect ends the stream at once, a reset is cleanup and the
same filter can be subscribed again meanwhile. Cover shared subscriptions, and
a non-shared stream that keeps acknowledging while it carries no filters, for
MQTT 5 and MQTT 3.1.1.

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

Run the scenarios through the server binding, and the shared subscription
acknowledgement scenarios through the server to client pipeline.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…carries no filters

When the application ends a subscribe stream after telling it that no filters
remain, there is nothing left to unsubscribe, and an UNSUBSCRIBE packet
without topic filters is malformed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ending an unsubscribed stream

When the last filter of a shared subscription stream is unsubscribed, keep the
stream open while messages already sent to the client are still awaiting their
PUBACK, PUBREC or PUBCOMP, so the acknowledgements reach the application, and
end it once none remain. The application is told by a flush without filters.
A message in progress is completed, a message that starts afterwards is not
delivered and is not acknowledged to the application, and a message whose
subscription identifiers belong only to removed filters is not delivered. A
disconnect or reset ends the stream at once. A new subscription of the same
filter opens a new stream while the previous one drains.

The subscribe stream decodes data in states, as the connection decodes packets:
a new message, a delivered message in progress and an ignored message in
progress. Acknowledgements for a packet id that no stream awaits are ignored.

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

A stream that keeps other filters does not deliver a message whose subscription
identifier belongs to a removed filter, and a non-shared stream carrying a
message awaiting acknowledgement accepts the same filter again.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Cover two consecutive SUBSCRIBE packets with a message delivered on the
first, then the second, then the first again, for shared then shared,
shared then non-shared, non-shared then shared and non-shared then
non-shared at QoS 1 and 2, and a shared subscription resubscribed at a
different QoS.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…her than granted qos

A subscription acknowledged with a granted QoS reports that QoS as its
reason code, while the same subscription carried in a later state may
report success, so equality on the raw reason code made subscriptions
that had not changed look added and removed. A later subscribe then
reopened already established streams and its subscribe acknowledgement
never completed.

Treat every granted QoS as success when comparing, keeping failure
codes distinct so a subscription whose removal was declined still
differs from its previous state.

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

A publish carrying a subscription identifier that the subscribe stream
did not yet hold was left undecoded, and nothing resumed decoding once
the stream received the filters that include it, so a resubscribe at a
different QoS never delivered its first message.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…e delivering its publish

A publish for a shared subscription whose subscribe stream had not yet
begun fell back to the non-shared stream of the same QoS and was
delivered to the wrong subscription. Only fall back to the non-shared
stream when the publish does not belong to a shared subscription, so
decoding waits for the shared stream to begin.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Cover unsubscribing a shared subscription and then disconnecting,
disconnecting with a shared subscription still established, a shared
subscription resubscribed at a different QoS and then unsubscribed, and
a subscribe stream ended while its shared subscription is still
established. The disconnect that follows each unsubscribe exposes any
further unsubscribe sent for the same filter.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…ters still subscribed

A subscribe stream that ended after its filters were already removed
through the session state unsubscribed them a second time, sending a
further UNSUBSCRIBE for each filter. Unsubscribe only the filters the
session still holds, and release them from the session as they are
unsubscribed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…knowledgement and delivery behavior

Convert the scenarios to the version-aware session state builder, add an
MQTT 3.1.1 variant of the in-flight qos 1 application script, and write the
unsubscribe acknowledgement after the remaining fragments of a publish
that is already in progress.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoWETrA5Cn7uqKAtsMH
@jfallows
jfallows force-pushed the feature/mqtt-shared-subscriptions-5-mqtt-unsubscribe-drain branch from 0a76d6f to f250e94 Compare November 17, 2026 07:13
@jfallows
jfallows merged commit d8cac97 into develop Nov 17, 2026
81 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.

2 participants