Repository navigation
Fix nested @pyscript_executor functions so they run in an executor thread - #942
Merged
craigbarratt merged 1 commit intoDec 4, 2026
Merged
Conversation
craigbarratt
approved these changes
Dec 4, 2026
craigbarratt
left a comment
Member
There was a problem hiding this comment.
Thanks @jfriend00, nice find, and good catch on the old thread test. Verified that the new cases run in the event loop on master and in an executor thread with this change, including through a closure. Approving.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Run nested @pyscript_executor functions in an executor thread
What was broken
A
@pyscript_executorfunction defined inside another function or a method ran in Home Assistant's event loop, not in an executor thread, so blocking I/O in it blocked Home Assistant, which is what the decorator is meant to prevent. 2.1.1 does this too, so it isn't new in 3.0.0. A@pyscript_executorfunction defined at module level or in a class body ran in an executor thread, as documented.Why
ast_functiondef()compiles the function and, for@pyscript_executor, puts an async wrapper that callsFunction.hass.async_add_executor_job()in the symbol table. When the name is a local variable of the enclosing function, theif local_var:branch at the end then storesfuncin that local variable, andfuncis still the unwrapped function, so the wrapper is lost. At module level and in a class body there's no local variable, so the wrapper stays.What I changed
The
@pyscript_executorbranch now only replacesfuncwith the wrapper, and the name is stored in one place at the end: in the local variable's cell when there is one, otherwise in the symbol table. Two comments explain why the cell is put back.Testing
The thread test in
tests/test_unit_eval.py, added with@pyscript_executor, says its second function "will run in a different thread", but both of its functions use@pyscript_compile, and its source ends with a literal[True, True], so it passed whatever thread they ran in. I changed the second function to@pyscript_executor, removed the literal so the comparisons are the result, corrected the first comment (func1returns the same thread ident, not a different one), and added a@pyscript_executorfunction defined inside a function and one inside a method. Without the change, the two new cases fail; with it, all four pass.tests/test_unit_eval.pypasses (36 tests), and ruff, codespell, pylint and mypy are clean. This ran on Windows with local stand-ins for Home Assistant's Linux-only imports, so CI is its first Linux run.Assisted by Claude Code