diff --git a/homeassistant/core.py b/homeassistant/core.py index de272beeeea..7fd6006f916 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -288,6 +288,8 @@ class HomeAssistant(object): This method is a coroutine. """ + import homeassistant.helpers.aiohttp_client as aiohttp_client + self.state = CoreState.stopping self.async_track_tasks() self.bus.async_fire(EVENT_HOMEASSISTANT_STOP) @@ -295,6 +297,9 @@ class HomeAssistant(object): self.executor.shutdown() self.state = CoreState.not_running + # cleanup connector pool from aiohttp + yield from aiohttp_client.async_cleanup_websession(self) + # cleanup async layer from python logging if self.data.get(DATA_ASYNCHANDLER): handler = self.data.pop(DATA_ASYNCHANDLER) diff --git a/homeassistant/helpers/aiohttp_client.py b/homeassistant/helpers/aiohttp_client.py index b0bf2b8e1d3..b44b6aebefe 100644 --- a/homeassistant/helpers/aiohttp_client.py +++ b/homeassistant/helpers/aiohttp_client.py @@ -92,33 +92,29 @@ def _async_get_connector(hass, verify_ssl=True): if DATA_CONNECTOR not in hass.data: connector = aiohttp.TCPConnector(loop=hass.loop) hass.data[DATA_CONNECTOR] = connector - - _async_register_connector_shutdown(hass, connector) else: connector = hass.data[DATA_CONNECTOR] else: if DATA_CONNECTOR_NOTVERIFY not in hass.data: connector = aiohttp.TCPConnector(loop=hass.loop, verify_ssl=False) hass.data[DATA_CONNECTOR_NOTVERIFY] = connector - - _async_register_connector_shutdown(hass, connector) else: connector = hass.data[DATA_CONNECTOR_NOTVERIFY] return connector -@callback -# pylint: disable=invalid-name -def _async_register_connector_shutdown(hass, connector): - """Register connector pool close on homeassistant shutdown. +@asyncio.coroutine +def async_cleanup_websession(hass): + """Cleanup aiohttp connector pool. - This method must be run in the event loop. + This method is a coroutine. """ - @asyncio.coroutine - def _async_close_connector(event): - """Close websession on shutdown.""" - yield from connector.close() + tasks = [] + if DATA_CONNECTOR in hass.data: + tasks.append(hass.data[DATA_CONNECTOR].close()) + if DATA_CONNECTOR_NOTVERIFY in hass.data: + tasks.append(hass.data[DATA_CONNECTOR_NOTVERIFY].close()) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_close_connector) + if tasks: + yield from asyncio.wait(tasks, loop=hass.loop)