From b83a362abe7c7247a1be2a4b3135b0f219237d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Otgaar?= Date: Wed, 29 Oct 2025 13:31:24 +0100 Subject: [PATCH] fix: wait for req socket send to make sure we dont stay stuck - if there's no REP this would be awaited forever. ref: N25B-205 --- .../agents/ri_communication_agent.py | 29 ++++++++++++++++--- .../api/v1/endpoints/command.py | 1 - 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/control_backend/agents/ri_communication_agent.py b/src/control_backend/agents/ri_communication_agent.py index 504c707..2b92989 100644 --- a/src/control_backend/agents/ri_communication_agent.py +++ b/src/control_backend/agents/ri_communication_agent.py @@ -17,6 +17,7 @@ class RICommunicationAgent(Agent): req_socket: zmq.Socket _address = "" _bind = True + connected = False def __init__( self, @@ -40,17 +41,34 @@ class RICommunicationAgent(Agent): # We need to listen and sent pings. message = {"endpoint": "ping", "data": {"id": "e.g. some reference id"}} - await self.agent.req_socket.send_json(message) + seconds_to_wait_total = 4.0 + try: + await asyncio.wait_for( + self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2 + ) + except TimeoutError as e: + logger.debug( + f"Waited too long to send message - we probably dont have any receivers... but let's check!" + ) # Wait up to three seconds for a reply:) try: - message = await asyncio.wait_for(self.agent.req_socket.recv_json(), timeout=3.0) + logger.debug(f"waiting for message for {seconds_to_wait_total / 2} seconds.") + message = await asyncio.wait_for( + self.agent.req_socket.recv_json(), timeout=seconds_to_wait_total / 2 + ) # We didnt get a reply :( - except asyncio.TimeoutError as e: - logger.info("No ping retrieved in 3 seconds, killing myself.") + except TimeoutError as e: + logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself.") + self.agent.connected = False + # TODO: Send event to UI letting know that we've lost connection + self.kill() + except Exception as e: + logger.debug(f"Differennt exception: {e}") + logger.debug('Received message "%s"', message) if "endpoint" not in message: logger.error("No received endpoint in message, excepted ping endpoint.") @@ -162,4 +180,7 @@ class RICommunicationAgent(Agent): # Set up ping behaviour listen_behaviour = self.ListenBehaviour() self.add_behaviour(listen_behaviour) + + # TODO: Let UI know that we're connected >:) + self.connected = True logger.info("Finished setting up %s", self.jid) diff --git a/src/control_backend/api/v1/endpoints/command.py b/src/control_backend/api/v1/endpoints/command.py index badaf90..e7fef60 100644 --- a/src/control_backend/api/v1/endpoints/command.py +++ b/src/control_backend/api/v1/endpoints/command.py @@ -17,6 +17,5 @@ async def receive_command(command: SpeechCommand, request: Request): topic = b"command" pub_socket: Socket = request.app.state.internal_comm_socket pub_socket.send_multipart([topic, command.model_dump_json().encode()]) - return {"status": "Command received"}