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 9, 2024
1 parent 042107a commit c694b7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
18 changes: 13 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,16 @@ def run_in_executor(

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

def create_websession(self) -> None:
"""Create a new aiohttp session"""

Check failure on line 550 in supervisor/coresys.py

View workflow job for this annotation

GitHub Actions / Check ruff

Ruff (D400)

supervisor/coresys.py:550:9: D400 First line should end with a period

Check failure on line 550 in supervisor/coresys.py

View workflow job for this annotation

GitHub Actions / Check ruff

Ruff (D415)

supervisor/coresys.py:550:9: D415 First line should end with a period, question mark, or exclamation point
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
4 changes: 4 additions & 0 deletions supervisor/host/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,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 c694b7f

Please sign in to comment.