Skip to content

Commit

Permalink
Recreate aiohttp session on connectivity check
Browse files Browse the repository at this point in the history
It seems to actually get a proper connectivity check run, we need a new
vanilla ClientSession() object.
  • Loading branch information
agners committed Oct 8, 2024
1 parent 38affe8 commit a8d65c6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
17 changes: 12 additions & 5 deletions supervisor/coresys.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self):

# 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()
Expand Down Expand Up @@ -96,10 +96,8 @@ def __init__(self):
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]] = []
Expand Down Expand Up @@ -548,6 +546,15 @@ def run_in_executor(

return self.loop.run_in_executor(None, funct, *args)

def create_websession(self) -> None:

Check failure on line 549 in supervisor/coresys.py

View workflow job for this annotation

GitHub Actions / Check ruff

Ruff (D102)

supervisor/coresys.py:549:9: D102 Missing docstring in public method
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()
Expand Down
7 changes: 4 additions & 3 deletions supervisor/host/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@ async def update(self, *, force_connectivity_check: bool = False):
"No network D-Bus connection available", _LOGGER.error
) from err

# This potentially updated the DNS configuration. Make sure the DNS plug-in
# picks up the latest settings.
await self.sys_plugins.dns.restart()
await self.check_connectivity(force=force_connectivity_check)

async def apply_changes(
Expand Down Expand Up @@ -285,6 +282,10 @@ async def apply_changes(
state = msg[0]
_LOGGER.debug("Active connection state changed to %s", state)

# This potentially updated the DNS configuration. Make sure the
# DNS plug-in picks up the latest settings.
await self.sys_plugins.dns.restart()

# update_only means not done by user so don't force a check afterwards
await self.update(force_connectivity_check=not update_only)

Expand Down
2 changes: 2 additions & 0 deletions supervisor/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ async def check_connectivity(self):
"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

0 comments on commit a8d65c6

Please sign in to comment.