From 1e74952cbc71af29d3ee1137bdfa316c0e36936b Mon Sep 17 00:00:00 2001 From: Zedas030 <75276424+Zedas030@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:24:59 +0100 Subject: [PATCH 1/2] Skip the function finalizer once HA has closed the event loop The weakref finalizer on a function's EvalFuncVar also ran from atexit, after HA had closed the loop, so a manager still RUNNING called hass.async_create_task() on a closed loop and logged a traceback at shutdown. Don't run it at exit, and return early when the loop is already closed. Fixes #880. --- custom_components/pyscript/decorator.py | 7 ++-- tests/test_decorator_manager.py | 48 ++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/custom_components/pyscript/decorator.py b/custom_components/pyscript/decorator.py index cd5fbc4c..28345058 100644 --- a/custom_components/pyscript/decorator.py +++ b/custom_components/pyscript/decorator.py @@ -249,10 +249,13 @@ def __init__(self, ast_ctx: AstEval, eval_func_var: EvalFuncVar) -> None: self.logger = self.eval_func.logger def on_func_var_deleted(): - if self.status is DecoratorManagerStatus.RUNNING: + # once HA has closed the loop at shutdown there is nothing left to stop + if self.status is DecoratorManagerStatus.RUNNING and not self.hass.loop.is_closed(): self.hass.async_create_task(self.safe_await(self.stop())) - weakref.finalize(eval_func_var, on_func_var_deleted) + # not at interpreter exit either, the loop is closed by then + self.func_var_finalizer = weakref.finalize(eval_func_var, on_func_var_deleted) + self.func_var_finalizer.atexit = False async def _call(self, data: DispatchData) -> None: handlers = self.get_decorators(CallHandlerDecorator) diff --git a/tests/test_decorator_manager.py b/tests/test_decorator_manager.py index fd8e28e6..021ede1e 100644 --- a/tests/test_decorator_manager.py +++ b/tests/test_decorator_manager.py @@ -3,10 +3,13 @@ from __future__ import annotations import ast +import asyncio from collections.abc import Awaitable +import gc import logging +from types import SimpleNamespace from typing import Any, ClassVar -from unittest.mock import patch +from unittest.mock import Mock, patch import pytest import voluptuous as vol @@ -844,6 +847,49 @@ async def handle_call_result(self, data: DispatchData, result: object) -> None: assert ast_ctx.logged_exceptions == [handler_bug] +@pytest.mark.asyncio +async def test_function_decorator_manager_stops_when_function_is_deleted(hass): + """Deleting the function of a running manager should stop the manager.""" + DecoratorManager.hass = hass + func_var = DummyEvalFuncVar() + manager = FunctionDecoratorManager(DummyAstCtx(), func_var) + manager.update_status(DecoratorManagerStatus.RUNNING) + + del func_var + gc.collect() + await hass.async_block_till_done() + + assert manager.status is DecoratorManagerStatus.STOPPED + + +def test_function_decorator_manager_finalizer_skips_closed_loop(): + """A running manager whose function goes away after HA closed the loop must not schedule stop().""" + closed_loop = asyncio.new_event_loop() + closed_loop.close() + create_task = Mock() + DecoratorManager.hass = SimpleNamespace(loop=closed_loop, async_create_task=create_task) + func_var = DummyEvalFuncVar() + manager = FunctionDecoratorManager(DummyAstCtx(), func_var) + manager.update_status(DecoratorManagerStatus.RUNNING) + + del func_var + gc.collect() + + create_task.assert_not_called() + assert manager.status is DecoratorManagerStatus.RUNNING + + +@pytest.mark.asyncio +async def test_function_decorator_manager_finalizer_not_run_at_exit(hass): + """The finalizer should not run from atexit, after HA has closed the loop.""" + DecoratorManager.hass = hass + func_var = DummyEvalFuncVar() + manager = FunctionDecoratorManager(DummyAstCtx(), func_var) + + assert manager.func_var_finalizer.alive + assert manager.func_var_finalizer.atexit is False + + @pytest.mark.asyncio async def test_call_result_handler_default_handle_call_exception_forwards_none(): """Default handle_call_exception should forward the call to handle_call_result with None.""" From 822557e96a59f5a2ebb7e439bdb2c6b83660934f Mon Sep 17 00:00:00 2001 From: Zedas030 <75276424+Zedas030@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:56:05 +0100 Subject: [PATCH 2/2] Hand the function finalizer off to the loop thread-safely The cyclic garbage collector can run the weakref finalizer in any thread, including HA's executor threads, where calling hass.async_create_task() trips HA's thread-safety check and the manager is never stopped. Schedule the stop through loop.call_soon_threadsafe() instead, which also raises RuntimeError once the loop is closed, so the separate is_closed() check and its race go away. As suggested in review. --- custom_components/pyscript/decorator.py | 13 ++++++++++--- tests/test_decorator_manager.py | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/custom_components/pyscript/decorator.py b/custom_components/pyscript/decorator.py index 28345058..d7efd48f 100644 --- a/custom_components/pyscript/decorator.py +++ b/custom_components/pyscript/decorator.py @@ -248,11 +248,18 @@ def __init__(self, ast_ctx: AstEval, eval_func_var: EvalFuncVar) -> None: self.logger = self.eval_func.logger - def on_func_var_deleted(): - # once HA has closed the loop at shutdown there is nothing left to stop - if self.status is DecoratorManagerStatus.RUNNING and not self.hass.loop.is_closed(): + def stop_deleted_func(): + if self.status is DecoratorManagerStatus.RUNNING: self.hass.async_create_task(self.safe_await(self.stop())) + def on_func_var_deleted(): + # garbage collection can run this in any thread, so hand off to the loop + try: + self.hass.loop.call_soon_threadsafe(stop_deleted_func) + except RuntimeError: + # once HA has closed the loop at shutdown there is nothing left to stop + pass + # not at interpreter exit either, the loop is closed by then self.func_var_finalizer = weakref.finalize(eval_func_var, on_func_var_deleted) self.func_var_finalizer.atexit = False diff --git a/tests/test_decorator_manager.py b/tests/test_decorator_manager.py index 021ede1e..9b6ffa71 100644 --- a/tests/test_decorator_manager.py +++ b/tests/test_decorator_manager.py @@ -890,6 +890,25 @@ async def test_function_decorator_manager_finalizer_not_run_at_exit(hass): assert manager.func_var_finalizer.atexit is False +@pytest.mark.asyncio +async def test_function_decorator_manager_finalizer_in_worker_thread(hass, caplog): + """Deleting the function in a worker thread should stop the manager via the loop.""" + DecoratorManager.hass = hass + holder = [DummyEvalFuncVar()] + manager = FunctionDecoratorManager(DummyAstCtx(), holder[0]) + manager.update_status(DecoratorManagerStatus.RUNNING) + + def drop(): + holder.clear() + gc.collect() + + await hass.async_add_executor_job(drop) + await hass.async_block_till_done() + + assert manager.status is DecoratorManagerStatus.STOPPED + assert "thread other than the event loop" not in caplog.text + + @pytest.mark.asyncio async def test_call_result_handler_default_handle_call_exception_forwards_none(): """Default handle_call_exception should forward the call to handle_call_result with None."""