# 运行时错误：无法取消正在取消中的目标

- **ID:** `ros2/nav2-cancel-goal-multiple-times`
- **领域:** ros2
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Nav2动作服务器不支持对同一目标并发取消请求；多次调用取消会触发动作服务器内部状态机的竞态条件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ros2-humble | active | — | — |
| ros2-iron | active | — | — |
| ros2-rolling | active | — | — |

## 解决方案

1. ```
   在取消前检查目标是否处于终端状态：if (goal_handle->get_status() == action_msgs::msg::GoalStatus::STATUS_ACCEPTED || goal_handle->get_status() == action_msgs::msg::GoalStatus::STATUS_EXECUTING) { goal_handle->cancel(); }
   ```
2. ```
   使用互斥锁序列化取消调用：std::lock_guard<std::mutex> lock(cancel_mutex_); if (!cancel_in_progress_) { cancel_in_progress_ = true; goal_handle->cancel(); cancel_in_progress_ = false; }
   ```

## 无效尝试

- **** — Race still occurs if two cancel calls happen within the sleep window; not a deterministic fix. (70% 失败率)
- **** — Error is suppressed but the action server state may remain inconsistent, leading to future crashes. (60% 失败率)
