Skip to content

Commit

Permalink
Ensure name of task is logged for unhandled loop exceptions (#118822)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 4, 2024
1 parent 709e32a commit 72e4aee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions homeassistant/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)

Expand Down
7 changes: 4 additions & 3 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 72e4aee

Please sign in to comment.