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

- **ID:** `ros2/ros2-service-client-wait-timeout`
- **领域:** ros2
- **类别:** runtime_error
- **错误码:** `ROS2-3002`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ros2:humble | active | — | — |
| ros2:iron | active | — | — |
| rclpy:4.0.0 | active | — | — |
| rclcpp:18.0.0 | active | — | — |

## 解决方案

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

## 无效尝试

- **Increasing the timeout value to 30 or 60 seconds** — The service server is not running at all; a longer timeout just delays the failure. (95% 失败率)
- **Restarting the client node without checking the service server status** — Restarting the client does not start the service server; the root cause is the missing server. (90% 失败率)
