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.
"""
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)

View file

@ -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)