Repository navigation
Stop task.wait_until's triggers when the waiting task is cancelled - #943
Merged
craigbarratt merged 1 commit intoDec 4, 2026
Merged
Conversation
craigbarratt
approved these changes
Dec 4, 2026
craigbarratt
left a comment
Member
There was a problem hiding this comment.
Thanks @jfriend00, good find. Verified end to end that a wait cancelled with task.cancel() or task.unique() leaves its trigger queues and tasks behind on master, and nothing with this change. Approving.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stop task.wait_until's triggers when the waiting task is cancelled
What was broken
Cancelling a task while it waits in
task.wait_until(), withtask.cancel()or a newertask.unique()call, didn't stop the wait's triggers. A leftoverstate_triggerkeeps evaluating its expression on each change of the entities it watches until the expression is next true. After that, its queue stays registered and gets an item for each later change of those entities, until Home Assistant restarts. Nothing else runs (the code aftertask.wait_until()never resumes), and no warning or error is logged.So the cost is small: an extra evaluation of the expression per change until it's next true, then a queue item per change. It only adds up when waits are cancelled often, as with
task.unique()andtask.wait_until()together, on entities that change often. 2.1.1 does this too, so it isn't new in 3.0.0. Withallow_all_importsset, callingpyscript.wait_until_demoshows the leftover queue:Why
WaitUntilDecoratorManagerstops its decorators indispatch(), when a trigger or the timeout fires, and inhandle_exception(). When the waiting task is cancelled,await self._futureraisesCancelledErrorand neither of them runs, so the decorators keep running. For astate_trigger, only itsstop()callsState.notify_del(). The next time the expression is true,dispatch()callsself._future.exception()on the cancelled future, which raisesCancelledErrorand ends the trigger's task, but its queue stays inState.notify.What I changed
WaitUntilDecoratorManager.wait_until()now stops the decorators when the wait is cancelled. It checks that the manager is still running first, since a trigger may have fired just before the cancel, andstop()warns when it isn't running. A cancel that arrives while the triggers are still starting isn't covered:DecoratorManager.start()only rolls back when a decorator raises anException, and onlymqtt_triggerandsentence_triggerwait while they start.Testing
A new test in
tests/test_decorator_manager.pycancels a task that's waiting inWaitUntilDecoratorManager.wait_until()and checks that the wait's decorator was stopped. It fails without the change.tests/test_decorator_manager.py(38 tests) andtests/test_unique.py(3 tests) pass, and ruff, codespell, pylint and mypy are clean. This ran on Windows with local stand-ins for Home Assistant's Linux-only imports, so CI is its first Linux run.Assisted by Claude Code