diff --git a/pyproject.toml b/pyproject.toml index e0255bbe65b..d913ad0daaf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,6 +258,7 @@ select = [ "SIM401", # Use get from dict with default instead of an if block "T20", # flake8-print "TRY004", # Prefer TypeError exception for invalid type + "RUF006", # Store a reference to the return value of asyncio.create_task "UP", # pyupgrade "W", # pycodestyle ] diff --git a/tests/test_runner.py b/tests/test_runner.py index 11a55031493..c5222cd026e 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -122,18 +122,21 @@ def test_run_does_not_block_forever_with_shielded_task(hass, tmpdir, caplog): async def test_unhandled_exception_traceback(hass, caplog): """Test an unhandled exception gets a traceback in debug mode.""" + raised = asyncio.Event() + async def _unhandled_exception(): + raised.set() raise Exception("This is unhandled") try: hass.loop.set_debug(True) - asyncio.create_task(_unhandled_exception()) + task = asyncio.create_task(_unhandled_exception()) + await raised.wait() + # Delete it without checking result to trigger unhandled exception + del task finally: hass.loop.set_debug(False) - await asyncio.sleep(0) - await asyncio.sleep(0) - assert "Task exception was never retrieved" in caplog.text assert "This is unhandled" in caplog.text assert "_unhandled_exception" in caplog.text