Stop the Expert MQTT client reconnecting in a hot loop - #8631
Merged
Merged
Conversation
reconnectAttempt was cleared on every successful connect, so a link that connected and then dropped seconds later never backed off and kept retrying at the initial delay. Record when the connection came up instead, and only clear the counter once it has held for STABLE_CONNECTION_MS. _dispatch also invoked observer handlers without catching, so any async handler that rejected escaped as an unhandled rejection. That is how in-flight packets flushed by a client teardown surfaced as "Connection closed". It now catches sync throws and attaches a handler to any promise a handler returns, staying quiet for teardown errors and warning on everything else. Also attaches a catch to the fire-and-forget abort publish and gives the expert:status-message ack the try/catch its sibling branches already have.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8631 +/- ##
=======================================
Coverage 77.25% 77.25%
=======================================
Files 466 466
Lines 25131 25131
Branches 6690 6690
=======================================
Hits 19414 19414
Misses 5717 5717
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
andypalmi
approved these changes
Sep 22, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #8630, which the follow-up PR closes.
The Expert's MQTT connection was reconnecting in a hot loop whenever a link connected and then dropped again a few seconds later, because
reconnectAttemptgot cleared on every successful connect. One user in production went through 11 reconnects in under 3 minutes that way.Three changes:
connectnow records when the connection came up, andscheduleReconnectonly clears the attempt counter once it has held for 30s. A flapping link walks 1s, 2s, 4s up to the existing 30s cap instead of retrying at 1s forever._dispatchwas calling observer handlers without catching, so an async handler that rejected escaped as an unhandled rejection. That is where theConnection closederrors in Sentry came from: mqtt.js flushes in-flight packets when a client is torn down during a reconnect, and nothing was listening. It now catches sync throws and attaches a handler to any promise a handler returns, staying quiet for teardown errors and warning on anything else.expert:status-messageack gets the try/catch its sibling branches already had.Publishes and subscribes still reject honestly rather than being resolved on teardown, since swallowing them inside the service would hide real delivery failures too.
Four tests added. Two of them fail without the fix, the other two are regression guards that passed either way.
Deliberately not in here: every drop currently posts a disconnect message and every recovery posts a greeting, so a short blip gets narrated twice. Suppressing that needs a call on how long an outage should last before the Expert mentions it at all.