feat: ActionHandler pause/resume + kCancelled status + ABI version guard

- ActionStatus::kCancelled (new, string "CANCELLED"): the missing "stopped
  on request" terminal status — previously a cancelled action only had
  Running/Succeeded/Failed to report through, so move_base2's state
  machine couldn't tell a cancelled action from a failed one.
- ActionHandler::cancelNeedsTicks() (default false), pause(now) and
  resume(now) (default no-ops): lets a handler opt into "I need more
  ticks to actually stop" (device handshake) instead of being torn down
  immediately, and gives handlers a real pause/resume contract instead of
  move_base2 having to freeze/unfreeze them from outside.
- action_abi.h (new) + ACTION_CORE_EXPORT_HANDLER macro, same pattern as
  recovery_core commit 254c7fc: exports a version symbol next to the
  factory alias; action_registry.cpp refuses a handler .so whose version
  doesn't match instead of dlsym+reinterpret_cast blindly onto a grown
  vtable.
- frame_sampler / wait / noop handlers implement pause/resume via
  paused_at_ / started_at_ += (now - paused_at_) so their own internal
  timers don't count paused time; paused_ is reset on start() so a reused
  handler instance can't come up already paused.
- test/plugins/stale_abi_action_handler.cpp (new) for the registry refusal
  test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 22:07:00 +07:00
parent a6bc3dfa3d
commit 669c3f7c06
13 changed files with 305 additions and 13 deletions

View File

@@ -386,6 +386,9 @@ if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND") message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif() endif()
# Plugin giả cho ABI guard test: chỉ build cùng test, không install.
add_action_core_plugin(action_core_test_stale_abi_handler test/plugins/stale_abi_action_handler.cpp)
set(ACTION_CORE_TESTS set(ACTION_CORE_TESTS
action_registry_test action_registry_test
noop_action_handler_test noop_action_handler_test

View File

@@ -0,0 +1,48 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* action_core — phiên bản ABI của biên plugin và macro export cho plugin .so.
*
* Author: DuongTD
*********************************************************************/
#ifndef ACTION_CORE_ACTION_ABI_H_
#define ACTION_CORE_ACTION_ABI_H_
#include <boost/dll/alias.hpp>
namespace action_core
{
/**
* Phiên bản ABI của `ActionHandler` (vtable + layout struct đi qua biên .so).
*
* TĂNG mỗi lần đổi chữ ký/virtual của `ActionHandler`, `ActionTick`, `ActionContext`. 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). `ActionRegistry` 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 `cancelNeedsTicks()`, `pause()`, `resume()`; `ActionStatus::kCancelled`.
*/
constexpr int kActionAbiVersion = 2;
/// Hàm được export kèm mỗi plugin qua @ref ACTION_CORE_EXPORT_HANDLER; trả @ref kActionAbiVersion.
inline int actionAbiVersion()
{
return kActionAbiVersion;
}
} // namespace action_core
/**
* @brief Export một handler 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 action_core::kActionAbiVersion để registry kiểm trước khi gọi factory.
*/
#define ACTION_CORE_EXPORT_HANDLER(factory, symbol) \
BOOST_DLL_ALIAS(factory, symbol) \
BOOST_DLL_ALIAS(action_core::actionAbiVersion, symbol##_abi)
#endif // ACTION_CORE_ACTION_ABI_H_

View File

@@ -79,8 +79,40 @@ public:
/// @brief Một control cycle. Chỉ được gọi sau khi @ref start trả true. /// @brief Một control cycle. Chỉ được gọi sau khi @ref start trả true.
virtual ActionTick update(const robot::Time& now) = 0; virtual ActionTick update(const robot::Time& now) = 0;
/// @brief Yêu cầu dừng an toàn. Handler phải đưa thiết bị về trạng thái an toàn, không treo. /**
* @brief Yêu cầu dừng an toàn. Handler phải đưa thiết bị về trạng thái an toàn, không treo.
*
* Hai kiểu handler (2026-09-11):
* - @ref cancelNeedsTicks trả false (mặc định; perception, no-op): cancel() làm xong việc ngay,
* caller quên handler, không tick nữa.
* - trả true (thiết bị: nâng kệ, sạc): cancel() chỉ RA LỆNH dừng; caller tiếp tục gọi
* @ref update cho tới khi handler trả `kCancelled` (hoặc kSucceeded/kFailed). Không được block
* trong cancel() — nó chạy trên control thread.
*/
virtual void cancel() = 0; virtual void cancel() = 0;
/// @brief Sau cancel() handler còn cần được tick tới khi trả terminal không. Xem @ref cancel.
virtual bool cancelNeedsTicks() const
{
return false;
}
/**
* @brief Tạm dừng (2026-09-11). KHÔNG dừng thiết bị — pause là đóng băng, không huỷ.
*
* Caller ngừng gọi update() cho tới khi @ref resume. Handler có timeout riêng PHẢI ghi mốc để
* resume() dịch `started_at_`: nếu không, 20 s đứng chờ bị tính vào timeout dò 15 s và tick đầu
* sau resume là kFailed → ABORTED oan (lỗi có thật, xem move_base2/docs/PLAN_LIFECYCLE…).
* Default no-op — handler không có đồng hồ thì không cần gì.
*/
virtual void pause(const robot::Time& /*now*/)
{
}
/// @brief Tiếp tục sau @ref pause: cộng quãng dừng vào mốc timeout. Default no-op.
virtual void resume(const robot::Time& /*now*/)
{
}
}; };
} // namespace action_core } // namespace action_core

View File

@@ -18,15 +18,17 @@ namespace action_core
* @enum ActionStatus * @enum ActionStatus
* @brief Tiến trình của action đang chạy, trả về mỗi control cycle. * @brief Tiến trình của action đang chạy, trả về mỗi control cycle.
* *
* Không có `kCancelled`: huỷ đi qua @ref ActionHandler::cancel và kết thúc lượt chạy ngay tại đó, * `kCancelled` (2026-09-11): chỉ handler khai @ref ActionHandler::cancelNeedsTicks mới trả nó —
* caller không tick tiếp nên không có kết quả nào để mang trạng thái đó. Cùng lý do mà * sau @ref ActionHandler::cancel handler tiếp tục được tick để đưa thiết bị về vị trí an toàn rồi
* `recovery_core::RecoveryStatus::kCancelled` không bao giờ vượt ranh giới port. * báo `kCancelled`; caller coi đó là terminal. Handler không cần tick sau cancel (5 handler hiện
* có: perception/no-op) không bao giờ trả giá trị này.
*/ */
enum class ActionStatus enum class ActionStatus
{ {
kRunning, ///< Chưa xong, caller phải tiếp tục gọi update(). kRunning, ///< Chưa xong, caller phải tiếp tục gọi update().
kSucceeded, ///< Thiết bị đã hoàn tất thao tác. kSucceeded, ///< Thiết bị đã hoàn tất thao tác.
kFailed ///< Không thực hiện được, hoặc quá thời gian của chính handler. kFailed, ///< Không thực hiện được, hoặc quá thời gian của chính handler.
kCancelled ///< Đã dừng an toàn sau cancel() (handler có cancelNeedsTicks). Terminal.
}; };
const char* toString(ActionStatus status); const char* toString(ActionStatus status);

View File

@@ -16,7 +16,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/dll/alias.hpp> #include <action_core/action_abi.h>
#include <robot/robot.h> #include <robot/robot.h>
#include <tf3/buffer_core.h> #include <tf3/buffer_core.h>
@@ -296,6 +296,7 @@ public:
bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override
{ {
started_at_ = now; started_at_ = now;
paused_ = false;
action_id_ = action.actionId; action_id_ = action.actionId;
resetRun(); resetRun();
simulated_goal_published_ = false; simulated_goal_published_ = false;
@@ -411,6 +412,27 @@ public:
return tick; return tick;
} }
/**
* @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào timeout.
*/
void pause(const robot::Time& now) override
{
if (!paused_)
{
paused_ = true;
paused_at_ = now;
}
}
void resume(const robot::Time& now) override
{
if (paused_)
{
started_at_ += (now - paused_at_);
paused_ = false;
}
}
void cancel() override void cancel() override
{ {
// Không có thiết bị nào để dừng; chỉ bỏ mẫu để lượt sau không thừa hưởng dữ liệu cũ. // Không có thiết bị nào để dừng; chỉ bỏ mẫu để lượt sau không thừa hưởng dữ liệu cũ.
@@ -606,6 +628,8 @@ private:
PoseSampleFilter filter_; PoseSampleFilter filter_;
robot::Time started_at_; robot::Time started_at_;
robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_.
bool paused_ = false;
std::string action_id_; std::string action_id_;
std::string frame_; std::string frame_;
bool simulated_goal_published_ = false; bool simulated_goal_published_ = false;
@@ -620,4 +644,4 @@ private:
} // namespace action_core } // namespace action_core
BOOST_DLL_ALIAS(action_core::FrameSamplerActionHandler::create, FrameSamplerActionHandler) ACTION_CORE_EXPORT_HANDLER(action_core::FrameSamplerActionHandler::create, FrameSamplerActionHandler)

View File

@@ -13,7 +13,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/dll/alias.hpp> #include <action_core/action_abi.h>
#include <robot/robot.h> #include <robot/robot.h>
namespace action_core namespace action_core
@@ -115,4 +115,4 @@ private:
} // namespace action_core } // namespace action_core
BOOST_DLL_ALIAS(action_core::LogReportActionHandler::create, LogReportActionHandler) ACTION_CORE_EXPORT_HANDLER(action_core::LogReportActionHandler::create, LogReportActionHandler)

View File

@@ -14,7 +14,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/dll/alias.hpp> #include <action_core/action_abi.h>
#include <robot/robot.h> #include <robot/robot.h>
namespace action_core namespace action_core
@@ -113,6 +113,7 @@ public:
bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override bool start(const robot_protocol_msgs::Action& action, const robot::Time& now) override
{ {
started_at_ = now; started_at_ = now;
paused_ = false;
action_id_ = action.actionId; action_id_ = action.actionId;
robot::log_info("[action_core] '%s': starting action '%s' (id '%s'), duration %.3f s.", robot::log_info("[action_core] '%s': starting action '%s' (id '%s'), duration %.3f s.",
@@ -154,6 +155,27 @@ public:
return tick; return tick;
} }
/**
* @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào duration/timeout.
*/
void pause(const robot::Time& now) override
{
if (!paused_)
{
paused_ = true;
paused_at_ = now;
}
}
void resume(const robot::Time& now) override
{
if (paused_)
{
started_at_ += (now - paused_at_);
paused_ = false;
}
}
void cancel() override void cancel() override
{ {
// Không có thiết bị nào để đưa về trạng thái an toàn. Handler thật phải làm việc đó ở đây. // Không có thiết bị nào để đưa về trạng thái an toàn. Handler thật phải làm việc đó ở đây.
@@ -169,9 +191,11 @@ private:
bool hang_ = false; ///< true = không bao giờ hoàn tất (fault injection) bool hang_ = false; ///< true = không bao giờ hoàn tất (fault injection)
robot::Time started_at_; robot::Time started_at_;
robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_.
bool paused_ = false;
std::string action_id_; std::string action_id_;
}; };
} // namespace action_core } // namespace action_core
BOOST_DLL_ALIAS(action_core::NoopActionHandler::create, NoopActionHandler) ACTION_CORE_EXPORT_HANDLER(action_core::NoopActionHandler::create, NoopActionHandler)

View File

@@ -16,7 +16,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/dll/alias.hpp> #include <action_core/action_abi.h>
#include <robot/robot.h> #include <robot/robot.h>
namespace action_core namespace action_core
@@ -99,6 +99,7 @@ public:
{ {
action_id_ = action.actionId; action_id_ = action.actionId;
started_at_ = now; started_at_ = now;
paused_ = false;
if (!readDuration(action, duration_)) if (!readDuration(action, duration_))
{ {
@@ -130,6 +131,27 @@ public:
return tick; return tick;
} }
/**
* @brief Tạm dừng: ghi mốc để resume() dịch started_at_ — quãng đứng không tính vào timeout.
*/
void pause(const robot::Time& now) override
{
if (!paused_)
{
paused_ = true;
paused_at_ = now;
}
}
void resume(const robot::Time& now) override
{
if (paused_)
{
started_at_ += (now - paused_at_);
paused_ = false;
}
}
void cancel() override void cancel() override
{ {
// Không có thiết bị nào để đưa về trạng thái an toàn — chờ thì dừng chờ là xong. // Không có thiết bị nào để đưa về trạng thái an toàn — chờ thì dừng chờ là xong.
@@ -186,10 +208,12 @@ private:
double max_duration_ = kDefaultMaxDuration; ///< [s] double max_duration_ = kDefaultMaxDuration; ///< [s]
robot::Time started_at_; robot::Time started_at_;
robot::Time paused_at_; ///< Mốc pause() gần nhất; chỉ có nghĩa khi paused_.
bool paused_ = false;
double duration_ = 0.0; ///< [s] của action đang chạy double duration_ = 0.0; ///< [s] của action đang chạy
std::string action_id_; std::string action_id_;
}; };
} // namespace action_core } // namespace action_core
BOOST_DLL_ALIAS(action_core::WaitActionHandler::create, WaitActionHandler) ACTION_CORE_EXPORT_HANDLER(action_core::WaitActionHandler::create, WaitActionHandler)

View File

@@ -8,6 +8,8 @@
*********************************************************************/ *********************************************************************/
#include <action_core/action_registry.h> #include <action_core/action_registry.h>
#include <action_core/action_abi.h>
#include <utility> #include <utility>
#include <boost/dll/import.hpp> #include <boost/dll/import.hpp>
@@ -112,6 +114,34 @@ bool ActionRegistry::loadOne(const std::string& name, const std::string& type,
} }
std::function<ActionHandler::Ptr()> factory; std::function<ActionHandler::Ptr()> factory;
// ABI guard (2026-09-11): plugin build với header ActionHandler cũ có vtable ngắn hơn; nạp vào
// runtime mới thì lời gọi virtual mới (pause/resume/cancelNeedsTicks) nhảy vào byte rác — crash
// đúng lúc người vận hành bấm pause. Tra `<type>_abi` trước; thiếu hoặc lệch thì từ chối, log LỖI;
// mission mang actionType đó sẽ bị từ chối ở start() thay vì crash cả tiến trình.
try
{
const auto abi = boost::dll::import_alias<int()>(library_path, type + "_abi",
boost::dll::load_mode::append_decorations);
const int plugin_abi = abi();
if (plugin_abi != kActionAbiVersion)
{
robot::log_error("[action_core] ActionRegistry: '%s' from '%s' was built for ABI %d but this "
"runtime is ABI %d — refusing to load it. Rebuild the plugin against the "
"current action_core headers.",
type.c_str(), library_path.c_str(), plugin_abi, kActionAbiVersion);
return false;
}
}
catch (const std::exception& ex)
{
robot::log_error("[action_core] ActionRegistry: '%s' from '%s' exports no '%s_abi' symbol "
"(built before 2026-09-11 or not using ACTION_CORE_EXPORT_HANDLER) — refusing "
"to load it: %s",
type.c_str(), library_path.c_str(), type.c_str(), ex.what());
return false;
}
try try
{ {
factory = boost::dll::import_alias<ActionHandler::Ptr()>( factory = boost::dll::import_alias<ActionHandler::Ptr()>(

View File

@@ -21,6 +21,8 @@ const char* toString(ActionStatus status)
return "SUCCEEDED"; return "SUCCEEDED";
case ActionStatus::kFailed: case ActionStatus::kFailed:
return "FAILED"; return "FAILED";
case ActionStatus::kCancelled:
return "CANCELLED";
} }
return "UNKNOWN"; return "UNKNOWN";
} }

View File

@@ -198,6 +198,24 @@ TEST(ActionRegistry, ReportsMissingLibraryPath)
EXPECT_EQ(registry.size(), 0u); EXPECT_EQ(registry.size(), 0u);
} }
TEST(ActionRegistry, RefusesAPluginBuiltForAnotherAbiVersion)
{
// 2026-09-11: header ActionHandler thêm virtual; plugin build với header cũ có vtable ngắn hơn và
// sẽ crash ở lời gọi pause(). Registry tra `<Type>_abi` trước factory và từ chối khi lệch.
robot::NodeHandle nh;
ActionRegistry registry;
EXPECT_FALSE(registry.loadFromConfig(nh, "actions_stale_abi"));
EXPECT_EQ(registry.size(), 0u) << "a plugin with a mismatching _abi must not be registered";
}
TEST(ActionRegistry, RefusesAPluginWithoutAnAbiSymbol)
{
robot::NodeHandle nh;
ActionRegistry registry;
EXPECT_FALSE(registry.loadFromConfig(nh, "actions_no_abi"));
EXPECT_EQ(registry.size(), 0u) << "a plugin built before the ABI guard must not be registered";
}
// ================================================================================================ // ================================================================================================
// Contract của registerHandler // Contract của registerHandler
// ================================================================================================ // ================================================================================================

View File

@@ -49,6 +49,15 @@ actions_missing_library:
actions_empty: actions_empty:
something_else: 1 something_else: 1
# ABI guard (2026-09-11): plugin giả export `_abi` = 1 (lệch) và một alias không có `_abi`.
# Registry phải từ chối cả hai, log lỗi, không crash.
actions_stale_abi:
handlers:
- {name: stale, type: StaleAbiActionHandler}
actions_no_abi:
handlers:
- {name: ancient, type: NoAbiActionHandler}
# Bộ handler default, mỗi cái một namespace riêng để test nạp độc lập. # Bộ handler default, mỗi cái một namespace riêng để test nạp độc lập.
actions_wait: actions_wait:
handlers: handlers:
@@ -193,6 +202,10 @@ actions_detect_simulated:
NoopActionHandler: NoopActionHandler:
library_path: libaction_core_noop_action_handler library_path: libaction_core_noop_action_handler
StaleAbiActionHandler:
library_path: libaction_core_test_stale_abi_handler
NoAbiActionHandler:
library_path: libaction_core_test_stale_abi_handler
FrameSamplerActionHandler: FrameSamplerActionHandler:
library_path: libaction_core_frame_sampler_action_handler library_path: libaction_core_frame_sampler_action_handler

View File

@@ -0,0 +1,72 @@
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* action_core — plugin GIẢ mô phỏng một handler build với header cũ: export `_abi` = 1 (và một
* symbol không có `_abi` nào). Chỉ để test ABI guard của ActionRegistry; không cài đặt.
*
* Author: DuongTD
*********************************************************************/
#include <memory>
#include <string>
#include <vector>
#include <boost/dll/alias.hpp>
#include <action_core/action_handler.h>
namespace action_core
{
namespace testing
{
/// Handler tối thiểu, hợp lệ về mặt interface — registry phải từ chối nó CHỈ vì phiên bản ABI.
class StaleAbiActionHandler final : public ActionHandler
{
public:
static ActionHandler::Ptr create()
{
return std::make_shared<StaleAbiActionHandler>();
}
bool configure(const std::string&, const ActionContext&, robot::NodeHandle&) override
{
return true;
}
std::vector<std::string> supportedActionTypes() const override
{
return {"stale"};
}
bool start(const robot_protocol_msgs::Action&, const robot::Time&) override
{
return true;
}
ActionTick update(const robot::Time&) override
{
ActionTick tick;
tick.status = ActionStatus::kSucceeded;
return tick;
}
void cancel() override
{
}
};
/// Phiên bản ABI "cũ" — lệch với kActionAbiVersion của runtime.
int staleAbiVersion()
{
return 1;
}
} // namespace testing
} // namespace action_core
// Alias 1: có _abi nhưng lệch phiên bản.
BOOST_DLL_ALIAS(action_core::testing::StaleAbiActionHandler::create, StaleAbiActionHandler)
BOOST_DLL_ALIAS(action_core::testing::staleAbiVersion, StaleAbiActionHandler_abi)
// Alias 2: không có _abi (plugin build trước 2026-09-11).
BOOST_DLL_ALIAS(action_core::testing::StaleAbiActionHandler::create, NoAbiActionHandler)