Skip to content

Commit

Permalink
chore(state-machine): Panic in the case of non-handled non-deferred e…
Browse files Browse the repository at this point in the history
…vents

This adds a sanity check to verify that there are no non-deferred events left in
the event queue after the run_queue has been populated.

If there was, then we would have undefined behaviour in the state-machine, and
as such the best thing we can do in this instance is to panic. This is a serious
logic error in our code, and as such such cause a hard-fault.

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>
  • Loading branch information
oleorhagen committed Dec 4, 2023
1 parent d21b262 commit 9cad8e7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/common/state_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,27 @@ class StateMachineRunner : virtual public EventPoster<EventType> {
return run_queue;
}


void FailIfNonDeferredEventsLeftInEventQueue(queue<EventType> queue_copy) {
// Check if there are any non-deferred events in the queue - then fail if
while (not queue_copy.empty()) {
EventType event = queue_copy.front();
queue_copy.pop();
for (const auto machine : machines_) {
if (machine->deferred_events_.find(event) == machine->deferred_events_.end()) {
log::Fatal(
"The state machine has an unprocessed non-deferred event in the queue. This is a programming error!");
}
}
}
}


void RunOne() {
vector<State<ContextType, EventType> *> run_queue = FillRunQueueFrom(event_queue_);

FailIfNonDeferredEventsLeftInEventQueue(event_queue_);

if (!run_queue.empty()) {
for (auto &state : run_queue) {
log::Trace("Entering state " + common::BestAvailableTypeName(*state));
Expand Down

0 comments on commit 9cad8e7

Please sign in to comment.