Skip to content

Commit a80a228

Browse files
facontidavideclaude
andcommitted
Remove thread_local tick stack that caused heap corruption
The thread_local std::vector in the exception backtrace feature was causing heap corruption when all tests ran in a single process, and on Windows DLLs. Simplified NodeExecutionError to store only the failing node's info instead of a full tick backtrace. The exception still wraps the original message with the node name, path, and registration name. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d16139a commit a80a228

3 files changed

Lines changed: 13 additions & 75 deletions

File tree

‎include/behaviortree_cpp/exceptions.h‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,20 @@ struct TickBacktraceEntry
7777
};
7878

7979
/// Exception thrown when a node's tick() method throws an exception.
80-
/// Contains the originating node and full tick backtrace showing the path through the tree.
80+
/// Contains information about the node where the exception originated.
8181
class NodeExecutionError : public RuntimeError
8282
{
8383
public:
84-
NodeExecutionError(std::vector<TickBacktraceEntry> backtrace,
85-
const std::string& original_message)
86-
: RuntimeError(formatMessage(backtrace, original_message))
87-
, backtrace_(std::move(backtrace))
84+
NodeExecutionError(TickBacktraceEntry failed_node, const std::string& original_message)
85+
: RuntimeError(formatMessage(failed_node, original_message))
86+
, failed_node_(std::move(failed_node))
8887
, original_message_(original_message)
8988
{}
9089

91-
/// The node that threw the exception (innermost in the backtrace)
90+
/// The node that threw the exception
9291
[[nodiscard]] const TickBacktraceEntry& failedNode() const
9392
{
94-
return backtrace_.back();
95-
}
96-
97-
/// Full tick backtrace from root to failing node
98-
[[nodiscard]] const std::vector<TickBacktraceEntry>& backtrace() const
99-
{
100-
return backtrace_;
93+
return failed_node_;
10194
}
10295

10396
[[nodiscard]] const std::string& originalMessage() const
@@ -106,22 +99,14 @@ class NodeExecutionError : public RuntimeError
10699
}
107100

108101
private:
109-
std::vector<TickBacktraceEntry> backtrace_;
102+
TickBacktraceEntry failed_node_;
110103
std::string original_message_;
111104

112-
static std::string formatMessage(const std::vector<TickBacktraceEntry>& bt,
105+
static std::string formatMessage(const TickBacktraceEntry& node,
113106
const std::string& original_msg)
114107
{
115-
std::string msg =
116-
StrCat("Exception in node '", bt.back().node_path, "': ", original_msg);
117-
msg += "\nTick backtrace:";
118-
for(size_t i = 0; i < bt.size(); ++i)
119-
{
120-
const bool is_last = (i == bt.size() - 1);
121-
msg += StrCat("\n ", is_last ? "-> " : " ", bt[i].node_path, " (",
122-
bt[i].registration_name, ")");
123-
}
124-
return msg;
108+
return StrCat("Exception in node '", node.node_path, "' [", node.registration_name,
109+
"]: ", original_msg);
125110
}
126111
};
127112

‎src/tree_node.cpp‎

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,6 @@
2020

2121
namespace BT
2222
{
23-
namespace
24-
{
25-
// Thread-local stack tracking the current tick hierarchy.
26-
// Used to build a backtrace when an exception is thrown during tick().
27-
thread_local std::vector<const TreeNode*> tick_stack_;
28-
29-
// RAII guard to push/pop nodes from the tick stack
30-
class TickStackGuard
31-
{
32-
public:
33-
explicit TickStackGuard(const TreeNode* node)
34-
{
35-
tick_stack_.push_back(node);
36-
}
37-
~TickStackGuard()
38-
{
39-
tick_stack_.pop_back();
40-
}
41-
TickStackGuard(const TickStackGuard&) = delete;
42-
TickStackGuard& operator=(const TickStackGuard&) = delete;
43-
TickStackGuard(TickStackGuard&&) = delete;
44-
TickStackGuard& operator=(TickStackGuard&&) = delete;
45-
46-
// Build a backtrace from the current tick stack
47-
static std::vector<TickBacktraceEntry> buildBacktrace()
48-
{
49-
std::vector<TickBacktraceEntry> backtrace;
50-
backtrace.reserve(tick_stack_.size());
51-
for(const auto* node : tick_stack_)
52-
{
53-
backtrace.push_back({ node->name(), node->fullPath(), node->registrationName() });
54-
}
55-
return backtrace;
56-
}
57-
};
58-
} // namespace
5923

6024
struct TreeNode::PImpl
6125
{
@@ -106,9 +70,6 @@ TreeNode::~TreeNode() = default;
10670

10771
NodeStatus TreeNode::executeTick()
10872
{
109-
// Track this node in the tick stack for exception backtrace
110-
TickStackGuard stack_guard(this);
111-
11273
auto new_status = _p->status;
11374
PreTickCallback pre_tick;
11475
PostTickCallback post_tick;
@@ -160,8 +121,8 @@ NodeStatus TreeNode::executeTick()
160121
}
161122
catch(const std::exception& ex)
162123
{
163-
// Wrap the exception with node context and backtrace
164-
throw NodeExecutionError(TickStackGuard::buildBacktrace(), ex.what());
124+
// Wrap the exception with this node's context
125+
throw NodeExecutionError({ name(), fullPath(), registrationName() }, ex.what());
165126
}
166127
std::atomic_thread_fence(std::memory_order_seq_cst);
167128
const auto t2 = steady_clock::now();

‎tests/gtest_exception_tracking.cpp‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ TEST(ExceptionTracking, BasicExceptionCapture)
8787
EXPECT_EQ(e.failedNode().node_name, "thrower");
8888
EXPECT_EQ(e.failedNode().registration_name, "ThrowingAction");
8989
EXPECT_EQ(e.originalMessage(), "Test exception from ThrowingAction");
90-
91-
// Verify backtrace has the node
92-
ASSERT_GE(e.backtrace().size(), 1u);
93-
EXPECT_EQ(e.backtrace().back().node_name, "thrower");
9490
}
9591
}
9692

@@ -127,13 +123,9 @@ TEST(ExceptionTracking, NestedExceptionBacktrace)
127123
// Verify the failed node is the innermost throwing node
128124
EXPECT_EQ(e.failedNode().node_name, "nested_thrower");
129125

130-
// Verify backtrace shows the full path (at least 3 nodes: Sequence, Retry, Thrower)
131-
ASSERT_GE(e.backtrace().size(), 3u);
132-
133-
// Check the what() message contains backtrace info
126+
// Check the what() message contains the failing node
134127
std::string what_msg = e.what();
135128
EXPECT_NE(what_msg.find("nested_thrower"), std::string::npos);
136-
EXPECT_NE(what_msg.find("Tick backtrace"), std::string::npos);
137129
}
138130
}
139131

0 commit comments

Comments
 (0)