Add os.walk to asyncio loop blocking detection (#118769)

This commit is contained in:
J. Nick Koston 2024-06-03 23:53:37 -05:00 committed by GitHub
parent 53ab215dfc
commit 553311cc7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

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

View File

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