Skip to content

Commit

Permalink
fix: cancel reconnect timer on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed Nov 6, 2024
1 parent 8633981 commit acb01c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/pysaleryd/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ async def __aenter__(self):
return self

async def __aexit__(self, _type, value, traceback):
self.disconnect()
try:
pass
finally:
self.disconnect()

async def _call_handlers_task(self):
"""Call handlers with data"""
Expand Down Expand Up @@ -98,8 +101,13 @@ async def check_connection():
while self._socket.state != State.RUNNING:
await asyncio.sleep(0.2)

self._socket.start()
await asyncio.gather(check_connection())
try:
self._socket.start()
await asyncio.gather(check_connection())
except asyncio.CancelledError:
_LOGGER.debug("Connect was cancelled")
self.disconnect()
raise

def disconnect(self):
"""Disconnect from system"""
Expand Down
16 changes: 11 additions & 5 deletions src/pysaleryd/websocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Websocket client to listen and send messages to and from HRV system."""

import asyncio
import enum
import logging
Expand All @@ -25,7 +26,7 @@ class State(enum.Enum):
STOPPED = "stopped"


RETRY_TIMER: Final = 15
RETRY_INTERVAL: Final = 15
RECEIVE_TIMEOUT: Final = 5
TIMEOUT: Final = 5

Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(

self._loop = asyncio.get_running_loop()
self._task = None
self._retry_timer = None
self._ws = None
self._state = self._previous_state = State.NONE

Expand Down Expand Up @@ -85,10 +87,10 @@ def _retry(self) -> None:
"Reconnecting to websocket failed (%s:%s) scheduling retry at an interval of %i seconds", # noqa: E501
self._host,
self._port,
RETRY_TIMER,
RETRY_INTERVAL,
)
self._state_changed()
self._loop.call_later(RETRY_TIMER, self.start)
self._retry_timer = self._loop.call_later(RETRY_INTERVAL, self.start)
else:
self._set_state(State.RETRYING)
_LOGGER.info(
Expand Down Expand Up @@ -172,10 +174,14 @@ def stop(self) -> None:
_LOGGER.info(
"Shutting down connection to websocket (%s:%s)", self._host, self._port
)
self._set_state(State.STOPPED)
self._state_changed()

if self._task:
self._task.cancel()
if self._retry_timer:
self._retry_timer.cancel()

self._set_state(State.STOPPED)
self._state_changed()

async def send_message(self, message: str):
"""Send message to system
Expand Down

0 comments on commit acb01c3

Please sign in to comment.