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 f0d32b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 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

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
2 changes: 2 additions & 0 deletions supervisor/supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ async def check_connectivity(self):
"""Check the connection."""
timeout = aiohttp.ClientTimeout(total=10)
try:
# Need to recreate the websession to avoid stale connection checks
self.coresys.create_websession()
await self.sys_websession.head(
"https://checkonline.home-assistant.io/online.txt", timeout=timeout
)
Expand Down

0 comments on commit f0d32b0

Please sign in to comment.