fix(fsm): refuse all transitions in the Error state

- The Error state is a terminate state: the state machine ignores all
  events in it and calls no action, so ChangeState() returned the
  result of the previous transition (ErrorFound: true)
- Return false for each transition in the Error state, except for
  ErrorFound: the device is already in the Error state, so callers
  such as ChangeStateOrThrow(ErrorFound) in Device::RunWrapper() or
  in user code do not throw
- Enable StateMachine.RefusesTransitionsInError
- Update the documentation pages that described the defect, and
  the source line links into the changed files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-04 21:12:48 +00:00
committed by Dennis Klein
parent 8de8505f8c
commit 442b203d0d
7 changed files with 17 additions and 17 deletions

View File

@@ -176,10 +176,8 @@ hook.
In `ERROR`, the controller of the control plugin stops. It requests no more transitions and no
shutdown sequence. Thus the device does not call `ResetTask()` and `Reset()` after an error.
:::caution[Known defect]
In `ERROR`, `ChangeState()` can return `true`, but the state does not change. See the disabled test
`StateMachine.RefusesTransitionsInError` in `test/state_machine/`.
:::
In `ERROR`, the state does not change. `ChangeState()` returns `false` for all transitions except
`ErrorFound`. The test `StateMachine.RefusesTransitionsInError` in `test/state_machine/` checks this.
For the handling of errors, see [Handle errors and timeouts](/guides/errors-timeouts/).

View File

@@ -85,7 +85,7 @@ Some rules follow from this table:
| [`ChannelResource`](/api/fair/mq/ChannelResource/) | No. Its map of the allocated messages has no lock. See [Use std::pmr memory resources](/guides/pmr/). | [MemoryResources.h](/source/fairmq/MemoryResources.h/#L54-L98) |
| The property functions of [`ProgOptions`](/api/fair/mq/ProgOptions/), for example `GetProperty<T>()` and `SetProperty<T>()` | Yes. Each function locks a mutex. [`GetVarMap()`](/api/fair/mq/ProgOptions/#GetVarMap) returns a reference, and the lock does not protect it. | [ProgOptions.h](/source/fairmq/ProgOptions.h/#L61-L87) |
| [`ChangeState()`](/api/fair/mq/Device/#ChangeState), [`NewStatePending()`](/api/fair/mq/Device/#NewStatePending), [`WaitFor()`](/api/fair/mq/Device/#WaitFor), [`GetCurrentState()`](/api/fair/mq/Device/#GetCurrentState) | Yes. The state machine uses a mutex and atomic values. | [StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L175-L182), [StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L257-L260) |
| Subscribe and unsubscribe for state changes | Yes. A mutex protects the subscriptions. | [StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L312-L329), test `PluginServices.SubscriptionThreadSafety` |
| Subscribe and unsubscribe for state changes | Yes. A mutex protects the subscriptions. | [StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L318-L335), test `PluginServices.SubscriptionThreadSafety` |
| [`WaitForState()`](/api/fair/mq/Device/#WaitForState), [`WaitForNextState()`](/api/fair/mq/Device/#WaitForNextState) | Call them from one thread only. The queue of states gives each state to one caller. | [StateQueue.h](/source/fairmq/StateQueue.h/#L31-L35) |
| [`Interrupt()`](/api/fair/mq/TransportFactory/#Interrupt), [`Resume()`](/api/fair/mq/TransportFactory/#Resume) of a transport | Yes. They set an atomic flag. | [zeromq/Context.h](/source/fairmq/zeromq/Context.h/#L149-L150), [shmem/Manager.h](/source/fairmq/shmem/Manager.h/#L283-L284), test `InterruptTransfer.Receive` |
| The byte and message counters of a channel, for example [`GetBytesTx()`](/api/fair/mq/Channel/#GetBytesTx) | Yes. The sockets keep them in atomic values. The rate logger thread reads them. | [zeromq/Socket.h](/source/fairmq/zeromq/Socket.h/#L399-L402), [shmem/Socket.h](/source/fairmq/shmem/Socket.h/#L459-L462) |

View File

@@ -92,7 +92,7 @@ The macro `REGISTER_FAIRMQ_PLUGIN` registers the class under the name `mycontrol
### The order of the callbacks
`RunStateMachine()` first calls the state change callbacks with `IDLE`, so the plugin requests `InitDevice`. In each later state, the state machine calls the state change callbacks before the state handler of the device. See [`StateMachine::ProcessWork()`](/source/fairmq/StateMachine.cxx/#L396-L414) and the [loop of the state machine](/source/fairmq/StateMachine.cxx/#L191-L219).
`RunStateMachine()` first calls the state change callbacks with `IDLE`, so the plugin requests `InitDevice`. In each later state, the state machine calls the state change callbacks before the state handler of the device. See [`StateMachine::ProcessWork()`](/source/fairmq/StateMachine.cxx/#L402-L420) and the [loop of the state machine](/source/fairmq/StateMachine.cxx/#L191-L219).
The state machine calls the callbacks in the order of the number at the start of their key. A key without a number, for example `example` or `mycontroller`, has the number 0. Callbacks with the same number run in the order of subscription. The hook in `main.cxx` subscribes before the plugin manager creates the plugin. Thus, in `RUNNING`, the callback of `main.cxx` runs first:

View File

@@ -103,9 +103,8 @@ matrix: from all states, `ErrorFound` leads to `ERROR`. The source is the `trans
- Each new transition interrupts the transports. The transports resume at the start of the next
state.
- In `EXITING` and `ERROR`, the state machine has stopped, and the state does not change. In
`EXITING`, `ChangeState()` returns `false` for all transitions except `ErrorFound`. In `ERROR`,
`ChangeState()` can return `true` (known defect, see the disabled test
`StateMachine.RefusesTransitionsInError`).
both states, `ChangeState()` returns `false` for all transitions except `ErrorFound` (see the
test `StateMachine.RefusesTransitionsInError`).
## Keys of the interactive controller

View File

@@ -275,7 +275,7 @@ or a data callback throws, the destructor requests the transition `ERROR FOUND`.
<Snippet file="fairmq/Device.cxx" start="tools::CallOnDestruction cod(" />
`StateMachine::ProcessWork()` uses the same pattern to go to `ERROR`
([StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L400-L413)). A destructor is `noexcept` by
([StateMachine.cxx](/source/fairmq/StateMachine.cxx/#L406-L419)). A destructor is `noexcept` by
default in C++. Thus a callback that throws ends the program with `std::terminate()`. No test of
`test/tools/` covers this class. The tests of the `ERROR` state in `test/device/` show its effect.

View File

@@ -259,6 +259,12 @@ try {
auto fsm = static_pointer_cast<FairMQFSM>(fFsm);
lock_guard<mutex> lock(fsm->fStateMtx);
if (!static_cast<bool>(fsm->fNewStatePending) || transition == Transition::ErrorFound) {
// The Error state is a terminate state: the state machine ignores all events in it and
// calls no action, so fLastTransitionResult would keep the result of ErrorFound.
// ErrorFound has nothing left to do; refuse all other transitions.
if (fsm->is_flag_active<boost::msm::TerminateFlag>()) {
return transition == Transition::ErrorFound;
}
switch (transition) {
case Transition::Auto:
fsm->process_event(AUTO_E());

View File

@@ -230,17 +230,14 @@ TEST(StateMachine, ErrorFoundFromEachState)
}
}
// Known defect: Error is a terminate state, so the state machine ignores all
// events in it. ChangeState() then returns the result of the previous
// transition (ErrorFound: true) instead of false. Device::RunWrapper() relies
// on ChangeStateOrThrow(ErrorFound) not throwing in Error, so a fix must
// change both. Enable this test with the fix.
TEST(StateMachine, DISABLED_RefusesTransitionsInError)
// Error is a terminate state: the state machine refuses all transitions in it.
// ErrorFound succeeds, because the device is already in the Error state.
TEST(StateMachine, RefusesTransitionsInError)
{
Machine m;
ASSERT_TRUE(m.Change(Transition::ErrorFound, State::Error));
for (auto const transition : AllTransitions()) {
EXPECT_FALSE(m.fFsm.ChangeState(transition)) << transition << " in Error";
EXPECT_EQ(m.fFsm.ChangeState(transition), transition == Transition::ErrorFound) << transition << " in Error";
}
}