Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate aiohttp session on connectivity check #5332

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading