|
| 1 | +#include "behaviortree_cpp/controls/finally_node.h" |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | +#include <exception> |
| 5 | +#include <iostream> |
| 6 | +#include <string_view> |
| 7 | +#include <thread> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +namespace BT |
| 11 | +{ |
| 12 | +namespace |
| 13 | +{ |
| 14 | +constexpr std::chrono::milliseconds kHaltTickPeriod{ 10 }; |
| 15 | + |
| 16 | +void printException(std::string_view node_name, const char* context, |
| 17 | + const std::exception_ptr& exception) |
| 18 | +{ |
| 19 | + std::cerr << "[" << node_name << "]: " << context << ": "; |
| 20 | + try |
| 21 | + { |
| 22 | + std::rethrow_exception(exception); |
| 23 | + } |
| 24 | + catch(const std::exception& ex) |
| 25 | + { |
| 26 | + std::cerr << ex.what() << std::endl; |
| 27 | + } |
| 28 | + catch(...) |
| 29 | + { |
| 30 | + std::cerr << "non-std exception" << std::endl; |
| 31 | + } |
| 32 | +} |
| 33 | +} // namespace |
| 34 | + |
| 35 | +FinallyNode::FinallyNode(const std::string& name, const NodeConfig& config) |
| 36 | + : ControlNode::ControlNode(name, config) |
| 37 | +{ |
| 38 | + setRegistrationID("Finally"); |
| 39 | +} |
| 40 | + |
| 41 | +void FinallyNode::halt() |
| 42 | +{ |
| 43 | + // halt() also runs from ~Tree(), where a propagating exception terminates, so nothing here throws. |
| 44 | + if(status() == NodeStatus::RUNNING && children_nodes_.size() == 2) |
| 45 | + { |
| 46 | + if(!in_cleanup_) |
| 47 | + { |
| 48 | + haltChildNoThrow(0); |
| 49 | + } |
| 50 | + finishCleanupDuringHalt(); |
| 51 | + } |
| 52 | + for(size_t i = 0; i < children_nodes_.size(); i++) |
| 53 | + { |
| 54 | + haltChildNoThrow(i); |
| 55 | + } |
| 56 | + in_cleanup_ = false; |
| 57 | + main_status_ = NodeStatus::IDLE; |
| 58 | + main_exception_ = nullptr; |
| 59 | + resetStatus(); |
| 60 | +} |
| 61 | + |
| 62 | +void FinallyNode::finishCleanupDuringHalt() |
| 63 | +{ |
| 64 | + unsigned timeout_msec = kDefaultHaltTimeoutMsec; |
| 65 | + if(auto input = getInput<unsigned>("halt_timeout_msec")) |
| 66 | + { |
| 67 | + timeout_msec = input.value(); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + std::cerr << "[" << name() << "]: cannot read halt_timeout_msec (" << input.error() |
| 72 | + << "), using " << kDefaultHaltTimeoutMsec << " ms" << std::endl; |
| 73 | + } |
| 74 | + const auto deadline = |
| 75 | + std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_msec); |
| 76 | + try |
| 77 | + { |
| 78 | + // Cleanup is commonly asynchronous, so keep ticking it until it finishes rather than |
| 79 | + // halting it after its first tick. |
| 80 | + NodeStatus cleanup_status = children_nodes_[1]->executeTick(); |
| 81 | + while(cleanup_status == NodeStatus::RUNNING) |
| 82 | + { |
| 83 | + // Start no tick after the deadline, so a tick that blocks cannot extend the wait. |
| 84 | + const auto next_tick = std::chrono::steady_clock::now() + kHaltTickPeriod; |
| 85 | + if(next_tick > deadline) |
| 86 | + { |
| 87 | + std::cerr << "[" << name() |
| 88 | + << "]: cleanup did not finish within halt_timeout_msec (" |
| 89 | + << timeout_msec << " ms), halting it unfinished" << std::endl; |
| 90 | + return; |
| 91 | + } |
| 92 | + std::this_thread::sleep_until(next_tick); |
| 93 | + cleanup_status = children_nodes_[1]->executeTick(); |
| 94 | + } |
| 95 | + if(cleanup_status == NodeStatus::FAILURE) |
| 96 | + { |
| 97 | + std::cerr << "[" << name() << "]: cleanup returned FAILURE during halt" |
| 98 | + << std::endl; |
| 99 | + } |
| 100 | + } |
| 101 | + catch(...) |
| 102 | + { |
| 103 | + printException(name(), "cleanup threw during halt", std::current_exception()); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void FinallyNode::haltChildNoThrow(size_t i) |
| 108 | +{ |
| 109 | + try |
| 110 | + { |
| 111 | + haltChild(i); |
| 112 | + } |
| 113 | + catch(...) |
| 114 | + { |
| 115 | + printException(name(), "a child threw while being halted", std::current_exception()); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +NodeStatus FinallyNode::tick() |
| 120 | +{ |
| 121 | + if(children_nodes_.size() != 2) |
| 122 | + { |
| 123 | + throw LogicError("[", name(), "]: Finally requires exactly 2 children"); |
| 124 | + } |
| 125 | + |
| 126 | + if(!isStatusActive(status())) |
| 127 | + { |
| 128 | + in_cleanup_ = false; |
| 129 | + main_exception_ = nullptr; |
| 130 | + } |
| 131 | + |
| 132 | + setStatus(NodeStatus::RUNNING); |
| 133 | + |
| 134 | + if(!in_cleanup_) |
| 135 | + { |
| 136 | + try |
| 137 | + { |
| 138 | + main_status_ = children_nodes_[0]->executeTick(); |
| 139 | + } |
| 140 | + catch(...) |
| 141 | + { |
| 142 | + main_exception_ = std::current_exception(); |
| 143 | + haltChildNoThrow(0); |
| 144 | + main_status_ = NodeStatus::FAILURE; |
| 145 | + } |
| 146 | + |
| 147 | + if(main_status_ == NodeStatus::RUNNING) |
| 148 | + { |
| 149 | + return NodeStatus::RUNNING; |
| 150 | + } |
| 151 | + if(main_status_ == NodeStatus::IDLE) |
| 152 | + { |
| 153 | + throw LogicError("[", name(), "]: A child should not return IDLE"); |
| 154 | + } |
| 155 | + in_cleanup_ = true; |
| 156 | + } |
| 157 | + |
| 158 | + NodeStatus cleanup_status = NodeStatus::IDLE; |
| 159 | + try |
| 160 | + { |
| 161 | + cleanup_status = children_nodes_[1]->executeTick(); |
| 162 | + } |
| 163 | + catch(...) |
| 164 | + { |
| 165 | + // As in a finally block, the exception replaces main's outcome and ends this run, so a later |
| 166 | + // halt, such as the one ~Tree() runs, does not retry cleanup. |
| 167 | + // Main finished but was not reset yet. haltChildNoThrow() resets both without throwing, so the |
| 168 | + // cleanup exception is the one that propagates. |
| 169 | + for(size_t i = 0; i < children_nodes_.size(); i++) |
| 170 | + { |
| 171 | + haltChildNoThrow(i); |
| 172 | + } |
| 173 | + in_cleanup_ = false; |
| 174 | + main_exception_ = nullptr; |
| 175 | + resetStatus(); |
| 176 | + throw; |
| 177 | + } |
| 178 | + if(cleanup_status == NodeStatus::RUNNING) |
| 179 | + { |
| 180 | + return NodeStatus::RUNNING; |
| 181 | + } |
| 182 | + |
| 183 | + resetChildren(); |
| 184 | + in_cleanup_ = false; |
| 185 | + if(main_exception_) |
| 186 | + { |
| 187 | + // executeTick() keeps our RUNNING status when tick() throws, and halt() would rerun cleanup. |
| 188 | + resetStatus(); |
| 189 | + std::rethrow_exception(std::exchange(main_exception_, nullptr)); |
| 190 | + } |
| 191 | + if(cleanup_status == NodeStatus::FAILURE) |
| 192 | + { |
| 193 | + return NodeStatus::FAILURE; |
| 194 | + } |
| 195 | + if(main_status_ == NodeStatus::SKIPPED) |
| 196 | + { |
| 197 | + // executeTick() keeps our RUNNING status on SKIPPED, and halt() would rerun cleanup. |
| 198 | + resetStatus(); |
| 199 | + } |
| 200 | + return main_status_; |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace BT |
0 commit comments