Skip to content

Fix async_unload teardown race in scripts#169562

Draft
arturpragacz wants to merge 4 commits intohome-assistant:devfrom
arturpragacz:h/script/fix-async-unload-teardown-race
Draft

Fix async_unload teardown race in scripts#169562
arturpragacz wants to merge 4 commits intohome-assistant:devfrom
arturpragacz:h/script/fix-async-unload-teardown-race

Conversation

@arturpragacz
Copy link
Copy Markdown
Member

Proposed change

Fix async_unload teardown race in scripts.

Script.async_unload could race: async_stop snapshots _runs once, so a fresh async_run() slipping in during the await left _runs non-empty, raised RuntimeError, and leaked the script in hass.data[DATA_SCRIPTS].

To fix, make async_unload async and set _unloaded = True before draining, fencing off new runs at the existing async_run guard.

async_unload was added in #169036.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (script) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of script can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign script Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (automation) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of automation can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign automation Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (websocket_api) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of websocket_api can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign websocket_api Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @ntilley905, mind taking a look at this pull request as it has been labeled with an integration (wake_on_lan) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of wake_on_lan can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign wake_on_lan Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a teardown race in homeassistant.helpers.script.Script by making async_unload a coroutine that fences off new runs before draining in-flight runs and cleaning up resources.

Changes:

  • Convert Script.async_unload to async def, set _unloaded = True before stopping, and move shutdown fencing earlier in async_run.
  • Update core call sites to await script.async_unload() where scripts are torn down.
  • Adjust and extend tests to reflect the new unload semantics (including unloading while a run is in-flight).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
homeassistant/helpers/script.py Makes script unloading async, blocks new runs before draining, and introduces _async_unload for synchronous teardown.
homeassistant/components/websocket_api/commands.py Awaits script unload in websocket “execute_script” cleanup.
homeassistant/components/wake_on_lan/switch.py Awaits unload of the WOL off-script during entity removal.
homeassistant/components/script/init.py Awaits unload of script entities when removed.
homeassistant/components/intent_script/init.py Switches intent action teardown to a single awaited unload.
homeassistant/components/automation/init.py Awaits unload of automation action script on entity removal.
tests/helpers/test_script.py Updates unload-related tests and adds coverage for unloading while running.
tests/components/wake_on_lan/test_switch.py Updates expectations around script stop/unload during entity removal.

Comment thread tests/helpers/test_script.py
Comment thread homeassistant/components/wake_on_lan/switch.py Outdated
Comment thread homeassistant/components/script/__init__.py Outdated
Comment thread tests/components/wake_on_lan/test_switch.py Outdated
@emontnemery emontnemery marked this pull request as draft April 30, 2026 18:28
Copilot AI review requested due to automatic review settings April 30, 2026 18:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Comment thread tests/components/wake_on_lan/test_switch.py Outdated
Comment on lines +1796 to +1801
if DATA_NEW_SCRIPT_RUNS_NOT_ALLOWED in self._hass.data:
self._log("Home Assistant is shutting down, starting script blocked")
return None
# The fences above rely on there being no await between these checks
# and the _runs.append below, so that setting either flag is
# sufficient to block new runs from being added.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For context: Moved because we should check this before checking the Context

Comment on lines +1924 to +1925
if self._unloaded:
return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need to allow unload more than once?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The race fixed by this PR should be asserted by test(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants