diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 66312f7283a..3877a6bf6e1 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -564,6 +564,14 @@ async def _async_set_up_integrations( except asyncio.TimeoutError: _LOGGER.warning("Setup timed out for stage 2 - moving forward") + # Wrap up startup + _LOGGER.debug("Waiting for startup to wrap up") + try: + async with hass.timeout.async_timeout(WRAP_UP_TIMEOUT, cool_down=COOLDOWN_TIME): + await hass.async_block_till_done() + except asyncio.TimeoutError: + _LOGGER.warning("Setup timed out for bootstrap - moving forward") + watch_task.cancel() async_dispatcher_send(hass, SIGNAL_BOOTSTRAP_INTEGRATONS, {}) @@ -576,11 +584,3 @@ async def _async_set_up_integrations( ) }, ) - - # Wrap up startup - _LOGGER.debug("Waiting for startup to wrap up") - try: - async with hass.timeout.async_timeout(WRAP_UP_TIMEOUT, cool_down=COOLDOWN_TIME): - await hass.async_block_till_done() - except asyncio.TimeoutError: - _LOGGER.warning("Setup timed out for bootstrap - moving forward") diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 3eeb06d056c..1ad64e58bd7 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -678,6 +678,7 @@ async def test_empty_integrations_list_is_only_sent_at_the_end_of_bootstrap(hass await bootstrap._async_set_up_integrations( hass, {"normal_integration": {}, "an_after_dep": {}} ) + await hass.async_block_till_done() assert integrations[0] != {} assert "an_after_dep" in integrations[0] @@ -686,3 +687,35 @@ async def test_empty_integrations_list_is_only_sent_at_the_end_of_bootstrap(hass assert "normal_integration" in hass.config.components assert order == ["an_after_dep", "normal_integration"] + + +@pytest.mark.parametrize("load_registries", [False]) +async def test_warning_logged_on_wrap_up_timeout(hass, caplog): + """Test we log a warning on bootstrap timeout.""" + + def gen_domain_setup(domain): + async def async_setup(hass, config): + await asyncio.sleep(0.1) + + async def _background_task(): + await asyncio.sleep(0.2) + + await hass.async_create_task(_background_task()) + return True + + return async_setup + + mock_integration( + hass, + MockModule( + domain="normal_integration", + async_setup=gen_domain_setup("normal_integration"), + partial_manifest={}, + ), + ) + + with patch.object(bootstrap, "WRAP_UP_TIMEOUT", 0): + await bootstrap._async_set_up_integrations(hass, {"normal_integration": {}}) + await hass.async_block_till_done() + + assert "Setup timed out for bootstrap - moving forward" in caplog.text