Skip to content

Avoid creating tasks to remove entities#110967

Merged
bdraco merged 30 commits into
devfrom
entity_removal_never_normally_suspends
Feb 21, 2024
Merged

Avoid creating tasks to remove entities#110967
bdraco merged 30 commits into
devfrom
entity_removal_never_normally_suspends

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented Feb 19, 2024

need to fix another race first #110978

Proposed change

Removing entities already runs in a task (mostly commonly from

asyncio.create_task(
) so we can avoid creating tasks inside that task to remove each entity as its rare that calling async_remove will ever need to suspend since the only place that can do cause the call to suspend is in async_will_remove_from_hass. This also fixes a race where if one of the entities raised while it was being removed, we would continue on and the platform would be in an indeterminate state since asyncio.gather does not cancel the other tasks when one raises:

If return_exceptions is False (default), the first raised exception is immediately propagated to the task that awaits on gather(). Other awaitables in the aws sequence won’t be cancelled and will continue to run.

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:

Checklist

  • 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.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Its rare that calling `async_remove` will ever need to suspend since
the only place that can do cause the call to return control to the event
loop is async_will_remove_from_hass
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Feb 19, 2024

The systemmonitor config entry is now reloaded in time so the test fails. We could schedule it instead

Comment thread homeassistant/components/systemmonitor/repairs.py Outdated
Comment thread homeassistant/helpers/entity.py
Comment thread homeassistant/helpers/entity.py
There was a race here because `_async_registry_updated` would previously run in a task and `self.registry_entry` would not get unset soon enough so it would write unavailable state instead of removing the entity.

I've seen this problem happen in production but I never could figure out why until now since its much more likely/reproducible as there are less tasks in play.  Any time the `_async_registry_updated` ran after the `async_remove` task, the entity wouldn't get removed and the state would get written as unavailable instead because `self.registry_entry` hasn't been unset yet. To avoid this and not break the api (`self.async_removed_from_registry` might still need to access `self.registry_entry`) a new flag is added called `self._removed_from_registry`
@bdraco bdraco marked this pull request as ready for review February 20, 2024 22:17
@bdraco bdraco requested a review from a team as a code owner February 20, 2024 22:17
@bdraco bdraco merged commit 1a02330 into dev Feb 21, 2024
@bdraco bdraco deleted the entity_removal_never_normally_suspends branch February 21, 2024 03:32
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants