Avoid creating tasks to remove entities#110967
Merged
Merged
Conversation
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
Member
Author
|
The systemmonitor config entry is now reloaded in time so the test fails. We could schedule it instead |
…_normally_suspends
…suspends' into entity_removal_never_normally_suspends
bdraco
commented
Feb 19, 2024
bdraco
commented
Feb 19, 2024
bdraco
commented
Feb 19, 2024
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`
20 tasks
If these tests fail they will not show anything other than AssertionError without the rewrite they are currently failing in #110978 and I have not been able to find the cause
balloob
approved these changes
Feb 21, 2024
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
need to fix another race first #110978Proposed change
Removing entities already runs in a task (mostly commonly from
core/homeassistant/config_entries.py
Line 1715 in 17ba96f
async_removewill ever need to suspend since the only place that can do cause the call to suspend is inasync_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 sinceasyncio.gatherdoes not cancel the other tasks when one raises:Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.To help with the load of incoming pull requests: