Recreate aiohttp session on connectivity check (#5332)

It seems to actually get a proper connectivity check run, we need a new
vanilla ClientSession() object.
This commit is contained in:
Stefan Agner 2024-10-09 21:41:46 +02:00 committed by GitHub
parent 9f3767b23d
commit 1504278223
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View File

@ -63,7 +63,7 @@ class CoreSys:
# External objects
self._loop: asyncio.BaseEventLoop = asyncio.get_running_loop()
self._websession: aiohttp.ClientSession = aiohttp.ClientSession()
self._websession = None
# Global objects
self._config: CoreConfig = CoreConfig()
@ -96,10 +96,8 @@ class CoreSys:
self._bus: Bus | None = None
self._mounts: MountManager | None = None
# Set default header for aiohttp
self._websession._default_headers = MappingProxyType(
{aiohttp.hdrs.USER_AGENT: SERVER_SOFTWARE}
)
# Setup aiohttp session
self.create_websession()
# Task factory attributes
self._set_task_context: list[Callable[[Context], Context]] = []
@ -548,6 +546,16 @@ class CoreSys:
return self.loop.run_in_executor(None, funct, *args)
def create_websession(self) -> None:
"""Create a new aiohttp session."""
if self._websession:
self.create_task(self._websession.close())
# Create session and set default header for aiohttp
self._websession: aiohttp.ClientSession = aiohttp.ClientSession(
headers=MappingProxyType({aiohttp.hdrs.USER_AGENT: SERVER_SOFTWARE})
)
def _create_context(self) -> Context:
"""Create a new context for a task."""
context = copy_context()

View File

@ -274,6 +274,8 @@ class Supervisor(CoreSysAttributes):
"https://checkonline.home-assistant.io/online.txt", timeout=timeout
)
except (ClientError, TimeoutError):
# Need to recreate the websession to avoid stale connection checks
self.coresys.create_websession()
self.connectivity = False
else:
self.connectivity = True