Skip to content

fix: keep cleaning up state triggers after an already removed watch - #901

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
junkerderprovinz:fix/notify-del-cleanup
Nov 18, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
junkerderprovinz:fix/notify-del-cleanup

Conversation

@junkerderprovinz

Copy link
Copy Markdown
Contributor

Hi, and thanks for pyscript!

Fixes #876

@ffrog8 found this one and wrote up the cause, the one-word fix and a before and after count from a live setup. Thank you, that made it very easy to follow!

State.notify_del() returned from the whole loop when it reached a watch whose queue was already gone. That happens when a trigger watches an entity and one of its attributes, for example sensor.example and sensor.example.flag, since both register under sensor.example. Any watch that came later in the loop, like sensor.other, kept the stopped queue, so stale queues piled up across reloads and kept receiving updates.

The early return is now a continue, so a missing entry only skips that one watch.

The new test in tests/test_state.py registers a queue for those three watches and removes it again. It uses a small set subclass with sorted iteration, since the bug depends on order and plain set order changes with string hashing. The new test fails without the fix, because the queue stays registered under sensor.other. With the fix the whole tests/ suite passes (196 tests), and ruff and mypy are clean.

Thanks for taking a look!

notify_del() returned as soon as it met a watch whose queue was already
gone, which happens when a trigger watches an entity and one of its
attributes. The remaining watches kept the stopped queue registered, so
they piled up across reloads.

Co-authored-by: ffrog8 <2747195+ffrog8@users.noreply.github.com>

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @junkerderprovinz, and thanks @ffrog8 for tracking down the cause!

Changing the return to continue is the right fix, and it covers both the legacy trigger path and the new state decorator, since both go through State.notify_del. I merged this into current master: all 200 tests pass, and ruff, pylint and mypy are clean. With the old return restored, the new test fails because the queue is still registered under sensor.other. Using SortedSet was a good call: with a plain set, the old code only leaked for some hash seeds.

One small thing we'll handle in a follow-up commit, so no change is needed here: notify_del leaves an empty dict behind in State.notify for each entity whose last watcher is removed, unlike the event, mqtt and webhook versions, which delete the key. Those entries don't affect delivery, but update() keeps recording last values for entities nobody watches anymore.

@craigbarratt
craigbarratt merged commit 3be8e13 into custom-components:master Nov 18, 2026
5 checks passed
@craigbarratt

Copy link
Copy Markdown
Member

Thanks for the PR!

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.

State.notify_del() can leave stale trigger queues registered after reload

2 participants