Skip to content

Commit

Permalink
Merge pull request #9 from bj00rn/bugfix/cancel-retry-timer-on-discon…
Browse files Browse the repository at this point in the history
…nect

Bugfix/cancel retry timer on disconnect
  • Loading branch information
bj00rn authored Nov 6, 2024
2 parents 4d39e88 + 3b2b650 commit d010fd7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ jobs:
matrix:
python:
- "3.10" # oldest Python supported by PSF
- "3.11" # newest Python that is stable
- "3.11"
- "3.12" # newest Python that is stable
platform:
- ubuntu-latest
runs-on: ${{ matrix.platform }}
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"-o",
"log_cli=1",
"--timeout=30",
"--verbose"
"--verbose",
"--import-mode=prepend",
],
}
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 d010fd7

Please sign in to comment.