Skip to content

Commit

Permalink
force cancelling the task
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger committed Nov 25, 2024
1 parent ac3f7c2 commit 3b27a6a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 7 additions & 1 deletion kraken/futures/websocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ async def stop(self: ConnectFuturesWebsocket) -> None:
"""Stops the websocket connection"""
self.keep_alive = False
if hasattr(self, "task") and not self.task.done():
await self.task
await self.socket.close()
try:
self.task.cancel()
except asyncio.CancelledError:
# Task was successfully cancelled
pass

async def __run( # noqa: C901
self: ConnectFuturesWebsocket,
Expand All @@ -97,6 +102,7 @@ async def __run( # noqa: C901

async with websockets.connect( # pylint: disable=no-member # noqa: PLR1702
f"wss://{self.__ws_endpoint}",
dditional_headers={"User-Agent": "btschwertfeger/python-kraken-sdk"},
ping_interval=30,
) as socket:
LOG.info("Websocket connected!")
Expand Down
1 change: 0 additions & 1 deletion kraken/spot/websocket/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ async def stop(self: ConnectSpotWebsocketBase) -> None:
await self.socket.close()
try:
self.task.cancel()
await self.task # Await to handle any cancellation cleanup
except asyncio.CancelledError:
# Task was successfully cancelled
pass
Expand Down

0 comments on commit 3b27a6a

Please sign in to comment.