Skip to content

Commit de12692

Browse files
dvadovszclaude
andcommitted
fix: Keep Finally cleanup to one run on halt and SKIPPED
halt() also runs from ~Tree(), so a cleanup exception there is printed instead of propagated. A SKIPPED result leaves the node RUNNING in executeTick(), so the node resets itself, keeping a later halt from rerunning cleanup. Adds tests for async main and cleanup, halts in each phase, and SKIPPED main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a5f90a0 commit de12692

3 files changed

Lines changed: 141 additions & 5 deletions

File tree

‎include/behaviortree_cpp/controls/finally_node.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace BT
1414
* - If this node is halted while main is RUNNING, main is halted and cleanup
1515
* is ticked once synchronously. If cleanup returns RUNNING, it is halted.
1616
* - The node returns main's status, or FAILURE if cleanup fails.
17-
* - Exceptions thrown by cleanup propagate.
17+
* - Exceptions thrown by cleanup propagate from tick(). During halt they are
18+
* printed to stderr instead, because halt() also runs from ~Tree().
1819
*
1920
* Requires exactly 2 children.
2021
*/

‎src/controls/finally_node.cpp‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ FinallyNode::FinallyNode(const std::string& name, const NodeConfig& config)
1212

1313
void FinallyNode::halt()
1414
{
15-
if(!in_cleanup_ && isStatusActive(status()) && children_nodes_.size() == 2)
15+
if(!in_cleanup_ && status() == NodeStatus::RUNNING && children_nodes_.size() == 2)
1616
{
1717
haltChild(0);
18-
if(children_nodes_[1]->executeTick() == NodeStatus::RUNNING)
18+
// halt() also runs from ~Tree(), where a propagating exception terminates.
19+
try
20+
{
21+
if(children_nodes_[1]->executeTick() == NodeStatus::RUNNING)
22+
{
23+
haltChild(1);
24+
}
25+
}
26+
catch(const std::exception& ex)
1927
{
20-
haltChild(1);
28+
std::cerr << "[" << name() << "]: Finally cleanup threw during halt: " << ex.what()
29+
<< std::endl;
2130
}
2231
}
2332
in_cleanup_ = false;
@@ -71,7 +80,16 @@ NodeStatus FinallyNode::tick()
7180

7281
resetChildren();
7382
in_cleanup_ = false;
74-
return cleanup_status == NodeStatus::FAILURE ? NodeStatus::FAILURE : main_status_;
83+
if(cleanup_status == NodeStatus::FAILURE)
84+
{
85+
return NodeStatus::FAILURE;
86+
}
87+
if(main_status_ == NodeStatus::SKIPPED)
88+
{
89+
// executeTick() keeps our RUNNING status on SKIPPED, and halt() would rerun cleanup.
90+
resetStatus();
91+
}
92+
return main_status_;
7593
}
7694

7795
} // namespace BT

‎tests/gtest_finally.cpp‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,48 @@
66

77
using BT::NodeStatus;
88

9+
// RUNNING until halted; throws on its second tick if `throw_on_running` is set.
10+
class AsyncMain : public BT::StatefulActionNode
11+
{
12+
public:
13+
AsyncMain(const std::string& name, const BT::NodeConfig& config, int* halted,
14+
bool throw_on_running)
15+
: StatefulActionNode(name, config), halted_(halted), throw_(throw_on_running)
16+
{}
17+
static BT::PortsList providedPorts()
18+
{
19+
return {};
20+
}
21+
NodeStatus onStart() override
22+
{
23+
return NodeStatus::RUNNING;
24+
}
25+
NodeStatus onRunning() override
26+
{
27+
if(throw_)
28+
{
29+
throw std::runtime_error("boom");
30+
}
31+
return NodeStatus::RUNNING;
32+
}
33+
void onHalted() override
34+
{
35+
(*halted_)++;
36+
}
37+
38+
private:
39+
int* halted_;
40+
bool throw_;
41+
};
42+
943
class FinallyTest : public testing::Test
1044
{
1145
protected:
1246
BT::BehaviorTreeFactory factory;
1347
int cleanup_count = 0;
1448
int main_ticks = 0;
49+
int main_halted = 0;
50+
int cleanup_ticks = 0;
1551

1652
void SetUp() override
1753
{
@@ -25,6 +61,12 @@ class FinallyTest : public testing::Test
2561
factory.registerSimpleCondition("RunTwice", [this](BT::TreeNode&) {
2662
return ++main_ticks < 3 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
2763
});
64+
factory.registerNodeType<AsyncMain>("AsyncMain", &main_halted, false);
65+
factory.registerNodeType<AsyncMain>("AsyncThrow", &main_halted, true);
66+
// Cleanup that is RUNNING on its first tick, then SUCCESS
67+
factory.registerSimpleCondition("AsyncCleanup", [this](BT::TreeNode&) {
68+
return ++cleanup_ticks < 2 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
69+
});
2870
factory.registerSimpleCondition("AlwaysRunning", [this](BT::TreeNode&) {
2971
main_ticks++;
3072
return NodeStatus::RUNNING;
@@ -130,3 +172,78 @@ TEST_F(FinallyTest, WrongChildCount_RejectedAtLoad)
130172
</BehaviorTree></root>)"),
131173
BT::RuntimeError);
132174
}
175+
176+
TEST_F(FinallyTest, MainSkipped_CleanupRunsAndReturnsSkipped)
177+
{
178+
auto tree = factory.createTreeFromText(R"(
179+
<root BTCPP_format="4"><BehaviorTree>
180+
<Finally><AlwaysSuccess _skipIf="true"/><Cleanup/></Finally>
181+
</BehaviorTree></root>)");
182+
183+
EXPECT_EQ(tree.tickWhileRunning(), NodeStatus::SKIPPED);
184+
tree.haltTree();
185+
EXPECT_EQ(cleanup_count, 1);
186+
}
187+
188+
TEST_F(FinallyTest, AsyncCleanup_ReturnsMainStatusWhenCleanupCompletes)
189+
{
190+
EXPECT_EQ(run("<AlwaysFailure/>", "<AsyncCleanup/>"), NodeStatus::FAILURE);
191+
EXPECT_EQ(cleanup_ticks, 2);
192+
}
193+
194+
TEST_F(FinallyTest, AsyncMainThrows_MainHaltedAndCleanupRuns)
195+
{
196+
EXPECT_EQ(run("<AsyncThrow/>"), NodeStatus::FAILURE);
197+
EXPECT_EQ(main_halted, 1);
198+
EXPECT_EQ(cleanup_count, 1);
199+
}
200+
201+
TEST_F(FinallyTest, HaltWhileMainRunning_MainHalted)
202+
{
203+
auto tree = factory.createTreeFromText(R"(
204+
<root BTCPP_format="4"><BehaviorTree>
205+
<Finally><AsyncMain/><Cleanup/></Finally>
206+
</BehaviorTree></root>)");
207+
208+
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
209+
tree.haltTree();
210+
EXPECT_EQ(main_halted, 1);
211+
EXPECT_EQ(cleanup_count, 1);
212+
}
213+
214+
TEST_F(FinallyTest, HaltWhileCleanupRunning_CleanupNotRestarted)
215+
{
216+
auto tree = factory.createTreeFromText(R"(
217+
<root BTCPP_format="4"><BehaviorTree>
218+
<Finally><AlwaysSuccess/><AsyncCleanup/></Finally>
219+
</BehaviorTree></root>)");
220+
221+
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
222+
tree.haltTree();
223+
EXPECT_EQ(cleanup_ticks, 1);
224+
}
225+
226+
TEST_F(FinallyTest, HaltCleanupRunning_CleanupHalted)
227+
{
228+
auto tree = factory.createTreeFromText(R"(
229+
<root BTCPP_format="4"><BehaviorTree>
230+
<Finally><AlwaysRunning/><AsyncCleanup/></Finally>
231+
</BehaviorTree></root>)");
232+
233+
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
234+
tree.haltTree();
235+
EXPECT_EQ(cleanup_ticks, 1);
236+
EXPECT_EQ(tree.rootNode()->status(), NodeStatus::IDLE);
237+
}
238+
239+
TEST_F(FinallyTest, CleanupThrowsDuringHalt_DoesNotPropagate)
240+
{
241+
auto tree = factory.createTreeFromText(R"(
242+
<root BTCPP_format="4"><BehaviorTree>
243+
<Finally><AlwaysRunning/><Throw/></Finally>
244+
</BehaviorTree></root>)");
245+
246+
EXPECT_EQ(tree.tickOnce(), NodeStatus::RUNNING);
247+
EXPECT_NO_THROW(tree.haltTree());
248+
EXPECT_EQ(tree.rootNode()->status(), NodeStatus::IDLE);
249+
}

0 commit comments

Comments
 (0)