# rclcpp: 检测到死锁：回调在同一个互斥回调组中等待另一个回调

- **ID:** `ros2/rclcpp-dual-mode-callback-group-deadlock`
- **领域:** ros2
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

互斥回调组中的某个回调阻塞在需要同一组中另一个回调的服务或动作调用上，导致死锁，因为该组一次只允许一个回调执行。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble (rclcpp 16.0.0) | active | — | — |
| ROS2 Iron (rclcpp 17.0.0) | active | — | — |
| ROS2 Rolling (rclcpp 18.0.0) | active | — | — |

## 解决方案

1. ```
   将阻塞的服务调用移动到单独的回调组（例如ReentrantCallbackGroup），使其不会阻塞原始组：auto callback_group = this->create_callback_group(rclcpp::CallbackGroupType::Reentrant);
   ```
2. ```
   使用异步服务客户端以避免完全阻塞回调：auto future = client->async_send_request(request);
   ```

## 无效尝试

- **Increase the callback group's thread count or use a ReentrantCallbackGroup** — Reentrant allows concurrent execution but does not solve the logical dependency; the service call still blocks. (60% 失败率)
- **Add a sleep or yield in the callback** — Sleeping does not release the callback group lock; the deadlock persists. (95% 失败率)
