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:
@@ -147,6 +147,10 @@ public:
|
||||
deps_.recovery = &recovery_;
|
||||
deps_.mission = &mission_;
|
||||
deps_.action = &action_;
|
||||
deps_.components = &components_;
|
||||
planner_.attachStatusBoard(&components_);
|
||||
recovery_.attachStatusBoard(&components_);
|
||||
action_.attachStatusBoard(&components_);
|
||||
}
|
||||
|
||||
void configure(const ControlLoopConfig& config = baseConfig())
|
||||
@@ -199,6 +203,7 @@ public:
|
||||
FakeRecoveryPort recovery_;
|
||||
FakeMissionPort mission_;
|
||||
FakeActionPort action_;
|
||||
move_base2::ComponentStatusBoard components_;
|
||||
ControlLoopDeps deps_;
|
||||
|
||||
robot_costmap_2d::LayeredCostmap global_;
|
||||
@@ -842,7 +847,11 @@ TEST(NavigationServerLifecycle, PauseTakesEffectOnTheNextCycleNotImmediately)
|
||||
<< "pause() goes straight into the core from the host thread";
|
||||
|
||||
fixture.spin(1);
|
||||
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kPaused);
|
||||
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kPausing)
|
||||
<< "pause is taken in one cycle: command zero, waiting for the runtime to settle";
|
||||
fixture.spin(1);
|
||||
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kPaused)
|
||||
<< "nothing is busy in this fixture, so PAUSED follows one cycle later";
|
||||
}
|
||||
|
||||
TEST(NavigationServerLifecycle, ResumeAfterPauseReturnsToControlling)
|
||||
@@ -856,7 +865,7 @@ TEST(NavigationServerLifecycle, ResumeAfterPauseReturnsToControlling)
|
||||
fixture.spin(2);
|
||||
|
||||
fixture.server_.pause();
|
||||
fixture.spin(1);
|
||||
fixture.spin(2);
|
||||
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kPaused);
|
||||
|
||||
fixture.server_.resume();
|
||||
@@ -901,6 +910,7 @@ TEST(NavigationServerLifecycle, CancelWinsOverAPauseRequestedInTheSameCycle)
|
||||
fixture.spin(1);
|
||||
|
||||
EXPECT_NE(fixture.server_.loop().state(), NavigationState::kPaused) << "pause won over cancel";
|
||||
EXPECT_NE(fixture.server_.loop().state(), NavigationState::kPausing) << "pause won over cancel";
|
||||
}
|
||||
|
||||
TEST(NavigationServerLifecycle, LifecycleRequestIsConsumedExactlyOnce)
|
||||
@@ -914,13 +924,14 @@ TEST(NavigationServerLifecycle, LifecycleRequestIsConsumedExactlyOnce)
|
||||
fixture.spin(2);
|
||||
|
||||
fixture.server_.pause();
|
||||
fixture.spin(1);
|
||||
fixture.spin(2);
|
||||
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kPaused);
|
||||
|
||||
// Không có lệnh mới: cờ đã bị tiêu thụ, các cycle sau không được tự tạm dừng lại lần nữa.
|
||||
fixture.server_.resume();
|
||||
fixture.spin(3);
|
||||
EXPECT_NE(fixture.server_.loop().state(), NavigationState::kPaused);
|
||||
EXPECT_NE(fixture.server_.loop().state(), NavigationState::kPausing);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
@@ -1149,6 +1160,169 @@ TEST(NavigationServerFeedback, NothingIsLatchedBeforeTheFirstRequest)
|
||||
<< "a freshly booted runtime reports a terminal result although no goal has ever been sent";
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// PREEMPTING phải nói thật (2026-09-11): host đọc PREEMPTED là "xong rồi, gửi goal kế được". Cancel
|
||||
// lúc SBPL đang chạy mà báo PREEMPTED sau 33 ms thì host gửi goal kế vào một runtime còn bận —
|
||||
// đúng chuỗi sự kiện trong log robot_app.log dẫn tới ABORTED oan.
|
||||
// ================================================================================================
|
||||
|
||||
TEST(NavigationServerFeedback, PreemptingIsHeldUntilTheCancelledPlannerAttemptReallyEnds)
|
||||
{
|
||||
Fixture fixture;
|
||||
ControlLoopConfig config = baseConfig();
|
||||
config.state_machine.planner_patience = 5.0; // [s]
|
||||
config.state_machine.settle_timeout = 2.0; // [s]
|
||||
fixture.configure(config);
|
||||
fixture.planner_.setLatencyCycles(10);
|
||||
fixture.planner_.setCancelKeepsBusy(true); // SBPL trước pha 2: cancelPlan() không cắt được
|
||||
|
||||
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
|
||||
<< fixture.server_.lastRejectReason();
|
||||
fixture.spin(2);
|
||||
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kPlanning);
|
||||
ASSERT_TRUE(fixture.planner_.isPlanning());
|
||||
|
||||
fixture.server_.cancel();
|
||||
fixture.spin(1);
|
||||
ASSERT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::PREEMPTING);
|
||||
|
||||
// Host poll 100 ms một lần = 2 cycle @ 20 Hz: mỗi lần poll trong lúc thread còn bận phải vẫn
|
||||
// thấy PREEMPTING, không phải PREEMPTED.
|
||||
std::size_t polls_while_busy = 0;
|
||||
for (int i = 0; i < 20 && fixture.planner_.isPlanning(); ++i)
|
||||
{
|
||||
fixture.spin(2);
|
||||
if (fixture.planner_.isPlanning())
|
||||
{
|
||||
++polls_while_busy;
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::PREEMPTING)
|
||||
<< "poll " << i << ": PREEMPTED reported while the planner thread is still busy";
|
||||
}
|
||||
}
|
||||
EXPECT_GE(polls_while_busy, 2u);
|
||||
|
||||
fixture.spin(2);
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::PREEMPTED)
|
||||
<< "once the thread is idle the cancel must be reported (and latched) as PREEMPTED";
|
||||
}
|
||||
|
||||
TEST(NavigationServerFeedback, GoalSentWhilePreemptingIsAcceptedAndRunsOnceThePlannerIsFree)
|
||||
{
|
||||
// Host cũ không đợi PREEMPTED. Vẫn phải chạy đúng: goal kế xếp sau, không bị coi là hỏng.
|
||||
Fixture fixture;
|
||||
ControlLoopConfig config = baseConfig();
|
||||
config.state_machine.planner_patience = 5.0; // [s]
|
||||
config.state_machine.max_planning_retries = 0; // như máy chạy log
|
||||
fixture.configure(config);
|
||||
fixture.planner_.setLatencyCycles(10);
|
||||
fixture.planner_.setCancelKeepsBusy(true);
|
||||
fixture.controller_.setScript({ ControllerScript::kOk });
|
||||
|
||||
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
|
||||
<< fixture.server_.lastRejectReason();
|
||||
fixture.spin(2);
|
||||
fixture.server_.cancel();
|
||||
fixture.spin(1);
|
||||
ASSERT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::PREEMPTING);
|
||||
|
||||
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(5.0).goal, 0.15, 0.10))
|
||||
<< fixture.server_.lastRejectReason();
|
||||
|
||||
bool saw_clearing_or_aborted = false;
|
||||
bool reached_controlling = false;
|
||||
for (int i = 0; i < 60 && !reached_controlling; ++i)
|
||||
{
|
||||
fixture.spin(1);
|
||||
const auto host = fixture.server_.getFeedback()->navigation_state;
|
||||
saw_clearing_or_aborted = saw_clearing_or_aborted ||
|
||||
host == robot::move_base_core::State::CLEARING ||
|
||||
host == robot::move_base_core::State::ABORTED;
|
||||
reached_controlling = host == robot::move_base_core::State::CONTROLLING;
|
||||
}
|
||||
EXPECT_FALSE(saw_clearing_or_aborted) << "the second goal went through recovery/abort";
|
||||
EXPECT_TRUE(reached_controlling);
|
||||
}
|
||||
|
||||
TEST(NavigationServerFeedback, PausedIsReportedOnlyOnceTheRuntimeIsQuiescent)
|
||||
{
|
||||
// PAUSING nói thật (2026-09-11): pause lúc SBPL còn chạy → host vẫn thấy PLANNING (+ feed_back_str
|
||||
// "pausing…"), KHÔNG phải PAUSED; PAUSED chỉ khi thread planner đã nhả.
|
||||
Fixture fixture;
|
||||
ControlLoopConfig config = baseConfig();
|
||||
config.state_machine.planner_patience = 5.0; // [s]
|
||||
fixture.configure(config);
|
||||
fixture.planner_.setLatencyCycles(10);
|
||||
fixture.planner_.setCancelKeepsBusy(true);
|
||||
|
||||
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
|
||||
<< fixture.server_.lastRejectReason();
|
||||
fixture.spin(2);
|
||||
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kPlanning);
|
||||
|
||||
fixture.server_.pause();
|
||||
fixture.spin(1);
|
||||
ASSERT_EQ(fixture.server_.loop().state(), NavigationState::kPausing);
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PLANNING)
|
||||
<< "while pausing the host must keep seeing the state that is still winding down";
|
||||
EXPECT_NE(fixture.server_.getFeedback()->feed_back_str.find("pausing"), std::string::npos);
|
||||
|
||||
std::size_t polls_while_busy = 0;
|
||||
for (int i = 0; i < 20 && fixture.planner_.isPlanning(); ++i)
|
||||
{
|
||||
fixture.spin(2);
|
||||
if (fixture.planner_.isPlanning())
|
||||
{
|
||||
++polls_while_busy;
|
||||
EXPECT_NE(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PAUSED)
|
||||
<< "poll " << i << ": PAUSED reported while the planner thread is still busy";
|
||||
}
|
||||
}
|
||||
EXPECT_GE(polls_while_busy, 2u);
|
||||
|
||||
fixture.spin(2);
|
||||
EXPECT_EQ(fixture.server_.loop().state(), NavigationState::kPaused);
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::PAUSED);
|
||||
}
|
||||
|
||||
TEST(NavigationServerFeedback, StalledControlThreadIsReportedAsLostByTheWatchdog)
|
||||
{
|
||||
// Pha 4 (2026-09-11): control thread treo thì refreshFeedback() không chạy nữa, host đọc mãi
|
||||
// state cuối. Watchdog (thread riêng) phải ghi đè thành LOST trong getFeedback() — trên thread host.
|
||||
Fixture fixture;
|
||||
fixture.configure();
|
||||
fixture.controller_.setScript({ ControllerScript::kOk });
|
||||
|
||||
move_base2::WatchdogConfig dog;
|
||||
dog.period = 0.01; // [s]
|
||||
dog.control_loop_stall_budget = 0.05; // [s]
|
||||
std::string error;
|
||||
ASSERT_TRUE(fixture.server_.startWatchdog(&fixture.components_, dog, error)) << error;
|
||||
|
||||
ASSERT_TRUE(fixture.server_.moveTo(makeRequest(3.0).goal, 0.15, 0.10))
|
||||
<< fixture.server_.lastRejectReason();
|
||||
fixture.spin(3); // mỗi spinOnce() là một heartbeat của "control thread"
|
||||
ASSERT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::CONTROLLING);
|
||||
|
||||
// Control thread "treo": không spinOnce nữa. Watchdog phải tự thấy sau ngân sách.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(150));
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state, robot::move_base_core::State::LOST)
|
||||
<< "a frozen CONTROLLING must not look like a running one";
|
||||
EXPECT_NE(fixture.server_.getFeedback()->feed_back_str.find("control thread stalled"),
|
||||
std::string::npos);
|
||||
|
||||
// Thread quay lại → hết LOST, state thật quay về.
|
||||
fixture.spin(2);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(30));
|
||||
fixture.spin(1);
|
||||
EXPECT_EQ(fixture.server_.getFeedback()->navigation_state,
|
||||
robot::move_base_core::State::CONTROLLING);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
// executeInstantAction() — cửa duy nhất nhận một action lẻ
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user