ROS2-3002 ros2 runtime_error ai_generated true

rclpy.service.ServiceException:等待 10.0 秒后服务 '/my_service' 仍不可用

rclpy.service.ServiceException: Service '/my_service' is not available after waiting 10.0 seconds

ID: ros2/ros2-service-client-wait-timeout

其他格式: JSON · Markdown 中文 · English
95%修复率
88%置信度
1证据数
2023-09-20首次发现

版本兼容性

版本状态引入弃用备注
ros2:humble active
ros2:iron active
rclpy:4.0.0 active
rclcpp:18.0.0 active

根因分析

服务客户端等待服务变为可用时超时,通常因为服务服务器未运行、未启动或网络/域 ID 阻止了发现。

English

The service client timed out waiting for the service to become available, typically because the service server is not running, not started, or the network/domain ID prevents discovery.

generic

官方文档

https://docs.ros2.org/latest/api/rclpy/api/services.html

解决方案

  1. 验证服务服务器是否运行并可访问。使用:ros2 service list | grep /my_service
    
    如果未列出,启动服务器节点:ros2 run my_package my_service_server_node
    
    然后重试客户端。
  2. 在客户端代码中添加带指数退避的重试循环,而非单一等待。Python 示例:
    import rclpy
    from rclpy.node import Node
    from std_srvs.srv import Empty
    
    class MyClient(Node):
        def __init__(self):
            super().__init__('my_client')
            self.cli = self.create_client(Empty, '/my_service')
            while not self.cli.wait_for_service(timeout_sec=1.0):
                self.get_logger().info('服务不可用,正在重试...')
            # 继续服务调用

无效尝试

常见但无效的做法:

  1. Increasing the timeout value to 30 or 60 seconds 95% 失败

    The service server is not running at all; a longer timeout just delays the failure.

  2. Restarting the client node without checking the service server status 90% 失败

    Restarting the client does not start the service server; the root cause is the missing server.