core
core copied to clipboard
Fix non thread safe state write in acmeda integration
Breaking change
Proposed change
The underlying module uses asyncio and the callback is run within the library's event loop. As such, the following runtime error was being thrown when calling async_write_ha_state:
RuntimeError: Detected that integration 'acmeda' calls async_write_ha_state from a thread other than the event loop, which may cause Home Assistant to crash or data to corrupt. For more information, see https://developers.home-assistant.io/docs/asyncio_thread_safety/#async_write_ha_state at homeassistant/components/acmeda/base.py, line 65: self.async_write_ha_state().
To resolve this, the library is now initialised to use the existing event loop of home assistant. The callbacks are now async so that they are run within the home assistant event loop thereby allowing async_write_ha_state to be called.
Type of change
- [ ] Dependency upgrade
- [x] 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 #123509
- This PR is related to issue:
- Link to documentation pull request:
Checklist
- [x] The code change is tested and works locally.
- [x] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] I have followed the perfect PR recommendations
- [x] 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:
- [x] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [x] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [x] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [x] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
This change was previously included in the pull request https://github.com/home-assistant/core/pull/124960 but has been split out into a dedicated pull request.
@bdraco would you mind checking this out. The library already supports passing in an existing event loop so if I understand what you're saying the better fix would be to re-use the existing home assistant event loop in the library rather than creating a new one? If this is right, how do I get a reference to the home assistant event loop?
@bdraco would you mind checking this out. The library already supports passing in an existing event loop so if I understand what you're saying the better fix would be to re-use the existing home assistant event loop in the library rather than creating a new one? If this is right, how do I get a reference to the home assistant event loop?
Yes that would be much better if possible.
hass.loop or asyncio.get_running_loop()
Yes that would be much better if possible.
hass.looporasyncio.get_running_loop()
OK done, that was surprisingly easy. Only additional thing I had to do was to make the callback async so that it ran within the event loop.
This PR has a merge conflict, could you take a look @atmurray ?
../Frenck
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes. Thank you for your contribution!