Skip to content

Copy callback list before dispatching updates#44

Open
oyvindwe wants to merge 1 commit into
echoromeo:masterfrom
oyvindwe:fix/data-callbacks
Open

Copy callback list before dispatching updates#44
oyvindwe wants to merge 1 commit into
echoromeo:masterfrom
oyvindwe:fix/data-callbacks

Conversation

@oyvindwe

@oyvindwe oyvindwe commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

A registered callback may deregister callbacks while it runs — e.g. a Home Assistant entity tearing down during a push deregisters its own update callback. Iterating self._callbacks directly means that removal mutates the list mid-iteration and the loop skips the following callback, so some consumers can miss an update.

Iterate over a snapshot (list(self._callbacks)) at both data-callback dispatch sites, matching how _set_connected already dispatches the connection-state callbacks.

Triggered by this comment: home-assistant/core#176511 (comment)

A registered callback may deregister callbacks while it runs — e.g. a
Home Assistant entity tearing down during a push deregisters its own
update callback. Iterating self._callbacks directly means that removal
mutates the list mid-iteration and the loop skips the following
callback, so some consumers can miss an update.

Iterate over a snapshot (list(self._callbacks)) at both data-callback
dispatch sites, matching how _set_connected already dispatches the
connection-state callbacks.
@oyvindwe
oyvindwe requested a review from capelevy July 14, 2026 18:38
oyvindwe added a commit to oyvindwe/home-assistant that referenced this pull request Jul 14, 2026
The call_soon deferral introduced an ordering hazard: when pynobo
processes a buffered zone removal followed by an id-reusing add before
the deferred removal runs, the re-add is rejected as a duplicate and the
stale snapshot then deletes the surviving entity, leaving the zone with
no entity until reload.

Synchronous removal has no such window — the old device/entity is gone
before the next buffered message's add. The original re-entrancy concern
(mutating pynobo's callback list mid-dispatch) is harmless here
(per-message dispatch; removal messages don't update survivors) and is
fixed at the root upstream in echoromeo/pynobo#44.
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