Skip to content

Fix nested @pyscript_executor functions so they run in an executor thread - #942

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
jfriend00:nested-pyscript-executor
Dec 4, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
jfriend00:nested-pyscript-executor

Conversation

@jfriend00

Copy link
Copy Markdown
Contributor

Run nested @pyscript_executor functions in an executor thread

What was broken

A @pyscript_executor function 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_executor function defined at module level or in a class body ran in an executor thread, as documented.

import threading

def outer():
    @pyscript_executor
    def inner():
        return threading.current_thread().name

    return inner()

log.warning(f"inner() ran in {outer()}")
# 2.1.1 and 3.0.0:  inner() ran in MainThread
# with this change: inner() ran in SyncWorker_0

Why

ast_functiondef() compiles the function and, for @pyscript_executor, puts an async wrapper that calls Function.hass.async_add_executor_job() in the symbol table. When the name is a local variable of the enclosing function, the if local_var: branch at the end then stores func in that local variable, and func is 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_executor branch now only replaces func with 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 (func1 returns the same thread ident, not a different one), and added a @pyscript_executor function 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.py passes (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

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@craigbarratt
craigbarratt merged commit 294d3f6 into custom-components:master Dec 4, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants