Skip to content

Commit

Permalink
Recreate aiohttp session on connectivity check (#5332)
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 authored Oct 9, 2024
1 parent 9f3767b commit 1504278
Show file tree
Hide file tree
Showing 2 changed files with 15 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."""
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 @@ -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 1504278

Please sign in to comment.