Repository navigation
fix: keep cleaning up state triggers after an already removed watch - #901
craigbarratt merged 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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.
|
Thanks for the PR! |
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 examplesensor.exampleandsensor.example.flag, since both register undersensor.example. Any watch that came later in the loop, likesensor.other, kept the stopped queue, so stale queues piled up across reloads and kept receiving updates.The early
returnis now acontinue, so a missing entry only skips that one watch.The new test in
tests/test_state.pyregisters 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 undersensor.other. With the fix the wholetests/suite passes (196 tests), and ruff and mypy are clean.Thanks for taking a look!