Skip to content

Commit

Permalink
fix: adjust timeout calls to use async context manager to fix compat …
Browse files Browse the repository at this point in the history
…with async_timeout 5+ (#55)
  • Loading branch information
bdraco authored Jan 24, 2025
1 parent 93852dd commit dc96aee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ urls."Bug Tracker" = "https://github.com/harmony-libs/aioharmony/issues"
urls.Changelog = "https://github.com/harmony-libs/aioharmony/blob/main/CHANGELOG.md"
urls.repository = "https://github.com/harmony-libs/aioharmony"
dependencies = [
"aiohttp>=3.10",
"aiohttp>=3.11",
"async-timeout>=4",
"slixmpp>=1.8",
]
Expand Down
21 changes: 14 additions & 7 deletions src/aioharmony/hubconnector_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from uuid import uuid4

import aiohttp
from aiohttp.client_ws import ClientWSTimeout
from async_timeout import timeout

from aioharmony.const import DEFAULT_WS_HUB_PORT as DEFAULT_HUB_PORT
Expand All @@ -28,6 +29,8 @@
# TODO: Add docstyle comments
# TODO: Clean up code styling

_WS_TIMEOUT = ClientWSTimeout(ws_receive=None, ws_close=DEFAULT_TIMEOUT)


# pylint: disable=too-many-instance-attributes
class HubConnector:
Expand Down Expand Up @@ -115,9 +118,10 @@ async def async_close_session(self) -> None:
""" "Close the aiohttp session."""
if self._aiohttp_session is None:
return
with suppress(asyncio.TimeoutError), timeout(DEFAULT_TIMEOUT):
await self._aiohttp_session.close()
self._aiohttp_session = None
with suppress(asyncio.TimeoutError):
async with timeout(DEFAULT_TIMEOUT):
await self._aiohttp_session.close()
self._aiohttp_session = None

async def hub_connect(self, is_reconnect: bool = False) -> bool:
"""Connect to Hub Web Socket"""
Expand Down Expand Up @@ -150,6 +154,7 @@ async def hub_connect(self, is_reconnect: bool = False) -> bool:
try:
self._websocket = await self._session.ws_connect(
f"ws://{self._ip_address}:{DEFAULT_HUB_PORT}/?domain={self._domain}&hubId={self._remote_id}",
timeout=_WS_TIMEOUT, # close timeout
heartbeat=10,
)
except (
Expand Down Expand Up @@ -212,8 +217,9 @@ async def hub_disconnect(self) -> None:
self._connected = False

if self._websocket:
with suppress(asyncio.TimeoutError), timeout(DEFAULT_TIMEOUT):
await self._websocket.close()
with suppress(asyncio.TimeoutError):
async with timeout(DEFAULT_TIMEOUT):
await self._websocket.close()

await self._session.close()
# Zero-sleep to allow underlying connections to close.
Expand Down Expand Up @@ -257,8 +263,9 @@ async def _reconnect(self) -> None:
_LOGGER.debug(
"%s: Web Socket half-closed, closing first", self._ip_address
)
with suppress(asyncio.TimeoutError), timeout(DEFAULT_TIMEOUT):
await self._websocket.close()
with suppress(asyncio.TimeoutError):
async with timeout(DEFAULT_TIMEOUT):
await self._websocket.close()

if self._aiohttp_session is not None and not self._aiohttp_session.closed:
_LOGGER.debug("%s: Closing sessions", self._ip_address)
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc96aee

Please sign in to comment.