Skip to content

Commit 7badb08

Browse files
authored
Fix: Websocket "close" connection and RPC listen exception bug (#139)
1 parent f120ce9 commit 7badb08

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

surrealdb/connection_ws.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ class WebsocketConnection(Connection):
1616
async def connect(self):
1717
try:
1818
self._ws = await connect(
19-
self._base_url + "/rpc", subprotocols=[Subprotocol("cbor")]
19+
self._base_url + "/rpc",
20+
subprotocols=[Subprotocol("cbor")],
21+
max_size=1048576,
2022
)
21-
self._receiver_task = asyncio.create_task(self.listen_to_ws(self._ws))
23+
self._receiver_task = asyncio.create_task(self._listen_to_ws(self._ws))
2224
except Exception as e:
2325
raise SurrealDbConnectionError("cannot connect db server", e)
2426

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

5052
async def close(self):
51-
if self._receiver_task:
53+
if self._receiver_task and not self._receiver_task.cancelled():
5254
self._receiver_task.cancel()
5355

5456
if self._ws:
@@ -88,7 +90,7 @@ async def _make_request(self, request_data: RequestData):
8890
finally:
8991
self.remove_response_queue(ResponseType.SEND, request_data.id)
9092

91-
async def listen_to_ws(self, ws):
93+
async def _listen_to_ws(self, ws):
9294
async for message in ws:
9395
try:
9496
response_data = self._decoder(message)
@@ -106,7 +108,8 @@ async def listen_to_ws(self, ws):
106108
continue
107109
await queue.put(response_data.get("result"))
108110
except asyncio.CancelledError:
111+
self._logger.info("Task cancelled. Stopped listening for RPC responses")
109112
break
110-
except Exception:
111-
break
112-
asyncio.get_event_loop().stop()
113+
except Exception as e:
114+
self._logger.error(e)
115+
continue

0 commit comments

Comments
 (0)