Repository navigation
Keep task.unique() in a script preamble from stopping automatic reload - #931
Merged
Merged
Conversation
The watchdog task was started with Function.create_task(), so it was in Function.our_tasks. A reload after a file save runs the script preamble in that task, so task.unique() there recorded the watchdog as the owner of the name. The next function calling task.unique() with that name then cancelled the watchdog, and automatic reload stopped until Home Assistant restarted. Start it with hass.async_create_background_task() instead. Also, task.unique(name, kill_me=True) outside a function cancelled the current task when a running function owned the name, which stopped a reload partway. It now does nothing there, as documented. Fixes #928. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #928.
Problem
The watchdog task, which reloads scripts when a file is saved, was started with
Function.create_task(), so it was inFunction.our_tasks. The reload runs a saved script's preamble in that task, so atask.unique("name")call in the preamble made the watchdog task the owner of the name. The next function that calledtask.unique("name")then cancelled the watchdog task. Nothing restarted it, so automatic reload stopped until Home Assistant was restarted.Also,
task.unique("name", kill_me=True)outside a function cancelled the current task if a running function owned the name. The docs say it does nothing there. In a preamble, this stopped the reload partway: on a save it cancelled the watchdog task, and withpyscript.reloadit cancelled the service call.Changes
hass.async_create_background_task(), so it isn't inour_tasks. It doesn't use the config entry's version, which would cancel the task when the integration is reloaded, andwatchdog_start()wouldn't start it again.hass_stop()is unchanged.task.unique(name, kill_me=True)only cancels the current task inside a function that pyscript started. Outside a function, it does nothing, as documented.Tests
test_task_unique_preamble_reload(with and withoutkill_me) runs the realwatchdog_start(), with only the file-system observer mocked, and simulates file saves. With a function running that owns the name, it checks that later saves still reload the script, and that apyscript.reloadcall completes. On master both cases fail. Without thetask.unique()change, thekill_me=Truecase fails.🤖 Generated with Claude Code