feat: add an abort flag ARAPlanner polls so cancel/pause can cut a running search

SBPLPlanner::set_abort_flag(const std::atomic<bool>*) / 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 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 22:06:02 +07:00
parent ca1ba36981
commit bd8992469e
2 changed files with 51 additions and 2 deletions

View File

@@ -30,8 +30,24 @@
#ifndef __PLANNER_H_
#define __PLANNER_H_
#include <atomic>
#include <cstddef>
#include <vector>
/**
* 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 <sbpl/config.h>
#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<bool>* 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<bool>* abort_flag_ = NULL;
};
#endif

View File

@@ -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<int>& 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<int>& 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<PlannerStats>* 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;
}