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

Fix: Websocket "close" connection and RPC listen exception bug #139

Merged
merged 4 commits into from
Jan 25, 2025
Merged
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
17 changes: 10 additions & 7 deletions surrealdb/connection_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class WebsocketConnection(Connection):
async def connect(self):
try:
self._ws = await connect(
self._base_url + "/rpc", subprotocols=[Subprotocol("cbor")]
self._base_url + "/rpc",
subprotocols=[Subprotocol("cbor")],
max_size=1048576,
)
self._receiver_task = asyncio.create_task(self.listen_to_ws(self._ws))
self._receiver_task = asyncio.create_task(self._listen_to_ws(self._ws))
except Exception as e:
raise SurrealDbConnectionError("cannot connect db server", e)

Expand Down Expand Up @@ -48,7 +50,7 @@ async def unset(self, key: str):
await self.send(METHOD_UNSET, key)

async def close(self):
if self._receiver_task:
if self._receiver_task and not self._receiver_task.cancelled():
self._receiver_task.cancel()

if self._ws:
Expand Down Expand Up @@ -88,7 +90,7 @@ async def _make_request(self, request_data: RequestData):
finally:
self.remove_response_queue(ResponseType.SEND, request_data.id)

async def listen_to_ws(self, ws):
async def _listen_to_ws(self, ws):
async for message in ws:
try:
response_data = self._decoder(message)
Expand All @@ -106,7 +108,8 @@ async def listen_to_ws(self, ws):
continue
await queue.put(response_data.get("result"))
except asyncio.CancelledError:
self._logger.info("Task cancelled. Stopped listening for RPC responses")
break
except Exception:
break
asyncio.get_event_loop().stop()
except Exception as e:
self._logger.error(e)
continue
Loading