async def send_to_ipc_server(socket_file: Path, cmd, **kwargs): try: socket_port = int(socket_file.read_text().strip()) except (ValueError, OSError) as exc: raise IPCServerNotRunning("Invalid IPC socket file") from exc try: stream = await trio.open_tcp_stream("127.0.0.1", socket_port) raw_req = cmd_req_serializer.dumps({"cmd": cmd, **kwargs}) await stream.send_all(raw_req) unpacker = Unpacker(exc_cls=IPCServerError) while True: raw = await stream.receive_some(1000) if not raw: raise IPCServerError( f"IPC server has closed the connection unexpectly") unpacker.feed(raw) raw_rep = next(unpacker, None) rep = cmd_rep_serializer.load(raw_rep) if rep: if rep["status"] != "ok": raise IPCServerBadResponse(rep) return rep except SerdeError as exc: raise IPCServerError(f"Invalid message format: {exc}") from exc except (OSError, trio.BrokenResourceError) as exc: raise IPCServerNotRunning( f"Impossible to connect to IPC server: {exc}") from exc
async def _client_handler(stream): # General exception handling try: # Stream handling try: unpacker = Unpacker() async for raw in stream: unpacker.feed(raw) for cmd in unpacker: cmd = cmd_req_serializer.load(cmd) rep = await cmd_handler(cmd) raw_rep = cmd_rep_serializer.dumps(rep) logger.info("Command processed", cmd=cmd["cmd"], rep_status=rep["status"]) await stream.send_all(raw_rep) except SerdeError as exc: await stream.send_all( packb({ "status": "invalid_format", "reason": str(exc) })) finally: await stream.aclose() except trio.BrokenResourceError: pass # Peer has closed the connection while we were sending a response except Exception: logger.exception("Unexpected crash")