Skip to content

Commit b3171a5

Browse files
schornakjJafarAbdi
andauthored
Wait for the thread to finish before deleting zmq (BehaviorTree#440)
Co-authored-by: JafarAbdi <[email protected]>
1 parent c2d1e4b commit b3171a5

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/loggers/bt_zmq_publisher.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ PublisherZMQ::~PublisherZMQ()
9292
}
9393
flush();
9494
zmq_->context.shutdown();
95+
if (send_future_.valid())
96+
{
97+
send_future_.wait();
98+
}
9599
delete zmq_;
96100
ref_count = false;
97101
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(BT_TESTS
1919
gtest_subtree.cpp
2020
gtest_switch.cpp
2121
gtest_wakeup.cpp
22+
gtest_logger_zmq.cpp
2223
)
2324

2425
if( BT_COROUTINES )

tests/gtest_logger_zmq.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "behaviortree_cpp_v3/bt_factory.h"
4+
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"
5+
6+
TEST(LoggerZMQ, ZMQLoggerDeletesCleanlyAfterTickingTree)
7+
{
8+
// GIVEN we create a behavior tree through the BT factory and attach a ZMQ publisher to it
9+
static constexpr auto XML = R"(
10+
<root>
11+
<BehaviorTree>
12+
<SetBlackboard output_key="arg1" value="1" />
13+
</BehaviorTree>
14+
</root>
15+
)";
16+
BT::BehaviorTreeFactory factory;
17+
auto tree = factory.createTreeFromText(XML);
18+
19+
{
20+
BT::PublisherZMQ zmq_logger{tree, 1};
21+
tree.tickRoot();
22+
// WHEN zmq_logger goes out of scope
23+
}
24+
25+
// THEN zmq_logger is destroyed cleanly without segfaulting
26+
SUCCEED();
27+
}
28+
29+
TEST(LoggerZMQ, ZMQLoggerDeletesCleanlyAfterNotTickingTree)
30+
{
31+
// GIVEN we create a behavior tree through the BT factory and attach a ZMQ publisher to it
32+
static constexpr auto XML = R"(
33+
<root>
34+
<BehaviorTree>
35+
<SetBlackboard output_key="arg1" value="1" />
36+
</BehaviorTree>
37+
</root>
38+
)";
39+
BT::BehaviorTreeFactory factory;
40+
auto tree = factory.createTreeFromText(XML);
41+
42+
{
43+
BT::PublisherZMQ zmq_logger{tree, 1};
44+
// GIVEN we haven't even ticked the tree, so ZMQ hasn't published any state change messages
45+
// (meaning no send is pending and send_future_ has not been set)
46+
// WHEN zmq_logger goes out of scope
47+
}
48+
49+
// THEN zmq_logger is destroyed cleanly without segfaulting
50+
SUCCEED();
51+
}

0 commit comments

Comments
 (0)