Skip to content

Unsubscribe dispatcher and update listeners on remove/unload - #532

Open
andreaslindroos wants to merge 1 commit into
custom-components:masterfrom
andreaslindroos:fix/unsubscribe-listeners-on-remove
Open

andreaslindroos wants to merge 1 commit into
custom-components:masterfrom
andreaslindroos:fix/unsubscribe-listeners-on-remove

Conversation

@andreaslindroos

Copy link
Copy Markdown

What's broken

Every config entry reload leaks the old sensors. They are removed from HA,
but they keep recalculating on every quarter-hour tick. With enough leaked
sensors this blocks the event loop and freezes all of HA for minutes at a
time
: automations, UI and integrations all stop.

It fails silently. Nothing is logged: no error, no warning, and HA's
blocking-call detection doesn't catch CPU-bound work. All you see is HA
going unresponsive every 15 minutes, with MQTT clients timing out as a side
effect, and nothing pointing at nordpool. This also affects automations and many
probably have automations running at exact hour or half past or similar.

Why

  • async_added_to_hass calls async_dispatcher_connect for NEW_DAY,
    NEW_PRICE and NEW_HOUR and discards the unsubscribe callbacks, so
    removed sensors stay subscribed.
  • The update listener is never removed, and async_reload_entry calls
    unload/setup directly. Listeners stack up and later updates trigger
    several reloads, each leaking more sensors.

Example of this happening

HA froze for ~2 min at every :00/:15/:30/:45, with nothing in the logs.
Stack dumps showed the event loop in handle_new_hr → _update →
_calc_price for the whole freeze. One render takes ~0.3 ms, but there
were 133 live sensor objects for 3 real sensors.

Fix

  • Wrap the dispatcher connections in self.async_on_remove(...).
  • Register the update listener with entry.async_on_unload(...).
  • Reload via hass.config_entries.async_reload(entry.entry_id).

Tested: after a restart with this patch, the nordpool_update_hour
subscriber count stays at 3 across reloads, and the freezes are gone.

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.

1 participant