Skip to content

Commit f17c2c8

Browse files
committed
chore: avoid infinite recursion
1 parent 9e8de5d commit f17c2c8

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

hivemind_bus_client/client.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,23 @@ def on_close(self, *args):
233233
self.crypto_key = None
234234
super().on_close(*args)
235235

236-
def wait_for_handshake(self, timeout=5):
236+
def wait_for_handshake(self, timeout=5, max_retries=15):
237237
"""
238238
Waits for the HiveMind handshake to complete; if the handshake is not set and the websocket connection is open, starts the handshake, otherwise waits for the websocket to open and retries.
239-
239+
240240
Parameters:
241241
timeout (float): Number of seconds to wait for each handshake or connection attempt before retrying.
242242
"""
243243
self.handshake_event.wait(timeout=timeout)
244244
if not self.handshake_event.is_set():
245+
if max_retries <= 0:
246+
raise RuntimeError("timed out waiting for handshake")
245247
if self.connected_event.is_set():
246248
self.protocol.start_handshake()
247249
else:
248250
LOG.warning("Can't start handshake because websocket connection is not yet open...")
249251
self.connected_event.wait(timeout=timeout)
250-
self.wait_for_handshake()
252+
self.wait_for_handshake(timeout, max_retries - 1)
251253

252254
@staticmethod
253255
def build_url(key, host='127.0.0.1', port=5678,
@@ -346,19 +348,18 @@ def _handle_hive_protocol(self, message: HiveMessage):
346348
def emit(self, message: Union[MycroftMessage, HiveMessage],
347349
binary_type: HiveMindBinaryPayloadType = HiveMindBinaryPayloadType.UNDEFINED):
348350
"""
349-
Send a HiveMind message (or wrap and send a MycroftMessage) and optionally transmit it as a binary payload.
350-
351+
Send a HiveMessage or MycroftMessage to the HiveMind network, injecting routing context for BUS messages and optionally sending binary payloads.
352+
351353
Parameters:
352-
message (MycroftMessage | HiveMessage): The outgoing message. A MycroftMessage will be wrapped into a BUS HiveMessage.
353-
binary_type (HiveMindBinaryPayloadType): Binary payload subtype to use when sending BINARY messages; defaults to UNDEFINED.
354-
355-
Behavior:
356-
- For BUS messages, ensures payload.context contains routing fields (source, platform, destination, session_id, site_id) and emits the payload to the client's internal bus before sending.
357-
- Chooses binary or text transport according to message type and client configuration; the payload may be compressed and/or encrypted based on client settings.
358-
354+
message (MycroftMessage | HiveMessage): The message to send. If a MycroftMessage is provided it will be wrapped into a BUS HiveMessage.
355+
binary_type (HiveMindBinaryPayloadType): When sending binary payloads, indicates the binary payload subtype; defaults to UNDEFINED.
356+
357+
Notes:
358+
- For messages with msg_type == HiveMessageType.BUS, the function will ensure the payload.context contains routing fields (source, platform, destination, session) and will emit the payload to the client's internal bus before sending.
359+
- This method transmits the message over the client's WebSocket and may perform serialization, optional compression, and optional encryption depending on client configuration.
360+
359361
Raises:
360-
ValueError: If the client has not been started (run_forever() not executed) and therefore cannot send messages.
361-
RuntimeError: If the client was started but the websocket connection did not open within the wait period, preventing the message from being sent.
362+
ValueError: If the client has not been started with run_forever() and the connection is not ready.
362363
"""
363364
if isinstance(message, MycroftMessage):
364365
message = HiveMessage(msg_type=HiveMessageType.BUS,

0 commit comments

Comments
 (0)