Bugfix aiohttp connector pool close on shutdown (#5190)

* Bugfix aiohttp connector pool close on shutdown

* fix circular import

* remove lint disable
This commit is contained in:
Pascal Vizeli 2017-01-05 23:09:04 +01:00 committed by Paulus Schoutsen
parent a36ca62445
commit 50a8ec7335
2 changed files with 16 additions and 15 deletions

View file

@ -288,6 +288,8 @@ class HomeAssistant(object):
This method is a coroutine. This method is a coroutine.
""" """
import homeassistant.helpers.aiohttp_client as aiohttp_client
self.state = CoreState.stopping self.state = CoreState.stopping
self.async_track_tasks() self.async_track_tasks()
self.bus.async_fire(EVENT_HOMEASSISTANT_STOP) self.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
@ -295,6 +297,9 @@ class HomeAssistant(object):
self.executor.shutdown() self.executor.shutdown()
self.state = CoreState.not_running self.state = CoreState.not_running
# cleanup connector pool from aiohttp
yield from aiohttp_client.async_cleanup_websession(self)
# cleanup async layer from python logging # cleanup async layer from python logging
if self.data.get(DATA_ASYNCHANDLER): if self.data.get(DATA_ASYNCHANDLER):
handler = self.data.pop(DATA_ASYNCHANDLER) handler = self.data.pop(DATA_ASYNCHANDLER)

View file

@ -92,33 +92,29 @@ def _async_get_connector(hass, verify_ssl=True):
if DATA_CONNECTOR not in hass.data: if DATA_CONNECTOR not in hass.data:
connector = aiohttp.TCPConnector(loop=hass.loop) connector = aiohttp.TCPConnector(loop=hass.loop)
hass.data[DATA_CONNECTOR] = connector hass.data[DATA_CONNECTOR] = connector
_async_register_connector_shutdown(hass, connector)
else: else:
connector = hass.data[DATA_CONNECTOR] connector = hass.data[DATA_CONNECTOR]
else: else:
if DATA_CONNECTOR_NOTVERIFY not in hass.data: if DATA_CONNECTOR_NOTVERIFY not in hass.data:
connector = aiohttp.TCPConnector(loop=hass.loop, verify_ssl=False) connector = aiohttp.TCPConnector(loop=hass.loop, verify_ssl=False)
hass.data[DATA_CONNECTOR_NOTVERIFY] = connector hass.data[DATA_CONNECTOR_NOTVERIFY] = connector
_async_register_connector_shutdown(hass, connector)
else: else:
connector = hass.data[DATA_CONNECTOR_NOTVERIFY] connector = hass.data[DATA_CONNECTOR_NOTVERIFY]
return connector return connector
@callback @asyncio.coroutine
# pylint: disable=invalid-name def async_cleanup_websession(hass):
def _async_register_connector_shutdown(hass, connector): """Cleanup aiohttp connector pool.
"""Register connector pool close on homeassistant shutdown.
This method must be run in the event loop. This method is a coroutine.
""" """
@asyncio.coroutine tasks = []
def _async_close_connector(event): if DATA_CONNECTOR in hass.data:
"""Close websession on shutdown.""" tasks.append(hass.data[DATA_CONNECTOR].close())
yield from connector.close() if DATA_CONNECTOR_NOTVERIFY in hass.data:
tasks.append(hass.data[DATA_CONNECTOR_NOTVERIFY].close())
hass.bus.async_listen_once( if tasks:
EVENT_HOMEASSISTANT_STOP, _async_close_connector) yield from asyncio.wait(tasks, loop=hass.loop)