Ensure name of task is logged for unhandled loop exceptions (#118822)

This commit is contained in:
J. Nick Koston 2024-06-04 11:48:29 -05:00 committed by Franck Nijhof
parent 6e30fd7633
commit b02c9aa2ef
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 8 additions and 5 deletions

View file

@ -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]
)

View file

@ -115,11 +115,11 @@ def test_run_does_not_block_forever_with_shielded_task(
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 test_unhandled_exception_traceback(
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 test_unhandled_exception_traceback(
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: