1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Patch aiohttp client session close (#34769)

* Patch aiohttp client session close

* Add test

* Restore close regardless of auto_cleanup

* Close session instead of detaching and do not restore

* Delint test

* Add frame helper

* Use frame helper

* Test warning log when closing session

* Clean up

* Correct docstring

* Do not change shutdown

* Fix tests
This commit is contained in:
Martin Hjelmare
2020-05-13 09:58:33 +02:00
committed by GitHub
parent 2f6da20065
commit 2a120d9045
4 changed files with 205 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
"""Helper for aiohttp webclient stuff."""
import asyncio
import logging
from ssl import SSLContext
import sys
from typing import Any, Awaitable, Optional, Union, cast
@@ -12,10 +13,13 @@ import async_timeout
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE, __version__
from homeassistant.core import Event, callback
from homeassistant.helpers.frame import MissingIntegrationFrame, get_integration_frame
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.loader import bind_hass
from homeassistant.util import ssl as ssl_util
_LOGGER = logging.getLogger(__name__)
DATA_CONNECTOR = "aiohttp_connector"
DATA_CONNECTOR_NOTVERIFY = "aiohttp_connector_notverify"
DATA_CLIENTSESSION = "aiohttp_clientsession"
@@ -67,6 +71,35 @@ def async_create_clientsession(
connector=connector, headers={USER_AGENT: SERVER_SOFTWARE}, **kwargs,
)
async def patched_close() -> None:
"""Mock close to avoid integrations closing our session."""
try:
found_frame, integration, path = get_integration_frame()
except MissingIntegrationFrame:
# Did not source from an integration? Hard error.
raise RuntimeError(
"Detected closing of the Home Assistant aiohttp session in the Home Assistant core. "
"Please report this issue."
)
index = found_frame.filename.index(path)
if path == "custom_components/":
extra = " to the custom component author"
else:
extra = ""
_LOGGER.warning(
"Detected integration that closes the Home Assistant aiohttp session. "
"Please report issue%s for %s using this method at %s, line %s: %s",
extra,
integration,
found_frame.filename[index:],
found_frame.lineno,
found_frame.line.strip(),
)
clientsession.close = patched_close # type: ignore
if auto_cleanup:
_async_register_clientsession_shutdown(hass, clientsession)