From 72e4aee155affe8c1b0d9a650227aee5acf7eb43 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 4 Jun 2024 11:48:29 -0500 Subject: [PATCH] Ensure name of task is logged for unhandled loop exceptions (#118822) --- homeassistant/runner.py | 6 ++++-- tests/test_runner.py | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/runner.py b/homeassistant/runner.py index 523dafdecf3e28..a1510336302c1e 100644 --- a/homeassistant/runner.py +++ b/homeassistant/runner.py @@ -137,16 +137,18 @@ def _async_loop_exception_handler(_: Any, context: dict[str, Any]) -> None: if source_traceback := context.get("source_traceback"): stack_summary = "".join(traceback.format_list(source_traceback)) logger.error( - "Error doing job: %s: %s", + "Error doing job: %s (%s): %s", context["message"], + context.get("task"), stack_summary, **kwargs, # type: ignore[arg-type] ) return logger.error( - "Error doing job: %s", + "Error doing job: %s (%s)", context["message"], + context.get("task"), **kwargs, # type: ignore[arg-type] ) diff --git a/tests/test_runner.py b/tests/test_runner.py index 79768aaf7cf136..a4bec12bc0d932 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -115,11 +115,11 @@ async def async_shielded(*_): tasks.append(asyncio.ensure_future(asyncio.shield(async_shielded()))) tasks.append(asyncio.ensure_future(asyncio.sleep(2))) tasks.append(asyncio.ensure_future(async_raise())) - await asyncio.sleep(0.1) + await asyncio.sleep(0) return 0 with ( - patch.object(runner, "TASK_CANCELATION_TIMEOUT", 1), + patch.object(runner, "TASK_CANCELATION_TIMEOUT", 0.1), patch("homeassistant.bootstrap.async_setup_hass", return_value=hass), patch("threading._shutdown"), patch("homeassistant.core.HomeAssistant.async_run", _async_create_tasks), @@ -145,7 +145,7 @@ async def _unhandled_exception(): try: hass.loop.set_debug(True) - task = asyncio.create_task(_unhandled_exception()) + task = asyncio.create_task(_unhandled_exception(), name="name_of_task") await raised.wait() # Delete it without checking result to trigger unhandled exception del task @@ -155,6 +155,7 @@ async def _unhandled_exception(): assert "Task exception was never retrieved" in caplog.text assert "This is unhandled" in caplog.text assert "_unhandled_exception" in caplog.text + assert "name_of_task" in caplog.text def test_enable_posix_spawn() -> None: