diff --git a/hassio/api/__init__.py b/hassio/api/__init__.py index c5cf8eee6..b07aa00ea 100644 --- a/hassio/api/__init__.py +++ b/hassio/api/__init__.py @@ -235,13 +235,16 @@ class RestAPI(CoreSysAttributes): async def start(self): """Run rest api webserver.""" await self._runner.setup() - self._site = web.TCPSite(self._runner, "0.0.0.0", 80) + self._site = web.TCPSite( + self._runner, host="0.0.0.0", port=80, shutdown_timeout=5) try: await self._site.start() except OSError as err: _LOGGER.fatal( "Failed to create HTTP server at 0.0.0.0:80 -> %s", err) + else: + _LOGGER.info("Start API on %s", self.sys_docker.network.supervisor) async def stop(self): """Stop rest api webserver.""" @@ -251,3 +254,5 @@ class RestAPI(CoreSysAttributes): # Shutdown running API await self._site.stop() await self._runner.cleanup() + + _LOGGER.info("Stop API on %s", self.sys_docker.network.supervisor) diff --git a/hassio/core.py b/hassio/core.py index 656ab0cf8..2687a299d 100644 --- a/hassio/core.py +++ b/hassio/core.py @@ -3,6 +3,8 @@ from contextlib import suppress import asyncio import logging +import async_timeout + from .coresys import CoreSysAttributes from .const import ( STARTUP_SYSTEM, STARTUP_SERVICES, STARTUP_APPLICATION, STARTUP_INITIALIZE) @@ -65,7 +67,6 @@ class HassIO(CoreSysAttributes): # start api await self.sys_api.start() - _LOGGER.info("Start API on %s", self.sys_docker.network.supervisor) # start addon mark as initialize await self.sys_addons.boot(STARTUP_INITIALIZE) @@ -113,12 +114,18 @@ class HassIO(CoreSysAttributes): self.sys_scheduler.suspend = True # process async stop tasks - await asyncio.wait([ - self.sys_api.stop(), - self.sys_dns.stop(), - self.sys_websession.close(), - self.sys_websession_ssl.close() - ]) + try: + with async_timeout.timeout(10): + await asyncio.wait([ + self.sys_api.stop(), + self.sys_dns.stop(), + self.sys_websession.close(), + self.sys_websession_ssl.close() + ]) + except asyncio.TimeoutError: + _LOGGER.warning("Force Shutdown!") + + _LOGGER.info("Hass.io is down") async def shutdown(self): """Shutdown all running containers in correct order.""" diff --git a/hassio/misc/dns.py b/hassio/misc/dns.py index c7ea30b50..10ef406de 100644 --- a/hassio/misc/dns.py +++ b/hassio/misc/dns.py @@ -3,6 +3,8 @@ import asyncio import logging import shlex +import async_timeout + _LOGGER = logging.getLogger(__name__) COMMAND = "socat UDP-RECVFROM:53,fork UDP-SENDTO:127.0.0.11:53" @@ -38,5 +40,10 @@ class DNSForward: return self.proc.kill() - await self.proc.wait() + try: + with async_timeout.timeout(5): + await self.proc.wait() + except asyncio.TimeoutError: + _LOGGER.warning("Stop waiting for DNS shutdown") + _LOGGER.info("Stop DNS forwarding")