From 3b27a6a6f3e0d9b6f6a45a2f188034cc8a076150 Mon Sep 17 00:00:00 2001 From: Benjamin Thomas Schwertfeger Date: Mon, 25 Nov 2024 19:32:05 +0100 Subject: [PATCH] force cancelling the task --- kraken/futures/websocket/__init__.py | 8 +++++++- kraken/spot/websocket/connectors.py | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kraken/futures/websocket/__init__.py b/kraken/futures/websocket/__init__.py index 3e1ace2..06d4eb6 100644 --- a/kraken/futures/websocket/__init__.py +++ b/kraken/futures/websocket/__init__.py @@ -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, @@ -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!") diff --git a/kraken/spot/websocket/connectors.py b/kraken/spot/websocket/connectors.py index 02c92f7..0415b85 100644 --- a/kraken/spot/websocket/connectors.py +++ b/kraken/spot/websocket/connectors.py @@ -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