Skip to content

Keep HiveMQ inbound subscription in step with start and stop - #11732

Open
akshit-khandelwal47 wants to merge 1 commit into
spring-projects:mainfrom
akshit-khandelwal47:mqtt-client-restart-resubscribe
Open

akshit-khandelwal47 wants to merge 1 commit into
spring-projects:mainfrom
akshit-khandelwal47:mqtt-client-restart-resubscribe

Conversation

@akshit-khandelwal47

Copy link
Copy Markdown

The HiveMQ-based Mqtt3MessageDrivenChannelAdapter and Mqtt5MessageDrivenChannelAdapter subscribe and unsubscribe asynchronously, and isSubscribed only changes when the broker acknowledges. doStart() and doStop() decide from that flag, so when they run within one broker round trip of each other they act on a stale state:

  • stop() then start(): start() still sees isSubscribed == true and skips the subscription. The UNSUBACK then clears the flag, and the adapter is running but not subscribed, so it receives nothing, with no error or warning.
  • start() then stop(): stop() doesn't see isSubscribed yet and skips the unsubscription. The SUBACK then sets the flag, and the stopped adapter keeps sending messages to its output channel.

Against the Mosquitto container from the module's tests, adapter.stop(); adapter.start(); leaves the adapter with isRunning() == true and isSubscribed == false, and a message published afterwards never arrives. With a one second pause between stop() and start() it works. So anything that restarts an adapter faster than the broker answers hits this, and the window grows with the round trip to the broker.

I found this while reading the new 7.2 HiveMQ adapters, not from a production system.

The fix keeps the existing flags and lets the acknowledgment callbacks catch up with the lifecycle:

  • isUnsubscribing tracks an in-flight unsubscription, as isSubscribing already does for the subscription, so the two operations don't overlap.
  • When the SUBACK arrives for an adapter that is no longer active, it unsubscribes.
  • When the UNSUBACK arrives for an adapter that is active again, it subscribes.

AbstractEndpoint sets active before doStart() and clears it before doStop(), so a callback that updates its flag and then checks isActive() can't miss a concurrent start or stop.

testStartRightAfterStopSubscribesAgain and testStopRightAfterStartUnsubscribes in Mqtt3MessageDrivenChannelAdapterTests and Mqtt5MessageDrivenChannelAdapterTests fail on main and pass with the change. Removing either half of the fix fails the matching two tests again. ./gradlew :spring-integration-mqtt-client:check passes locally, and the adapter tests passed 5 runs in a row.

I used Claude Code (Claude Opus 5.5) to help with this. The commit credits it in a Co-authored-by trailer.

🤖 Generated with Claude Code

The `Mqtt3MessageDrivenChannelAdapter` and `Mqtt5MessageDrivenChannelAdapter`
subscribe and unsubscribe asynchronously, and `isSubscribed` changes only when
the broker acknowledges.
`doStart()` and `doStop()` decide from that flag, so they act on a stale state
when they run within one broker round trip of each other:

* `stop()` followed by `start()`: `start()` still sees `isSubscribed` and skips
  the subscription, the UNSUBACK then clears it, and the running adapter
  receives nothing
* `start()` followed by `stop()`: `stop()` does not see `isSubscribed` yet and
  skips the unsubscription, the SUBACK then sets it, and the stopped adapter
  keeps sending messages to its output channel

Fix:

* Track an in-flight unsubscription with `isUnsubscribing`, as `isSubscribing`
  does for the subscription
* When the SUBACK arrives for an adapter that is no longer active, unsubscribe
* When the UNSUBACK arrives for an adapter that is active again, subscribe

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Akshit Khandelwal <akshitkhandelwal20@gmail.com>

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
We will merge this next week when we start 7.2.1 patch version.

Thanks

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants