Skip to content

Keep task.unique() in a script preamble from stopping automatic reload - #931

Merged
craigbarratt merged 1 commit into
masterfrom
fix-928-watchdog-task-unique
Nov 26, 2026
Merged

craigbarratt merged 1 commit into
masterfrom
fix-928-watchdog-task-unique

Conversation

@craigbarratt

Copy link
Copy Markdown
Member

Fixes #928.

Problem

The watchdog task, which reloads scripts when a file is saved, was started with Function.create_task(), so it was in Function.our_tasks. The reload runs a saved script's preamble in that task, so a task.unique("name") call in the preamble made the watchdog task the owner of the name. The next function that called task.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 with pyscript.reload it cancelled the service call.

Changes

  • Start the watchdog task with hass.async_create_background_task(), so it isn't in our_tasks. It doesn't use the config entry's version, which would cancel the task when the integration is reloaded, and watchdog_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 without kill_me) runs the real watchdog_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 a pyscript.reload call completes. On master both cases fail. Without the task.unique() change, the kill_me=True case fails.

🤖 Generated with Claude Code

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>
@craigbarratt
craigbarratt merged commit 1a9015c into master Nov 26, 2026
11 checks passed
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.

task.unique() outside a function can stop automatic reload until Home Assistant restarts

1 participant