feat: lifecycle contract — cancel/pause must reach true quiescence before reporting done
Root cause (from a C++ host log, 2026-09-11): cancel during PLANNING only set a discard flag on the planner thread, which ran to allocated_time before CANCELLED could be reported; a queued next mission landed on a still-busy planner and timed out too. Same class of bug existed for pause, recovery, and implicitly for a legitimate ABORTED read while a component was still unwinding. Full audit, decisions, and phased design in docs/PLAN_LIFECYCLE_CANCEL_PAUSE_RESUME.md; old-vs-new behaviour per state in docs/LIFECYCLE_BEHAVIOUR_MATRIX.md. Runtime quiescence, not just "command sent": - kPausing (new state) mirrors kCancelling: PAUSED/CANCELLED is only entered once planner and recovery are idle, the action reports terminal, and the robot is stopped per odometry (robot_stopped, stopped_measured_linear/ angular_eps) — not merely "the command we sent was zero". settle_timeout bounds the wait; exceeding it still finishes but logs an ungated error naming which component was still busy, so a plugin that doesn't honour cancel/pause is visible instead of silently swallowed. - PlannerRunner::running_planner_ (new): the instance the planner thread is actually inside makePlan() with, tracked separately from active_ — active_ can already point at a different instance after swapPlanner() while the old attempt is still running. cancelPlan(), swapPlanner() (when a job is running_ or only pending_ and not yet picked up), and the destructor all preempt through this pointer now; the old code preempted through active_ and could silently preempt the wrong (idle) instance after a profile swap. - ComponentStatusBoard + per-component ComponentPhase (idle/busy/stopping/ paused/hung), read by PlannerRunner, RecoveryRunner, ActionRunner and ControlLoop instead of ad hoc bools — this is what makes "did everything actually stop" answerable at all, and what the new Watchdog thread reads to flag a component busy-without-progress as hung (logged once at the edge) without the control loop needing to detect its own stall. - Resume: PLANNING replans from the current pose. CONTROLLING re-issues setPlan() with the plan already in hand (decided: setPlan again, not a fresh plan) — the local planner needs nothing else since it already runs once per control cycle; a fresh plan on resume was considered and rejected as unnecessary work for a plan that's still valid. - Command-to-zero on cancel/pause stays immediate (no deceleration ramp) — existing VelocityArbiter behaviour, left as is per a scoped decision. Depends on: Test/sbpl (bd89924), pnkx_nav_core (2b0993b, robot_nav_core:: Preemptable + component_status_probe.h), Test/sbpl_lattice_planner (7456e99), Test/recovery_core (254c7fc, onCancelRequested + ABI guard), Test/ sbpl_recovery (ea79686, worker-thread cancel), Test/action_core (669c3f7, pause/resume + kCancelled), Test/dock_pattern_action (cc1ea74). Verified: 13 gtest binaries / 528 tests, 0 failures. Runtime on the dev machine (Gazebo + real ROS host + VDA5050 over MQTT), 6 scenarios — cancel during PLANNING, pause/resume during PLANNING and CONTROLLING (incl. submit-while-still-winding-down), pause-then-cancel during PLANNING and CONTROLLING: no ABORTED/RECOVERING in any run, preempt lands within hundredths of a millisecond of entering CANCELLING/PAUSING, PAUSED/CANCELLED only reported once the robot is actually stopped per odometry. Full numbers in the plan doc section 6. Excluded from this commit (pre-existing, unrelated): config/runtime/recovery_behaviors_params.yaml (allocated_time 1.0->2.0, SBPLDetourPlanner tuning predating this work). Not yet done (see plan doc "Trạng thái"): config tuning (phase 0, pending per a scoping decision); runtime scenarios for cancel mid-detour and a 30 s pause mid-action; commit to rb_ws is a separate step, not done here. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -90,6 +90,7 @@ PlannerRunner::~PlannerRunner()
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
shutdown_ = true;
|
||||
discard_ = true;
|
||||
preemptRunningLocked(); // shutdown không phải chờ hết allocated_time của lượt dở
|
||||
}
|
||||
cv_.notify_all();
|
||||
|
||||
@@ -102,6 +103,11 @@ PlannerRunner::~PlannerRunner()
|
||||
}
|
||||
}
|
||||
|
||||
void PlannerRunner::attachStatusBoard(ComponentStatusBoard* board)
|
||||
{
|
||||
status_board_ = board;
|
||||
}
|
||||
|
||||
void PlannerRunner::attachStats(RuntimeStats* stats)
|
||||
{
|
||||
stats_ = stats;
|
||||
@@ -366,11 +372,25 @@ bool PlannerRunner::swapPlanner(const std::string& planner_name)
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
// Lượt đang chạy thuộc về planner cũ — kết quả của nó không còn nghĩa. Không cắt ngang được
|
||||
// phép tính, chỉ đánh dấu vứt kết quả. Con trỏ planner cũ vẫn hợp lệ vì cache không xoá entry.
|
||||
if (running_ || pending_)
|
||||
// Lượt đang chạy thuộc về planner cũ — kết quả của nó không còn nghĩa: đánh dấu vứt, và cắt
|
||||
// nếu plugin Preemptable. Con trỏ planner cũ vẫn hợp lệ vì cache không xoá entry.
|
||||
if (pending_ && !running_)
|
||||
{
|
||||
// Thread chưa nhận: rút yêu cầu ra, kẻo nó chạy yêu cầu của planner cũ trên instance mới rồi
|
||||
// vứt — tốn một lượt makePlan vô ích.
|
||||
pending_ = false;
|
||||
request_order_.reset();
|
||||
}
|
||||
else if (running_)
|
||||
{
|
||||
discard_ = true;
|
||||
// Lượt cũ vẫn chiếm thread; cắt nó ngay để yêu cầu kế (dùng planner mới) không phải chờ
|
||||
// planner_patience. Phải cắt TRƯỚC khi đổi active_ — sau dòng dưới active_ đã là instance mới.
|
||||
preemptRunningLocked();
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kStopping);
|
||||
}
|
||||
}
|
||||
active_ = planner;
|
||||
}
|
||||
@@ -463,12 +483,49 @@ bool PlannerRunner::pollPlan(PlanResult& result)
|
||||
return true;
|
||||
}
|
||||
|
||||
void PlannerRunner::preemptRunningLocked()
|
||||
{
|
||||
// Gọi dưới mutex_. `running_planner_` là instance thread đang ở trong makePlan — KHÔNG phải
|
||||
// `active_`, vì swapPlanner có thể đã trỏ active_ sang instance khác trong lúc lượt cũ chưa về.
|
||||
// Instance cũ được cache/retired giữ sống nên con trỏ còn hợp lệ suốt lượt; preempt() là một
|
||||
// atomic store, gọi được từ control thread. Plugin không Preemptable: không có gì để làm.
|
||||
if (running_planner_ == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (auto* preemptable = dynamic_cast<robot_nav_core::Preemptable*>(running_planner_))
|
||||
{
|
||||
preemptable->preempt();
|
||||
}
|
||||
}
|
||||
|
||||
void PlannerRunner::cancelPlan()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (pending_ || running_)
|
||||
if (pending_ && !running_)
|
||||
{
|
||||
// Lượt đã được xếp nhưng thread chưa kịp nhận (cửa sổ giữa startPlan() và lúc thread thức dậy).
|
||||
// Rút nó ra khỏi hàng đợi là đủ — không có gì để preempt, và nếu chỉ đặt discard_ thì thread
|
||||
// sẽ nhận job này NGAY SAU cancel, chạy trọn allocated_time cho một plan không ai cần (bắt được
|
||||
// bằng test CancelPreemptsAPreemptablePlannerWithinMilliseconds, 2026-09-11).
|
||||
pending_ = false;
|
||||
request_order_.reset();
|
||||
}
|
||||
else if (pending_ || running_)
|
||||
{
|
||||
discard_ = true;
|
||||
if (running_)
|
||||
{
|
||||
// Cắt thật (2026-09-11): plugin implement robot_nav_core::Preemptable thì lượt makePlan đang
|
||||
// chạy thoát trong vài ms thay vì đi tới hết allocated_time. Plugin cũ (không Preemptable)
|
||||
// thì chỉ còn discard_ như trước — kết quả về bị vứt, thread bận tới khi nó xong.
|
||||
preemptRunningLocked();
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
// Thread vẫn ở trong makePlan; từ giờ tới lúc nó trả về là "đang dừng", không phải "bận".
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kStopping);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Kết quả đã nằm sẵn trong hộp thư cũng bỏ luôn: bên gọi vừa nói nó không còn cần plan này.
|
||||
has_result_ = false;
|
||||
@@ -501,6 +558,7 @@ void PlannerRunner::threadBody()
|
||||
// Chụp lại yêu cầu rồi NHẢ MUTEX trước khi gọi plugin. Đây là điểm mấu chốt của cả mô hình:
|
||||
// control thread không bao giờ phải chờ một lượt lập plan.
|
||||
robot_nav_core::BaseGlobalPlanner* planner = active_;
|
||||
running_planner_ = planner;
|
||||
const robot_geometry_msgs::PoseStamped start = request_start_;
|
||||
const robot_geometry_msgs::PoseStamped goal = request_goal_;
|
||||
const std::shared_ptr<robot_protocol_msgs::Order> order = request_order_;
|
||||
@@ -508,6 +566,10 @@ void PlannerRunner::threadBody()
|
||||
|
||||
pending_ = false;
|
||||
running_ = true;
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kBusy);
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
|
||||
@@ -558,6 +620,11 @@ void PlannerRunner::threadBody()
|
||||
|
||||
lock.lock();
|
||||
running_ = false;
|
||||
running_planner_ = nullptr;
|
||||
if (status_board_ != nullptr)
|
||||
{
|
||||
status_board_->enter(Component::kGlobalPlanner, ComponentPhase::kIdle);
|
||||
}
|
||||
|
||||
if (discard_)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user