From 553311cc7d6e5c312e6123aaff0643757bce65b4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 3 Jun 2024 23:53:37 -0500 Subject: [PATCH] Add os.walk to asyncio loop blocking detection (#118769) --- homeassistant/block_async_io.py | 3 +++ tests/test_block_async_io.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/homeassistant/block_async_io.py b/homeassistant/block_async_io.py index e829ed4925b..2dc94fa456a 100644 --- a/homeassistant/block_async_io.py +++ b/homeassistant/block_async_io.py @@ -67,6 +67,9 @@ def enable() -> None: glob.iglob = protect_loop( glob.iglob, strict_core=False, strict=False, loop_thread_id=loop_thread_id ) + os.walk = protect_loop( + os.walk, strict_core=False, strict=False, loop_thread_id=loop_thread_id + ) if not _IN_TESTS: # Prevent files being opened inside the event loop diff --git a/tests/test_block_async_io.py b/tests/test_block_async_io.py index e4f248e80d1..1ceb84c249f 100644 --- a/tests/test_block_async_io.py +++ b/tests/test_block_async_io.py @@ -275,7 +275,7 @@ async def test_protect_loop_scandir( caplog.clear() with contextlib.suppress(FileNotFoundError): await hass.async_add_executor_job(os.scandir, "/path/that/does/not/exists") - assert "Detected blocking call to listdir with args" not in caplog.text + assert "Detected blocking call to scandir with args" not in caplog.text async def test_protect_loop_listdir( @@ -290,3 +290,17 @@ async def test_protect_loop_listdir( with contextlib.suppress(FileNotFoundError): await hass.async_add_executor_job(os.listdir, "/path/that/does/not/exists") assert "Detected blocking call to listdir with args" not in caplog.text + + +async def test_protect_loop_walk( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test glob calls in the loop are logged.""" + block_async_io.enable() + with contextlib.suppress(FileNotFoundError): + os.walk("/path/that/does/not/exists") + assert "Detected blocking call to walk with args" in caplog.text + caplog.clear() + with contextlib.suppress(FileNotFoundError): + await hass.async_add_executor_job(os.walk, "/path/that/does/not/exists") + assert "Detected blocking call to walk with args" not in caplog.text