Skip to content

Fix boot race: retry on any startup error and clean up paho threads - #462

Closed
tomer-w wants to merge 2 commits into
mainfrom
tomer-w-fix-mqtt-reconnect-resubscribe
Closed

Fix boot race: retry on any startup error and clean up paho threads#462
tomer-w wants to merge 2 commits into
mainfrom
tomer-w-fix-mqtt-reconnect-resubscribe

Conversation

@tomer-w

@tomer-w tomer-w commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Problem

Fixes #461

When HA and the Victron system boot simultaneously after a power outage (no UPS), the integration can fail permanently and never recover until manually reloaded.

Root Cause

Hub.start() only catches CannotConnectError and AuthenticationError. But during the boot race, connect() can raise other exceptions (e.g. NotConnectedError if the connection drops mid-setup, or AssertionError, etc.). Any uncaught exception causes HA to treat the failure as permanent — no retry.

Additionally, connect() starts a paho background thread via loop_start() early in the setup. If any later step fails, the thread is leaked (no loop_stop() call). Each HA retry creates a new Hub, leaking another thread.

Fix (3 minimal changes)

  1. Hub.start(): except CannotConnectErrorexcept Exception — any startup error (except auth) becomes ConfigEntryNotReady, so HA retries with exponential backoff
  2. connect(): Wrap post-loop_start() code in try/except that calls loop_stop() on failure — prevents leaked paho threads on retry
  3. disconnect(): Add loop_stop() — properly stops the background thread

… fails

When the MQTT broker restarts (e.g. Victron power cycle, power outage),
paho-mqtt auto-reconnects and fires the on_connect callback. Previously,
if _setup_subscriptions() raised ANY exception during reconnection, the
_on_connect handler called client.disconnect(), which permanently killed
paho's auto-reconnect mechanism. The integration would never recover
without a manual reload.

Changes:
- Only call client.disconnect() on first-connect errors (auth failures,
  connection refused). On reconnection, log the error and let paho
  continue auto-reconnecting.
- Track subscription state with _subscribed flag, cleared on disconnect.
- Retry failed subscriptions from the keepalive loop (runs every 30s),
  so even if subscription setup fails during reconnection, it will be
  retried automatically.
- Add reconnection-specific logging (session_present flag, retry status)
  for better diagnostics.

Closes #461

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6374e2f1-1fb3-453d-a1c2-dddf69a62f61
@tomer-w tomer-w changed the title fix: prevent MQTT auto-reconnect from being killed when subscription setup fails Fix boot race condition and MQTT reconnection handling Jul 15, 2026
When HA and Victron boot simultaneously (power outage, no UPS), the
integration can fail permanently because:

1. Hub.start() only catches CannotConnectError, but connect() can also
   raise NotConnectedError (if connection drops mid-setup) or other
   exceptions. Uncaught exceptions cause HA to treat the failure as
   permanent (no retry). Fix: catch Exception (except AuthenticationError)
   and convert to ConfigEntryNotReady so HA retries with backoff.

2. connect() calls loop_start() early, creating a paho background thread.
   If any later step fails, loop_stop() is never called, leaking the
   thread. Each HA retry leaks another thread. Fix: try/except around
   post-loop_start() code that calls loop_stop() on failure.

3. disconnect() calls client.disconnect() but not loop_stop(), so the
   background thread is never explicitly stopped. Fix: add loop_stop().

Fixes #461

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 6374e2f1-1fb3-453d-a1c2-dddf69a62f61
@tomer-w
tomer-w force-pushed the tomer-w-fix-mqtt-reconnect-resubscribe branch from 0df6a30 to fbdd541 Compare July 15, 2026 05:49
@tomer-w tomer-w changed the title Fix boot race condition and MQTT reconnection handling Fix boot race: retry on any startup error and clean up paho threads Jul 15, 2026
@tomer-w tomer-w closed this Jul 15, 2026
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.

[Bug]: integration does not re-subscribe until reloaded

1 participant