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:
213
src/io/watchdog.cpp
Normal file
213
src/io/watchdog.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* move_base2 — watchdog thành phần treo.
|
||||
*
|
||||
* Author: DuongTD
|
||||
*********************************************************************/
|
||||
#include <move_base2/io/watchdog.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
|
||||
#include <robot/console.h>
|
||||
|
||||
namespace move_base2
|
||||
{
|
||||
|
||||
bool WatchdogConfig::validate(std::string& error) const
|
||||
{
|
||||
const double values[] = {period, planner_timeout, local_planner_call_budget,
|
||||
recovery_stall_budget, action_stall_budget, control_loop_stall_budget};
|
||||
for (double v : values)
|
||||
{
|
||||
if (!std::isfinite(v))
|
||||
{
|
||||
error = "watchdog budgets must be finite [s]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (period < 0.0)
|
||||
{
|
||||
error = "watchdog_period must be >= 0 [s] (0 = watchdog off)";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string WatchdogConfig::describe() const
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "Watchdog:\n";
|
||||
out << " period : " << period << " s" << (period > 0.0 ? "" : " (off)") << '\n';
|
||||
const auto line = [&out](const char* name, double v) {
|
||||
out << " " << name << " : " << v << " s" << (v > 0.0 ? "" : " (not watched)") << '\n';
|
||||
};
|
||||
line("planner_timeout ", planner_timeout);
|
||||
line("local_planner_call_budget", local_planner_call_budget);
|
||||
line("recovery_stall_budget ", recovery_stall_budget);
|
||||
line("action_stall_budget ", action_stall_budget);
|
||||
line("control_loop_stall_budget", control_loop_stall_budget);
|
||||
return out.str();
|
||||
}
|
||||
|
||||
Watchdog::~Watchdog()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
bool Watchdog::start(const ComponentStatusBoard* board, const WatchdogConfig& config,
|
||||
std::string& error)
|
||||
{
|
||||
if (board == nullptr)
|
||||
{
|
||||
error = "watchdog needs a component status board";
|
||||
return false;
|
||||
}
|
||||
if (!config.validate(error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (running_.load())
|
||||
{
|
||||
error = "watchdog already running";
|
||||
return false;
|
||||
}
|
||||
board_ = board;
|
||||
config_ = config;
|
||||
for (auto& flag : hung_)
|
||||
{
|
||||
flag.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
if (config_.period <= 0.0)
|
||||
{
|
||||
return true; // Tắt: không thread, checkOnce() vẫn dùng được nếu ai đó tự gọi.
|
||||
}
|
||||
running_.store(true);
|
||||
thread_ = std::thread(&Watchdog::threadBody, this);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Watchdog::stop()
|
||||
{
|
||||
running_.store(false);
|
||||
if (thread_.joinable())
|
||||
{
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
bool Watchdog::anyHung() const
|
||||
{
|
||||
for (const auto& flag : hung_)
|
||||
{
|
||||
if (flag.load(std::memory_order_relaxed))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double Watchdog::budgetFor(Component c) const
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case Component::kGlobalPlanner:
|
||||
return config_.planner_timeout;
|
||||
case Component::kLocalPlanner:
|
||||
return config_.local_planner_call_budget;
|
||||
case Component::kRecovery:
|
||||
return config_.recovery_stall_budget;
|
||||
case Component::kAction:
|
||||
return config_.action_stall_budget;
|
||||
case Component::kControlLoop:
|
||||
return config_.control_loop_stall_budget;
|
||||
case Component::kCostmapGlobal:
|
||||
case Component::kCostmapLocal:
|
||||
// Chưa có heartbeat từ map-update thread (cần móc vào gói costmap) — không giám sát ở pha này.
|
||||
return 0.0;
|
||||
case Component::kCount:
|
||||
break;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
void Watchdog::checkOnce()
|
||||
{
|
||||
if (board_ == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (std::size_t i = 0; i < ComponentStatusBoard::kRows; ++i)
|
||||
{
|
||||
const Component c = static_cast<Component>(i);
|
||||
const double budget = budgetFor(c);
|
||||
if (budget <= 0.0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const ComponentPhase phase = board_->phase(c);
|
||||
// Hàng heartbeat-only (control loop) không có phase: luôn được coi là "đang chạy". Hàng khác chỉ
|
||||
// xét khi đang có việc — idle/paused không có gì để treo.
|
||||
const bool watched = c == Component::kControlLoop ||
|
||||
phase == ComponentPhase::kBusy || phase == ComponentPhase::kStopping;
|
||||
// Hàng chưa từng nhích (last_progress = 0): runtime mới lên, chưa có gì để so.
|
||||
const bool ever_progressed = board_->at(c).last_progress_ns.load(std::memory_order_relaxed) != 0;
|
||||
const double since = board_->secondsSinceProgress(c);
|
||||
const bool stalled = watched && ever_progressed && since > budget;
|
||||
|
||||
std::atomic<bool>& flag = hung_[i];
|
||||
const bool was_hung = flag.load(std::memory_order_relaxed);
|
||||
if (stalled && !was_hung)
|
||||
{
|
||||
flag.store(true, std::memory_order_relaxed);
|
||||
// LỖI, không có khoá tắt: đây là "một phần của runtime đã chết đứng". In một lần ở sườn.
|
||||
robot::log_error("[move_base2] watchdog: %s is HUNG — phase '%s', no progress for %.2f s "
|
||||
"(budget %.2f s). %s\n",
|
||||
toString(c), toString(phase), since, budget,
|
||||
c == Component::kControlLoop
|
||||
? "The control thread is not cycling: host must treat cmd_vel as stale."
|
||||
: "cancel/pause will hit settle_timeout; the plugin is not returning.");
|
||||
}
|
||||
else if (!stalled && was_hung)
|
||||
{
|
||||
flag.store(false, std::memory_order_relaxed);
|
||||
robot::log_warning("[move_base2] watchdog: %s recovered (phase '%s').\n", toString(c),
|
||||
toString(phase));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Watchdog::report() const
|
||||
{
|
||||
std::ostringstream out;
|
||||
bool any = false;
|
||||
for (std::size_t i = 0; i < ComponentStatusBoard::kRows; ++i)
|
||||
{
|
||||
if (hung_[i].load(std::memory_order_relaxed))
|
||||
{
|
||||
const Component c = static_cast<Component>(i);
|
||||
out << (any ? ", " : "") << toString(c) << " hung for "
|
||||
<< (board_ != nullptr ? board_->secondsSinceProgress(c) : 0.0) << " s";
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
void Watchdog::threadBody()
|
||||
{
|
||||
const auto period = std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
||||
std::chrono::duration<double>(config_.period));
|
||||
while (running_.load(std::memory_order_relaxed))
|
||||
{
|
||||
checkOnce();
|
||||
std::this_thread::sleep_for(period);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace move_base2
|
||||
Reference in New Issue
Block a user