Skip to content

Reconnect edge cases: same-tick double trigger, and unload during an in-executor connect #14

Open

Description

@abrainwood

Found reviewing #13 (#11). Both bugs reproduce on main. Neither has been seen live yet.

1. Same-tick double trigger cancels a running forced attempt

When a sleeping retry is preempted, mqtt_retry_sleeping stays True until that task's finally runs. A second _trigger_mqtt_reconnect in the same loop iteration (watchdog, reconnect-failed, credential refresh) then cancels the forced attempt that just started (api.py ~1268-1272). Cost: a duplicate auth request and a wasted attempt. #13 adds a third trigger source, so the odds go up.

2. Unload during an in-executor connect leaves a live client and recreates the store

Cancelling the reconnect task doesn't stop _connect_mqtt in the executor thread. When that thread finishes, _schedule_credential_refresh -> _get_entry_store recreates the entry store (api.py ~1376), and the MQTT client stays connected after unload.

Acceptance

  • A deterministic test for each bug that fails on main.
  • Only one attempt runs after a preempt plus a same-tick trigger.
  • After unload, nothing recreates the store and the client is disconnected.

Probe shapes K and I2 are in the #13 review scratch (test_probe_review11_r2.py).

Activity

  1. abrainwood commented on Sep 15, 2026

    @abrainwood
    OwnerAuthor

    Another unload edge case, found in review of PR #13 (credential-refresh reconnect self-cancel). It was already on main before that PR and is out of scope for it.

    _connect_mqtt calls _get_entry_store, whose setdefault brings back an empty store in hass.data if the entry was unloaded while a connect was in flight. In production, cleanup_entry cancels the attempt first, so this needs an unload that skips cleanup. A reviewer probe (L3b) reproduces it.

    Fix direction: use a non-creating lookup (hass.data.get) on every path that can run after unload, the same way _wake_held_write_on_reconnect already does.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions