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
Add thread safety checks to async_create_task #116339
Conversation
Calling async_create_task from a thread almost always results in an fast crash. Since most internals are using async_create_background_task or other task APIs, and this is the one integrations seem to get wrong the most, add a thread safety check here
Calling async_create_task from a thread almost always results in an fast crash. Since most internals are using async_create_background_task or other task APIs, and this is the one integrations seem to get wrong the most, add a thread safety check here
…y' into async_create_task_thread_safety
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's give it a shot to see how much things we catch.
Btw, one thing I was wondering is if verify_event_loop_thread
should include a suggestion what method to use instead (for another PR).
I was thinking on having it spit out a url and creating pages on the development site to explain what to do instead for each one. Before I do that I need to move all the ones that are currently hitting on async fire in the other registries sooner so they give a useful suggestion |
* Add thread safety checks to async_create_task Calling async_create_task from a thread almost always results in an fast crash. Since most internals are using async_create_background_task or other task APIs, and this is the one integrations seem to get wrong the most, add a thread safety check here * Add thread safety checks to async_create_task Calling async_create_task from a thread almost always results in an fast crash. Since most internals are using async_create_background_task or other task APIs, and this is the one integrations seem to get wrong the most, add a thread safety check here * missed one * Update homeassistant/core.py * fix mocks * one more internal * more places where internal can be used * more places where internal can be used * more places where internal can be used * internal one more place since this is high volume and was already eager_start
Proposed change
Calling
async_create_task
from a thread other than the event loop thread almost always results in a fast crash. Since most internals are usingasync_create_background_task
or other task APIs, the performance impact of adding a check here is not so bad, and this is the one integrations seem to get wrong the most, add a thread safety check here.Reasoning:
eager_start
is now the default and any unsafe thread operations start right away, so mistakes (generally in custom components) calling from the wrong thread have a higher impact. While it’s nicer to crash quickly so mistakes can be found as opposed to crashing randomly, which makes for hard-to-find issues, in the short term, we need a way to catch these mistakes in integrations (custom components) even if there is a performance cost.We turned on asyncio debug in April 2024 in the dev containers in the hope of catching some of the issues that have been reported. It will take a while to get all the issues fixed in custom components.
In 2025.5 we should guard the
verify_event_loop_thread
check with a check for thehass.config.debug
flag being set as long term we don't want to be checking this in production environments since it is a performance hit. For context, the run time of theverify_event_loop_thread
is slightly faster thanasyncio.get_running_loop()
so the while the performance hit isn't large its still undesirable.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: