feat: onCancelRequested() sync hook + ABI version guard for RecoveryBehavior plugins

- RecoveryBehavior::cancel() now calls a new virtual onCancelRequested()
  synchronously, from whichever thread calls cancel(). Documents onCancel()
  as effectively dead in move_base2: it only ran on the next update() tick,
  and CANCELLING forces tick_recovery=false, so that tick never came. Every
  existing behavior's onCancel() was unreachable code. Velocity-only
  behaviors (wait/rotate/back_up/clear_costmap) still stop correctly via the
  arbiter zeroing cmd_vel; this only matters for a behavior that owns a
  long-running operation of its own (see Test/sbpl_recovery, separate
  commit).
- recovery_abi.h (new) + RECOVERY_CORE_EXPORT_BEHAVIOR(factory, symbol)
  macro: exports an extra <symbol>_abi version symbol alongside the
  existing BOOST_DLL_ALIAS factory. recovery_registry.cpp probes it before
  calling the factory and refuses to load a .so whose version doesn't
  match (or is missing) instead of dlsym+reinterpret_cast blindly onto a
  vtable that just grew a virtual — the ABI hazard boost::dll plugins
  don't otherwise catch at load time.
- All 4 in-tree behaviors switched to the export macro.
- test/plugins/stale_abi_recovery.cpp (new): a plugin built against the
  old ABI, used to verify the registry actually refuses it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 22:06:40 +07:00
parent c03d16f98c
commit 254c7fc181
12 changed files with 256 additions and 15 deletions

View File

@@ -0,0 +1,51 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* recovery_core — phiên bản ABI của biên plugin và macro export cho plugin .so.
*
* Author: DuongTD
*********************************************************************/
#ifndef RECOVERY_CORE_RECOVERY_ABI_H_
#define RECOVERY_CORE_RECOVERY_ABI_H_
#include <boost/dll/alias.hpp>
namespace recovery_core
{
/**
* Phiên bản ABI của `RecoveryBehavior` (vtable + layout struct đi qua biên .so).
*
* TĂNG mỗi lần đổi chữ ký/virtual của `RecoveryBehavior`, `RecoveryResult`, `RecoveryGoal`,
* `RecoveryContext`. Plugin build với header cũ nạp vào runtime mới thì lời gọi ảo nhảy vào slot
* sai — crash lúc chạy, không có compile error nào bắt được (boost::dll = dlsym + reinterpret_cast).
* `RecoveryRegistry` tra symbol `<Type>_abi` TRƯỚC factory và từ chối nạp khi lệch.
*
* 1 — trước 2026-09-11 (không có symbol; registry coi là "không khai").
* 2 — 2026-09-11: thêm virtual `onCancelRequested()`.
*/
constexpr int kRecoveryAbiVersion = 2;
/// Hàm được export kèm mỗi plugin qua @ref RECOVERY_CORE_EXPORT_BEHAVIOR; trả @ref kRecoveryAbiVersion.
inline int recoveryAbiVersion()
{
return kRecoveryAbiVersion;
}
} // namespace recovery_core
/**
* @brief Export một behavior cho boost::dll kèm phiên bản ABI.
*
* Thay cho `BOOST_DLL_ALIAS(Factory, Symbol)` thuần: sinh thêm symbol `<Symbol>_abi` trả
* @ref recovery_core::kRecoveryAbiVersion để registry kiểm trước khi gọi factory.
*
* @param factory Hàm/static member trả `RecoveryBehavior::RecoveryBehaviorPtr`.
* @param symbol Tên symbol khớp với `type` trong YAML (`{name: wait, type: WaitRecovery}`).
*/
#define RECOVERY_CORE_EXPORT_BEHAVIOR(factory, symbol) \
BOOST_DLL_ALIAS(factory, symbol) \
BOOST_DLL_ALIAS(recovery_core::recoveryAbiVersion, symbol##_abi)
#endif // RECOVERY_CORE_RECOVERY_ABI_H_

View File

@@ -91,11 +91,13 @@ public:
RecoveryResult update(const robot::Time& now);
/**
* @brief Yêu cầu dừng. Tick kế tiếp trả stop output + `kCancelled`.
* @brief Yêu cầu dừng. Gọi @ref onCancelRequested NGAY (đồng bộ), rồi tick kế tiếp (nếu có) trả
* stop output + `kCancelled`.
*
* @note Trong `move_base2`, state machine không tick recovery sau khi cancel (state chuyển sang
* `CANCELLING`, nơi mọi nguồn vận tốc bị khoá), nên `kCancelled` thường không bao giờ đến
* tay caller. Đường này vẫn phải đúng: nó là hàng rào cho host khác và cho tương lai.
* `CANCELLING`, nơi mọi nguồn vận tốc bị khoá), nên `kCancelled`/`onCancel()` không bao
* giờ đến tay caller — chỉ @ref onCancelRequested là chắc chắn chạy. Behavior có việc chạy
* dài (worker thread, planner) phải kết thúc việc đó ngay trong hook ấy (2026-09-11).
*/
void cancel();
@@ -155,11 +157,24 @@ protected:
virtual RecoveryResult onUpdate(const robot::Time& now, double dt) = 0;
/**
* @brief Cơ hội phát lệnh giảm tốc trước khi dừng hẳn.
* @brief Gọi NGAY trong @ref cancel, trên thread gọi cancel(), trước bất kỳ tick nào.
*
* Default trả stop output ngay. Plugin họ velocity đang chạy nhanh nên override để ramp về 0 thay
* vì nhảy bậc — dù `VelocityArbiter` phía ngoài có clamp gia tốc, kế hoạch dừng là việc của
* plugin, không phải của hàng rào cuối.
* Đây là hook DUY NHẤT chắc chắn chạy khi bị huỷ (xem ghi chú ở @ref cancel). Plugin có việc chạy
* dài — worker thread, một lượt makePlan — phải cắt và ĐỢI việc đó kết thúc ở đây, có trần thời
* gian; khi hàm trả về, runner coi behavior đã im (bảng trạng thái thành phần về idle). Không
* được phát vận tốc ở đây. Default no-op. (2026-09-11)
*/
virtual void onCancelRequested()
{
}
/**
* @brief Tick kết thúc sau cancel — CHỈ chạy nếu caller còn tick thêm một lần sau @ref cancel.
*
* `move_base2` không làm vậy (CANCELLING không tick recovery), và `VelocityArbiter` phát 0 tức
* thì ở mọi state phải dừng nên có ramp ở đây cũng bị đè (quyết định 2026-09-11: lệnh lifecycle =
* 0 ngay, giảm tốc là việc của driver bánh xe). Dùng để dọn state nội bộ nếu caller khác tick;
* KHÔNG dùng để ramp. Default trả stop output.
*/
virtual RecoveryResult onCancel();