Skip to content

Allow MQTTHandlers to replace other MQTTHandlers - #3656

Open
richturner wants to merge 7 commits into
mainfrom
worktree-mqtt-provisioning-handler-override
Open

richturner wants to merge 7 commits into
mainfrom
worktree-mqtt-provisioning-handler-override

Conversation

@richturner

Copy link
Copy Markdown
Member

This PR introduces MQTTHandler.getReplacedHandlers() to allow one MQTTHandler to replace one or more other MQTTHandler(s) by class name.

This is an alternative approach to #3590 which this PR also reverts.

Restores the original `provisioning` topic token changed in #3590. Custom
projects that need the old mechanism on this topic can instead replace the
built-in handler. The X.509 challenge prefix change from #3590 is kept.
Adds `MQTTHandler.getReplacedHandlers()` so a custom project can take over
the topic of a built-in handler (e.g. `UserAssetProvisioningMQTTHandler`).
A handler that extends another loaded handler replaces it implicitly.
Replaced handlers are discarded on load, so they are never initialized or
started and never consume messages; ordering by priority alone was not
enough because every handler receives published messages on its topics.
stop() reloaded the handlers via ServiceLoader, so it stopped new instances
that were never initialized instead of the running ones, and ignored
replaced handlers. It now stops the loaded handlers in reverse priority order.
@richturner

Copy link
Copy Markdown
Member Author

Validated this in a custom project and it works

@ebariaux ebariaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Other codex findings

  • [P2] Test which handler instances receive lifecycle callbacks — manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java:462-463

X509ProvisioningProofTest constructs its handler directly, so it would still pass if the broker initialized a replaced provider or reverted to stopping freshly loaded instances. Add a Spock unit spec in manager/src/test/groovy with a replacement and an unrelated peer; assert that only the selected instances initialize and start, and that those same instances stop once in reverse order.

And you did not revert the change on the challenge, so this still needs to be indicated as a breaking change for this release.

@richturner

Copy link
Copy Markdown
Member Author

Changes made based on the review; prefix revert means the PR no longer needs the Breaking Change label.

@richturner
richturner requested a review from ebariaux December 3, 2026 09:23

@ebariaux ebariaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One small finding remaining about regression test, nothing major.
Agent also found related issue that was present before this PR, to be addressed separately: #3663

The PR fixes normal shutdown. There's a remaining finding about whether the test would catch that bug coming back.

An MQTT handler stores state on the instance that started. For example, start() stores a client session in a field, and stop() reads that field to close the session. The old broker ran ServiceLoader again during shutdown. That created a fresh object of the same handler class, with an empty session field. The handler that had actually started never received its cleanup callback. The PR fixes this by keeping the selected instances and stopping those same objects. MQTTHandler.java

The test gap is one method call. In production, the container calls broker.stop(container), which now calls stopHandlers(). The test calls stopHandlers() directly, skipping the method where the original bug lived. I restored the old broker.stop(container) in an isolated copy and all five tests still passed. Calling broker.stop(container) in the test would let its existing assertions catch the regression. MQTTHandlerReplacementTest.groovy

The separate, pre-existing issue happens during cleanup after a failed startup. The sequence I reproduced was:

  1. One handler finishes init(). The next handler throws during init().
  2. The container begins shutdown. Artemis would have been created in the later start() phase, so the broker’s server field is still null.
  3. broker.stop(container) calls server.stop() first. That throws before execution reaches stopHandlers().

The first handler therefore received init() but never received stop(). Any resource that depends on its cleanup callback can stay open.

Calling stopHandlers() directly skipped the method where the original
bug lived, so the spec still passed with the old ServiceLoader-based
stop(). Stub the broker's server and debouncer and call stop(container)
as the container does.
@richturner
richturner requested a review from ebariaux December 3, 2026 19:48
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants