Skip to content

feat(binding-mqtt): acknowledge subscribe after application streams begin - #3112

Merged
jfallows merged 29 commits into
feature/mqtt-shared-subscriptions-3-mqtt-kafka-sharefrom
feature/mqtt-shared-subscriptions-4-mqtt-suback-gating
Nov 17, 2026
Merged

jfallows merged 29 commits into
feature/mqtt-shared-subscriptions-3-mqtt-kafka-sharefrom
feature/mqtt-shared-subscriptions-4-mqtt-suback-gating

Conversation

@jfallows

@jfallows jfallows commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

binding-mqtt now sends SUBACK only after the reply BEGIN of every application stream opened for the SUBSCRIBE, so a message published right after SUBACK is not lost. This is required by shared subscriptions, where the backing stream is a Kafka share group member that only starts receiving once it has joined, and it also removes the same race for ordinary subscriptions. The layer also hardens the stream lifecycle edge cases that this gating exposes.

Part of #1801

SUBACK waits for application stream BEGIN

  • A reset, abort or END before BEGIN becomes a failure code for that filter, or a disconnect when the stream also carries established filters.
  • Failed filters are removed from the persisted session state, and restored subscriptions are kept in session state.
  • An UNSUBSCRIBE of a pending filter is deferred until its SUBACK, which is then acknowledged in order.
  • A subscribe stream that aborts before it begins fails its pending SUBACK, and the end of the initial direction of a subscribe stream is recorded.

Stream lifecycle fixes found along the way

  • Sibling subscribe streams survive when one stream is rejected or closes.
  • A resubscribed shared stream survives a late abort of the previous stream; a late reset or abort of an unsubscribed stream is cleaned up quietly and leaves the connection alive.
  • Session state compares subscriptions by outcome rather than granted QoS, and decoding resumes when the filters of a subscribe stream change.
  • A publish for a shared subscription waits for its stream before delivery.
  • The mqtt-kafka proxy spec and tests expect no MQTT reply BEGIN before the Kafka share reply BEGIN.

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 (this PR) binding-mqtt: SUBACK after application streams begin #3111
5 #3113 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 / 548
    • specs/binding-mqtt-kafka.spec: 32 / 263
    • runtime/binding-mqtt: 63 / 453
    • 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 here is preceded by a spec commit and an IT expectation commit that fail first, so the scenario list in the commit history doubles as the case list.
  • The UNSUBSCRIBE draining behavior is in the next PR. 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

… stream begin

A subscription is in effect only once its application stream has replied,
so SUBACK follows the reply BEGIN of every stream opened for the SUBSCRIBE.
Ordering is pinned on the single network connection by interleaving PINGREQ
with the held reply BEGIN. A stream reset before its reply BEGIN is reported
per filter in the SUBACK instead of disconnecting the client.

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

SUBACK was sent as soon as the application subscribe streams were opened,
before any had replied, so the client could treat the subscription as in
effect while the application had not yet established it. The SUBACK now waits
for the reply BEGIN of every application stream the SUBSCRIBE opened, tracked
per packet id like the other pending subscribe requests. A stream reset before
its reply BEGIN yields the reset reason code, or unspecified error, for its
filters in the SUBACK and leaves the connection open.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
The subscribe data extension carries deferred on the INIT frame of a
fragmented message, but the builder and matcher could not set or assert it.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
The mqtt side of the fragmented shared subscription scenarios now asserts
deferred on the INIT frame, matching what the kafka side already asserts, and
that an expired fragmented record leaves no deferred on the delivered message.

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

A shared subscription must not report its stream as established until the
kafka share stream has replied. The kafka share stream is reset before its
reply BEGIN and the mqtt client, waiting on a reply BEGIN, must see the reset
without a connected stream.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
An UNSUBSCRIBE received while the SUBACK of the SUBSCRIBE it undoes is still pending is acknowledged after that SUBACK, for one or for one of two filters, in MQTT 3.1.1 and 5.

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

An UNSUBSCRIBE of a filter whose SUBACK is still pending is held in the deferred unsubscribes and released once every pending SUBACK covering its filters has been sent, so UNSUBACK follows SUBACK. Release moves from the first subscribe stream window to the points where a SUBACK completes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
When an application stream for a SUBSCRIBE is reset before it begins, the session state sent to the application no longer contains the failed filters, so a reconnect with Clean Start 0 restores only the accepted filters. The existing reject scenarios now read the rewritten state and new reconnect scenarios are added for MQTT 3.1.1 and 5.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
A subscribe stream reset before it begins fails its filters in the SUBACK and those filters are removed from the session state sent to the application, the way an UNSUBSCRIBE removes them, so they are not restored when the client reconnects with Clean Start 0.

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

A SUBSCRIBE opening exactly-once and at-least-once filters on one route has its exactly-once stream reset before it begins. The SUBACK fails that filter and grants the other, the surviving streams keep delivering and acknowledging at-least-once messages, and an UNSUBSCRIBE and a DISCONNECT still reach them.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
Closing a non-shared subscribe stream removed the whole per-route map of streams, orphaning the streams of the other QoS levels, so unsubscribe and disconnect could no longer reach them. Only the closing stream is removed, and the route entry only once it holds no streams.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
A subscribe stream reset before it begins fails only filters of pending SUBACKs. A stream that also carries restored filters ends the connection, two back-to-back SUBSCRIBEs sharing one unreplied stream each get failure codes in order, and a rejected stream of the new SUBSCRIBE leaves restored filters on their own streams untouched, in MQTT 3.1.1 and 5.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
The subscriptions restored for a reconnect with Clean Start 0 were not recorded as the session's current state, so the next SUBSCRIBE wrote a session state without them and an UNSUBSCRIBE of a restored filter found no subscription. They are recorded when the streams are reopened.

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

Failing filters in the SUBACK is only valid when every filter on the reset stream belongs to a pending SUBACK. A stream that also carries restored filters or filters of an earlier acknowledged SUBSCRIBE ends the connection with the reset reason code, as before the SUBACK waited for the application streams to begin. Pending SUBACKs are kept in arrival order so back-to-back SUBSCRIBEs are acknowledged in order.

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

A shared SUBSCRIBE is unsubscribed and subscribed again, opening a second stream under the same filter. The first stream's reply direction is then aborted by the application, and the second stream keeps delivering and acknowledging at-least-once messages and still ends on UNSUBSCRIBE, in MQTT 5.

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

Closing a shared subscribe stream removed whatever stream was registered under its filter, so a late close of a stream already unsubscribed removed the stream opened by a later SUBSCRIBE of the same filter, and UNSUBSCRIBE could no longer reach it. A shared stream is removed only while it is the registered one, as for non-shared streams.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
A subscribe stream whose reply direction is aborted before it begins fails only the filters of its pending SUBACK with the unspecified failure code, leaving the connection open, in MQTT 3.1.1 and 5. One of two shared filters failing yields a failure and a granted code, and a stream that also carries a restored filter ends the connection.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…am aborts before it begins

A subscribe stream whose reply direction was aborted or ended before it began never completed the SUBACK waiting for it, leaving the client without a reply. It now takes the path of a reset before begin: the SUBACK fails the filters of the pending SUBACK with the unspecified failure code when the stream carries only those filters, and a stream that also carries established filters ends the connection.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVCzGPCAFKe3smSge2
…n the stream aborts before it begins

An UNSUBSCRIBE received while a SUBSCRIBE waits for its application stream is acknowledged after the SUBACK fails that filter, reporting that no subscription existed, in MQTT 5.

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

A subscribe stream ended its initial direction without recording it, so a
later reset or abort of the same stream could not tell it from a live one and
an abort was still sent on the ended direction. Mark the direction closed when
it is ended, as the abort path already does, and stop sending flushes on it.

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

Convert the acknowledgement scenarios to the version-aware session state
builder and add MQTT 3.1.1 variants of the application scripts, which
carry no subscription ids.

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-4-mqtt-suback-gating branch from 9a3d35f to e2d4690 Compare November 17, 2026 06:34
@jfallows
jfallows merged commit fcd378b 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