Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The aiohttp and websocket connections do not get closed properly #335

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions kraken/base_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,15 @@ async def __check_response_data( # pylint: disable=invalid-overridden-method

raise Exception(f"{response.status} - {response.text}")

async def async_close(self: SpotAsyncClient) -> None:
async def close(self: SpotAsyncClient) -> None:
"""Closes the aiohttp session"""
await self.__session.close()

async def __aenter__(self: Self) -> Self:
return self

async def __aexit__(self: SpotAsyncClient, *args: object) -> None:
await self.async_close()
await self.close()


class NFTClient(SpotClient):
Expand Down Expand Up @@ -1142,15 +1142,15 @@ async def __check_response_data( # pylint: disable=invalid-overridden-method

raise Exception(f"{response.status} - {response.text}")

async def async_close(self: FuturesAsyncClient) -> None:
async def close(self: FuturesAsyncClient) -> None:
"""Closes the aiohttp session"""
await self.__session.close()

async def __aenter__(self: Self) -> Self:
return self

async def __aexit__(self: FuturesAsyncClient, *args: object) -> None:
return await self.async_close()
return await self.close()


__all__ = [
Expand Down
7 changes: 4 additions & 3 deletions kraken/futures/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ async def start(self: FuturesWSClient) -> None:
else:
raise TimeoutError("Could not connect to the Kraken API!")

async def stop(self: FuturesWSClient) -> None:
"""Method to stop the websocket connection."""
async def close(self: FuturesWSClient) -> None:
"""Method to close the websocket connection."""
if self._conn:
await self._conn.stop()
await super().close()

@property
def key(self: FuturesWSClient) -> str:
Expand Down Expand Up @@ -443,7 +444,7 @@ async def __aexit__(
) -> None:
"""Exit if used as context manager"""
await super().__aexit__()
await self.stop()
await self.close()


__all__ = ["FuturesWSClient"]
7 changes: 4 additions & 3 deletions kraken/spot/websocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ async def start(self: SpotWSClientBase) -> None:
else:
raise TimeoutError("Could not connect to the Kraken API!")

async def stop(self: SpotWSClientBase) -> None:
"""Method to stop the websocket connection."""
async def close(self: SpotWSClientBase) -> None:
"""Method to close the websocket connection."""
if self._pub_conn:
await self._pub_conn.stop()
if self._priv_conn:
await self._priv_conn.stop()
await super().close()

async def on_message(
self: SpotWSClientBase,
Expand Down Expand Up @@ -184,7 +185,7 @@ async def __aexit__(
) -> None:
"""Exit if used as context manager"""
await super().__aexit__()
await self.stop()
await self.close()

async def get_ws_token(self: SpotWSClientBase) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/futures/test_futures_base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def check() -> None:
dict,
)
finally:
await client.async_close()
await client.close()

run(check())

Expand Down
2 changes: 1 addition & 1 deletion tests/futures/test_futures_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def instantiate_client() -> None:
await client.start()
await async_sleep(4)
assert not client.is_auth
await client.stop()
await client.close()
await async_sleep(2)

asyncio.run(instantiate_client())
Expand Down
4 changes: 2 additions & 2 deletions tests/spot/test_spot_base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def check() -> None:
),
)
finally:
await client.async_close()
await client.close()

run(check())

Expand Down Expand Up @@ -225,7 +225,7 @@ async def check() -> None:
)
sleep(2)
finally:
await client.async_close()
await client.close()

run(check())

Expand Down
2 changes: 1 addition & 1 deletion tests/spot/test_spot_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def create_client() -> None:
client = SpotWebsocketClientTestWrapper()
await client.start()
await async_sleep(5)
await client.stop()
await client.close()

asyncio_run(create_client())

Expand Down
Loading