Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/behaviortree_cpp/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Tree

void initialize();

/// Halt the tree and wake tickWhileRunning(). Concurrent or reentrant calls
/// return immediately while the first call is still halting the tree.
void haltTree();

[[nodiscard]] TreeNode* rootNode() const;
Expand Down
3 changes: 3 additions & 0 deletions include/behaviortree_cpp/tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "behaviortree_cpp/utils/strcat.hpp"
#include "behaviortree_cpp/utils/wakeup_signal.hpp"

#include <atomic>
#include <charconv>
#include <exception>
#include <map>
Expand Down Expand Up @@ -470,6 +471,8 @@ class TreeNode
Expected<NodeStatus> checkPreConditions();
void checkPostConditions(NodeStatus status);

std::atomic_bool& treeHaltFlag();

/// The method used to interrupt the execution of a RUNNING node.
/// Only Async nodes that may return RUNNING should implement it.
virtual void halt() = 0;
Expand Down
35 changes: 31 additions & 4 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,19 +671,46 @@ void Tree::initialize()
// NOLINTNEXTLINE(readability-make-member-function-const)
void Tree::haltTree()
{
if(rootNode() == nullptr)
auto* root = rootNode();
if(root == nullptr)
{
return;
}

auto& halting = root->treeHaltFlag();
if(halting.exchange(true))
{
return;
}
// Release the guard even if a node's halt callback throws.
class ResetHaltFlag
{
public:
explicit ResetHaltFlag(std::atomic_bool& flag) : flag_(flag)
{}
~ResetHaltFlag()
{
flag_.store(false);
}
ResetHaltFlag(const ResetHaltFlag&) = delete;
ResetHaltFlag& operator=(const ResetHaltFlag&) = delete;
ResetHaltFlag(ResetHaltFlag&&) = delete;
ResetHaltFlag& operator=(ResetHaltFlag&&) = delete;

private:
std::atomic_bool& flag_;
};
const ResetHaltFlag reset_halting(halting);

// the halt should propagate to all the node if the nodes
// have been implemented correctly
rootNode()->haltNode();
root->haltNode();

//but, just in case.... this should be no-op
auto visitor = [](BT::TreeNode* node) { node->haltNode(); };
BT::applyRecursiveVisitor(rootNode(), visitor);
BT::applyRecursiveVisitor(root, visitor);

rootNode()->resetStatus();
root->resetStatus();

// interrupt the sleep of tickWhileRunning(), if called from another thread
if(wake_up_)
Expand Down
7 changes: 7 additions & 0 deletions src/tree_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct TreeNode::PImpl
const std::string name;

NodeStatus status = NodeStatus::IDLE;
// Tree::haltTree() uses this only on the root node.
std::atomic_bool tree_halting{ false };

std::condition_variable state_condition_variable;

Expand Down Expand Up @@ -178,6 +180,11 @@ void TreeNode::haltNode()
}
}

std::atomic_bool& TreeNode::treeHaltFlag()
{
return _p->tree_halting;
}

void TreeNode::setStatus(NodeStatus new_status)
{
if(new_status == NodeStatus::IDLE)
Expand Down
80 changes: 80 additions & 0 deletions tests/gtest_finally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <functional>
#include <future>
#include <stdexcept>
#include <thread>
Expand Down Expand Up @@ -788,3 +789,82 @@ TEST_F(FinallyTest, ConcurrentHaltDuringHaltLoop_DoesNotTickCleanupConcurrently)
EXPECT_EQ(ticks, 3);
EXPECT_EQ(tree.rootNode()->status(), NodeStatus::IDLE);
}

namespace
{
class CallbackCleanup : public AsyncMain
{
public:
CallbackCleanup(const std::string& name, const BT::NodeConfig& config, int* halts,
std::function<NodeStatus()> on_running)
: AsyncMain(name, config, halts, false, false), on_running_(std::move(on_running))
{}

NodeStatus onRunning() override
{
return on_running_();
}

private:
std::function<NodeStatus()> on_running_;
};

constexpr auto halt_test_tree = R"(
<root BTCPP_format="4"><BehaviorTree>
<Finally halt_timeout_msec="5000"><AlwaysRunning/><CallbackCleanup/></Finally>
</BehaviorTree></root>)";
} // namespace

TEST_F(FinallyTest, ConcurrentHaltDuringHaltLoop_DoesNotHaltRunningCleanup)
{
int cleanup_halts = 0;
std::promise<void> entered;
std::promise<void> release;
auto entered_future = entered.get_future();
auto release_future = release.get_future();
factory.registerNodeType<CallbackCleanup>("CallbackCleanup", &cleanup_halts, [&] {
entered.set_value();
release_future.wait();
return NodeStatus::SUCCESS;
});
auto tree = factory.createTreeFromText(halt_test_tree);

auto first_halt = std::async(std::launch::async, [&] {
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
tree.haltTree();
});

const auto timeout = std::chrono::seconds(2);
if(entered_future.wait_for(timeout) != std::future_status::ready)
{
release.set_value();
first_halt.get();
FAIL() << "The first halt did not enter cleanup";
}
auto second_halt = std::async(std::launch::async, [&] { tree.haltTree(); });
const bool returned_during_cleanup =
second_halt.wait_for(timeout) == std::future_status::ready;
release.set_value();
second_halt.get();
first_halt.get();

EXPECT_TRUE(returned_during_cleanup);
EXPECT_EQ(cleanup_halts, 0);
EXPECT_EQ(tree.rootNode()->status(), NodeStatus::IDLE);
}

TEST_F(FinallyTest, ReentrantHaltDuringHaltLoop_DoesNotHaltRunningCleanup)
{
int cleanup_halts = 0;
BT::Tree tree;
factory.registerNodeType<CallbackCleanup>("CallbackCleanup", &cleanup_halts, [&] {
tree.haltTree();
return NodeStatus::SUCCESS;
});
tree = factory.createTreeFromText(halt_test_tree);
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
tree.haltTree();

EXPECT_EQ(cleanup_halts, 0);
EXPECT_EQ(tree.rootNode()->status(), NodeStatus::IDLE);
}
3 changes: 3 additions & 0 deletions tests/gtest_halt_child.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ void checkBuiltinParentThrowingHalt(const char* body)
}
EXPECT_NO_THROW(tree.haltTree());
EXPECT_EQ(halts, 1);
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
EXPECT_THROW(tree.haltTree(), std::runtime_error);
EXPECT_EQ(halts, 2);
// ~Tree() calls haltTree() again: it must not throw (-> terminate).
}
} // namespace
Expand Down
Loading