8000 Hold a lock to prevent concurrent setup of config entries by bdraco · Pull Request #116482 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Hold a lock to pre 8000 vent concurrent setup of config entries #116482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 30, 2024

Conversation

bdraco
Copy link
Member
@bdraco bdraco commented Apr 30, 2024

Proposed change

If something triggers a config entry to reload while we are still in the setup of the underlying component/integration, the lock was not held because async_setup_component does not hold the reload lock. We only had a lock to prevent concurrent reloads, and there wasn't anything to handle a reload during startup. The common case is that there is a config entry update listener and it triggers a reload because something during its setup updates its config entry data.

This has been a problem for a long time but it wasn't fast enough until 2024.5.x for us to see this happen on a regular basis.

config_entries.async_remove should also probably hold the lock to unload the entry and remove it but I didn't want to make that change in this PR as the problem is far less likely to happen

Rename the reload lock to setup lock.

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

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_requiremen 8000 ts_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:

If something triggers a config entry to reload while we
are still in the setup of the underlying component/integration
the lock was not held because async_setup_component does not
hold the reload lock.

Rename the reload lock to setup lock.
@bdraco bdraco added this to the 2024.5.0 milestone Apr 30, 2024
Comment on lines +1795 to +1801
if entry.domain not in self.hass.config.components:
# If the component is not loaded, just load it as
# the config entry will be loaded as well. We need
# to do this before holding the lock to avoid a
# deadlock.
await async_setup_component(self.hass, entry.domain, self._hass_config)
return entry.state is ConfigEntryState.LOADED
Copy link
Member Author

Choose a reason for hiding this comment

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

This is exactly what self.async_setup(entry_id) does, but we need to do it without holding the lock since async_setup_component will hold the lock

@bdraco
Copy link
Member Author
bdraco commented Apr 30, 2024

testing this on my production now.

I want to add an explicit test for the case where we have a long setup time and we trigger a reload

@bdraco
Copy link
Member Author
bdraco commented Apr 30, 2024

I also need to profile this

@bdraco
Copy link
Member Author
bdraco commented Apr 30, 2024

profile is good

pushed to all production

@bdraco
Copy link
Member Author
bdraco commented Apr 30, 2024

I've beat this up as much as I can on my production and dev envs.

@bdraco bdraco marked this pull request as ready for review April 30, 2024 23:47
@bdraco bdraco requested review from emontnemery, jbouwh and a team as code owners April 30, 2024 23:47
@bdraco bdraco merged commit 6cf1c5c into dev Apr 30, 2024
34 checks passed
@bdraco bdraco deleted the hold_lock_to_prevent_concurrent_setup branch April 30, 2024 23:47
@github-actions github-actions bot locked and limited conversation to collaborators May 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants
0