def METHOD_NAME(event, recv, error, cmd_queue):
    while True:
        request = recv()
        if not request:
            break
        try:
            kind = request["kind"]
            if kind == "signal":
                # Cancel signals are handled here, the rest forwarded
                if request["status"] == "cancel":
                    event.set()
                else:
                    # Ignore other signals
                    logger.error("Unhandled signal: %r", request)
            elif kind == "command":
                cmd_queue.join()  # Wait for existing command to complete
                event.clear()  # Reset event for next command
                cmd_queue.put(request)
            else:
                error("invalid-command", "Unsupported request type")
        except KeyError as e:
            error("invalid-command", str(e))
        except RpcException as e:
            error(e.status, e.message, e.body)
        except Exception as e:
            logger.exception("Unhandled exception")
            error("exception", f"{e!r}")
    event.set()
    cmd_queue.put(None)