fix: Mitigate gateway null responses.
* fix: Handle gateway stream when None is given.
* refactor: Change occurrence of heartbeater task recreation
(This just mitigates an edgecase of heartbeating when the wsclient is None.
* chore: pre-commit ci
@@ -262,6 +262,7 @@ class WebSocketClient:
_receive.cancel()
return
+ if msg is not None: # this can happen
await self._handle_stream(msg)
async def _handle_stream(self, stream: Dict[str, Any]):
@@ -710,6 +711,13 @@ class WebSocketClient:
self._client = None
+ # We need to check about existing heartbeater tasks for edge cases.
+
+ if self._task:
+ self._task.cancel()
+ if self.__heartbeat_event.is_set():
+ self.__heartbeat_event.clear() # Because we're hardresetting the process
+
if not to_resume:
url = self.ws_url if self.ws_url else await self._http.get_gateway()
else:
@@ -721,11 +729,6 @@ class WebSocketClient:
self.__heartbeater.delay = data["d"]["heartbeat_interval"]
- if self._task:
- self._task.cancel()
- if self.__heartbeat_event.is_set():
- self.__heartbeat_event.clear() # Because we're hardresetting the process
-
self._task = create_task(self.run_heartbeat())
if not to_resume:
@@ -800,7 +803,19 @@ class WebSocketClient:
if packet.data is None:
continue # We just loop it over because it could just be processing something.
- return loads(packet.data) if isinstance(packet.data, str) else None
+ try:
+ msg = loads(packet.data)
+ except Exception as e:
+ import traceback
+
+ log.debug(
+ f'Error serialising message: {"".join(traceback.format_exception(type(e), e, e.__traceback__))}.'
+ )
+ # There's an edge case when the packet's None... or some other deserialisation error.
+ # Instead of raising an exception, we just log it to debug, so it doesn't annoy end user's console logs.
+ msg = None
+
+ return msg
async def _send_packet(self, data: Dict[str, Any]) -> None:
"""
