ROS2-3002 ros2 runtime_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
95%Fix Rate
88%Confidence
1Evidence
2023-09-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ros2:humble active
ros2:iron active
rclpy:4.0.0 active
rclcpp:18.0.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success Verify the service server is running and accessible. Use: ros2 service list | grep /my_service If not listed, start the server node: ros2 run my_package my_service_server_node Then retry the client.
    Verify the service server is running and accessible. Use: ros2 service list | grep /my_service
    
    If not listed, start the server node: ros2 run my_package my_service_server_node
    
    Then retry the client.
  2. 90% success Add a retry loop with exponential backoff in the client code instead of a single wait. Example in 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('Service not available, retrying...') # Proceed with service call
    Add a retry loop with exponential backoff in the client code instead of a single wait. Example in 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('Service not available, retrying...')
            # Proceed with service call

中文步骤

  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('服务不可用,正在重试...')
            # 继续服务调用

Dead Ends

Common approaches that don't work:

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

    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% fail

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