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
This commit is contained in:
Björn Otgaar
2025-10-29 13:31:24 +01:00
parent 158911b134
commit b83a362abe
2 changed files with 25 additions and 5 deletions

View File

@@ -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)

View File

@@ -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"}