From bd8992469e14976c3dd0ff86cd11876a11f6aa26 Mon Sep 17 00:00:00 2001 From: duongtd Date: Sat, 12 Sep 2026 22:06:02 +0700 Subject: [PATCH] feat: add an abort flag ARAPlanner polls so cancel/pause can cut a running search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SBPLPlanner::set_abort_flag(const std::atomic*) / aborted() let a caller hand ARAPlanner a flag it checks every expansion, in both ImprovePath and the outer Search loop. Aborted -> return "no solution" instead of running to allocated_time. Root cause this fixes (from a C++ host log): cancel during a long SBPL search only set a discard flag — the search ran to allocated_time (10-30 s) before the runtime could report CANCELLED, and the next mission queued behind it also timed out and aborted. This flag is the primitive move_base2's PlannerRunner and sbpl_lattice_planner's Preemptable wrapper (separate repos) build the real preempt on top of. extern "C" int sbpl_abort_flag_abi_version() lets a caller probe for this feature at runtime via dlsym before touching the flag, since two sbpl installs (apt ros-noetic-sbpl vs. this vendored build) can share a soname. Co-Authored-By: Claude Sonnet 5 --- src/include/sbpl/planners/planner.h | 34 +++++++++++++++++++++++++++++ src/planners/araplanner.cpp | 19 ++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/include/sbpl/planners/planner.h b/src/include/sbpl/planners/planner.h index 96ac1a8..df62c95 100644 --- a/src/include/sbpl/planners/planner.h +++ b/src/include/sbpl/planners/planner.h @@ -30,8 +30,24 @@ #ifndef __PLANNER_H_ #define __PLANNER_H_ +#include #include #include + +/** + * Feature macro for consumers (2026-09-11): this sbpl build carries SBPLPlanner::set_abort_flag(). + * A consumer compiled against a header that lacks it must not try to use it; a consumer compiled + * against THIS header but linked at runtime with an older libsbpl.so must also not use it — the + * base-class layout differs — so it should additionally probe sbpl_abort_flag_abi_version() with + * dlsym before the first set_abort_flag() call. + */ +#define SBPL_HAS_ABORT_FLAG 1 +#define SBPL_ABORT_FLAG_ABI_VERSION 1 + +extern "C" { +/** \brief Returns SBPL_ABORT_FLAG_ABI_VERSION; absent from older libsbpl.so builds. */ +int sbpl_abort_flag_abi_version(); +} #include #define GETSTATEIND(stateid, mapid) StateID2IndexMapping[mapid][stateid] @@ -341,10 +357,28 @@ public: */ virtual void set_initialsolution_eps(double initialsolution_eps) { } + /** + * \brief Attach an external abort flag (may be NULL to detach). + * + * The caller owns the flag and sets it from ANOTHER thread while replan() is running; the + * planner polls it in its expansion loop and returns 0 ("no solution") as soon as it sees it. + * Added 2026-09-11 so a navigation runtime can cancel a running search instead of waiting for + * allocated_time to expire (ARA* honours it; other planners ignore it unless they call + * aborted()). Relaxed atomic load, no lock, no allocation. + */ + void set_abort_flag(const std::atomic* flag) { abort_flag_ = flag; } + + /** \brief True once the attached abort flag has been raised. */ + bool aborted() const + { + return abort_flag_ != NULL && abort_flag_->load(std::memory_order_relaxed); + } + virtual ~SBPLPlanner() { } protected: DiscreteSpaceInformation *environment_; + const std::atomic* abort_flag_ = NULL; }; #endif diff --git a/src/planners/araplanner.cpp b/src/planners/araplanner.cpp index c63fd31..dc543de 100644 --- a/src/planners/araplanner.cpp +++ b/src/planners/araplanner.cpp @@ -369,7 +369,9 @@ int ARAPlanner::ImprovePath(ARASearchStateSpace_t* pSearchStateSpace, double Max //expand states until done minkey = pSearchStateSpace->heap->getminkeyheap(); CKey oldkey = minkey; + // `!aborted()`: external cancel (2026-09-11) — polled once per expansion, one relaxed atomic load. while (!pSearchStateSpace->heap->emptyheap() && minkey.key[0] < INFINITECOST && goalkey > minkey && + !aborted() && (clock() - TimeStarted) < MaxNumofSecs * (double)CLOCKS_PER_SEC && (pSearchStateSpace->eps_satisfied == INFINITECOST || (clock() - TimeStarted) < repair_time * (double)CLOCKS_PER_SEC)) @@ -932,7 +934,7 @@ bool ARAPlanner::Search(ARASearchStateSpace_t* pSearchStateSpace, vector& p stats.clear(); int prevexpands = 0; clock_t loop_time; - while (pSearchStateSpace->eps_satisfied > final_epsilon && + while (pSearchStateSpace->eps_satisfied > final_epsilon && !aborted() && (clock() - TimeStarted) < MaxNumofSecs * (double)CLOCKS_PER_SEC && (pSearchStateSpace->eps_satisfied == INFINITECOST || (clock() - TimeStarted) < repair_time * (double)CLOCKS_PER_SEC)) @@ -1016,7 +1018,13 @@ bool ARAPlanner::Search(ARASearchStateSpace_t* pSearchStateSpace, vector& p int solcost = INFINITECOST; bool ret = false; - if (PathCost == INFINITECOST) { + if (aborted()) { + // Cancelled from outside: whatever partial path the heap holds is not a solution the caller + // asked for. Report "no solution"; the caller knows it raised the flag. + SBPL_PRINTF("search aborted by the caller\n"); + ret = false; + } + else if (PathCost == INFINITECOST) { SBPL_PRINTF("could not find a solution\n"); ret = false; } @@ -1284,3 +1292,10 @@ void ARAPlanner::get_search_stats(vector* s) s->push_back(stats[i]); } } + +// Runtime probe for the abort-flag ABI (see planner.h). Lives here because ARA* is the planner +// that honours the flag; the symbol is what a consumer dlsym()s before touching the flag slot. +extern "C" int sbpl_abort_flag_abi_version() +{ + return SBPL_ABORT_FLAG_ABI_VERSION; +}