Skip to content

fix(binding-mqtt): complete in-flight shared deliveries before the unsubscribe state write - #3157

Merged
jfallows merged 13 commits into
feature/mqtt-shared-subscriptions-10-recovery-fetch-boundfrom
feature/mqtt-shared-subscriptions-11-unsubscribe-drain-recovery
Nov 17, 2026
Merged

jfallows merged 13 commits into
feature/mqtt-shared-subscriptions-10-recovery-fetch-boundfrom
feature/mqtt-shared-subscriptions-11-unsubscribe-drain-recovery

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

A QoS 2 claim ticket whose subscription is not restored at reconnect stays in the store until session termination and is never redelivered. MQTT 5.0 4.8.2 [4.8.2-4/-5] requires QoS 2 delivery to complete to the same client on reconnect, and 4.4 [MQTT-4.4.0-1] requires unacknowledged PUBLISH and PUBREL to be resent.

The case found by test is UNSUBSCRIBE while a QoS 2 delivery is in flight. The MQTT server wrote the session state without the subscription as soon as the UNSUBSCRIBE arrived and only then drained the in-flight acknowledgements. A disconnect mid-drain left a PUBLISH-sent or PUBREL-pending ticket in the store with no subscription restored on reconnect.

Rather than add a stream that is not tied to a subscription, which has no established pattern (the MQTT server only opens subscribe streams for subscriptions in the session state), the subscription now stays in the session state until the drain completes, so recovery runs through the existing restored-subscription path.

binding-mqtt

  • An UNSUBSCRIBE of a shared filter with in-flight QoS 1 or 2 deliveries stops new messages with the stop flush, keeps the subscription in the session state, and writes the removal and sends UNSUBACK only once the in-flight acknowledgements complete. It reuses the deferred-unsubscribe mechanism; the stopped phase is derived from the stream state, with no new flags.
  • Messages arriving while the stream drains are dropped. An application reset or abort mid-drain ends quietly, without a disconnect, and the unsubscribe is acknowledged with success. A network disconnect mid-drain writes nothing, so the subscription stays in the state.
  • A filter with nothing in flight and non-shared subscriptions keep their existing ordering.

binding-mqtt-kafka

  • Shared memberships are reconciled when Kafka echoes the session state back, rather than when the MQTT side writes it. A failed state write no longer leaves the registry count lower than the subscriptions the persisted state still holds.
  • After the unsubscribe stop flush, the shared stream releases the next record to the share group instead of delivering it: no ticket is written and nothing is accepted. A record already in flight completes first. This mirrors how the cache releases records it cannot deliver, and closes a second path where a ticket outlived its subscription.

Part of #1801

Stack

Layer 11 of the stack of PRs for #1801, based on #3156 (layer 10). See #3154 for the full table.

Maintainer decisions

  1. UNSUBACK is now deferred until the drain completes for shared filters with in-flight QoS 1 or 2. MQTT 5.0 3.10.4 lets the server complete delivery first, but a slow QoS 2 client delays UNSUBACK.
  2. A re-SUBSCRIBE of the same filter before the UNSUBACK is left to the existing flow. The resubscribe script is reordered so the final ack precedes UNSUBACK.

Tests

Written first and seen failing for the right reason (all 7 shared in-flight UnsubscribeIT scenarios and PipelineIT qos1/qos2 before the server change; "unexpected store operation delete memberships" for the failed state write; a timeout for the release scenarios).

  • specs/binding-mqtt.spec: UnsubscribeIT (network v4 and v5, application) and UpstreamIT.
  • specs/binding-mqtt-kafka.spec: MqttIT and KafkaIT.
  • runtime/binding-mqtt: full verify.
  • runtime/binding-mqtt-kafka: full verify.
  • ./mvnw checkstyle:check on both bindings and both spec modules: 0 violations.
  • The full reactor build of this tip (./mvnw clean install): BUILD SUCCESS.

Registry and memberships invariants are asserted, via the test store assertions, by:

  • MqttKafkaSessionProxyIT.shouldRetainSharedMembershipWhenStateWriteFails (new): nothing after the membership put when the write fails.
  • MqttKafkaSessionProxyIT.shouldLeaveSharedSubscriptionOnUnsubscribe and its second variant (existing): membership delete, then registry replace 0:1 to 0:0.
  • MqttKafkaSubscribeProxyIT.shouldSubscribeSharedUnsubscribeInflightQos2DisconnectRecoverPublishSent and ...PubrelPending (new): membership and registry untouched across a mid-drain disconnect; ticket put, put, delete.
  • MqttKafkaSubscribeProxyIT.shouldSubscribeSharedUnsubscribeReleaseQos2 (new): no ticket written for a released record.

The new recovery, release and failed-state-write scenarios are subscribe.shared.unsubscribe.inflight.qos2.disconnect.recover.{publish.sent,pubrel.pending}, subscribe.shared.unsubscribe.release.{qos1,qos2}, subscribe.shared.unsubscribe.inflight.then.release and session.unsubscribe.shared.state.write.reset.

Real broker

Kafka 4.1.1 with examples/mqtt.kafka.proxy, using an image built from this branch plus the commit from #3119, and a raw MQTT 5 client. Without that commit, reconnects with a reused client id are disconnected with 0x8E (#3103).

  • A QoS 2 delivery is received and an UNSUBSCRIBE is sent while it is unacknowledged. No UNSUBACK arrives.
  • After a drop and a reconnect with Clean Start 0 (session present), the PUBLISH is redelivered with the same packet id and DUP=1. PUBREC gets PUBREL, and PUBCOMP is sent.
  • Reconnecting again with the subscription still restored, before any UNSUBSCRIBE, redelivers nothing, so the ticket was removed.
  • A resent UNSUBSCRIBE is acknowledged, and a final reconnect redelivers nothing.

The state topic holds two records for the client: the subscribe, and the removal written only after the drain.


Generated by Claude Code

… after the drain

An UNSUBSCRIBE of a shared subscription with in-flight QoS 1 or 2 deliveries
stops new messages, then writes the session state without the subscription and
sends UNSUBACK only once the in-flight acknowledgements complete. A disconnect
before then leaves the subscription in the session state.

Part of #1801

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV8BXxXeLTJEP4JLG9
… in-flight deliveries complete

An UNSUBSCRIBE of a shared subscription with in-flight QoS 1 or 2 deliveries
now stops new messages at once but keeps the subscription in the session state
until the in-flight acknowledgements complete, then writes the state and sends
UNSUBACK. A client that disconnects mid-drain keeps the subscription in the
state, so it is restored on reconnect and unfinished deliveries can be
completed.

Part of #1801

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

While an unsubscribed shared stream drains, new messages are dropped, an
application reset or abort completes the unsubscribe without a disconnect,
and the unsubscribe is acknowledged with success. The upstream scripts
acknowledge in-flight deliveries before the UNSUBSCRIBE.

Part of #1801

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV8BXxXeLTJEP4JLG9
…ate is durable

Memberships were reconciled when the session state was written to Kafka, so a
failed write left the registry count lower than the subscriptions that the
persisted state still holds. Reconcile when Kafka echoes the state back.

Part of #1801

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

After the stop flush of an unsubscribe the shared stream releases the next
record to the share group instead of delivering it. It writes no ticket and
accepts nothing, so the record stays available to the other members. A record
already in flight completes first.

Part of #1801

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV8BXxXeLTJEP4JLG9
@jfallows
jfallows force-pushed the feature/mqtt-shared-subscriptions-11-unsubscribe-drain-recovery branch from 28d1b73 to 399e363 Compare November 17, 2026 18:36
@jfallows
jfallows merged commit 544a828 into develop Nov 17, 2026
54 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