diff --git a/homeassistant/block_async_io.py b/homeassistant/block_async_io.py index a8770c7e9c4..090afed822d 100644 --- a/homeassistant/block_async_io.py +++ b/homeassistant/block_async_io.py @@ -160,7 +160,7 @@ _BLOCKED_CALLS = BlockedCalls(set()) def enable() -> None: """Enable the detection of blocking calls in the event loop.""" if _BLOCKED_CALLS.calls: - return + raise RuntimeError("Blocking call detection is already enabled") loop_thread_id = threading.get_ident() for blocking_call in BLOCKING_CALLS: diff --git a/tests/test_block_async_io.py b/tests/test_block_async_io.py index efbc1a69a61..4f9b5a11378 100644 --- a/tests/test_block_async_io.py +++ b/tests/test_block_async_io.py @@ -229,6 +229,17 @@ async def test_protect_open(caplog: pytest.LogCaptureFixture) -> None: assert "Detected blocking call to open with args" in caplog.text +async def test_enable_multiple_times(caplog: pytest.LogCaptureFixture) -> None: + """Test trying to enable multiple times.""" + with patch.object(block_async_io, "_IN_TESTS", False): + block_async_io.enable() + + with pytest.raises( + RuntimeError, match="Blocking call detection is already enabled" + ): + block_async_io.enable() + + @pytest.mark.parametrize( "path", [