Refactor async_get_hass to rely on threading.local instead of a ContextVar#96005
Conversation
|
Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
emontnemery
left a comment
There was a problem hiding this comment.
This can't possibly be the responsibility of MQTT.
I think we should instead do this in the non-async methods on the hass object for scheduling jobs:
hass.create_task
hass.add_job
All their methods finally call |
eb7c3fa to
74a40a3
Compare
|
Do the new tests fail for the old code? |
|
Would be nice to have a PR with a commit adding the new tests first, failing them. Then update the code under test in a second commit and see the tests pass. |
True, but may be we need to split tests, as they can influence each other if they are in one test |
Only the Threaded examples, so the if could be moved below and still let the test pass. |
That's fine. I just meant that it would be nice to see a commit history in the PR where the first commits add the tests and the existing code under test fails the new tests. And then we update the code under test in the last commit and all the tests pass. |
Check is added |
|
Check again if this solves the issue in the case with MQTT and that works okay too. |
|
When we're happy with this PR, I'd like to suggest we create a new PR with just two commits, first being new tests, second being code fix, that we can check out locally and validate with and without fix applied. |
1e1d0c1 to
615d963
Compare
2c9c9bb to
950244f
Compare
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
emontnemery
left a comment
There was a problem hiding this comment.
I think this is a more suitable solution than a context variable; the thread local data is only accessible to the event loop, and that perfectly matches the definition that async_*-functions should only ever be called from the event loop.
Thanks a lot @jbouwh 👍
|
Great! Now I think we can rebase and make two commits so we can verify locally. |
d4a8907 to
d6903a6
Compare
|
There was one bug in the test. The PR is rebased and commits for test and fix are splitted now. |
Proposed change
Refactor
async_get_hassto rely onthreading.localinstead of aContextVarProblem
async_get_hassallows access to the globalHomeAssistantinstance which is stored in aContextVar.The context is not copied between threads, which means the context is lost when a worker thread or an executor submits a job or task to the event loop. For example, this meant
async_get_hassfailed when an automation was triggered by an MQTT message, because the MQTT client runs in its own worker thread.Solution
Let the event look store the
HomeAssistantinstance in athreading.localinstance instead.Type of change
Additional information
Checklist
black --fast 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: