mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
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:
@@ -176,10 +176,8 @@ hook.
|
|||||||
In `ERROR`, the controller of the control plugin stops. It requests no more transitions and no
|
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.
|
shutdown sequence. Thus the device does not call `ResetTask()` and `Reset()` after an error.
|
||||||
|
|
||||||
:::caution[Known defect]
|
In `ERROR`, the state does not change. `ChangeState()` returns `false` for all transitions except
|
||||||
In `ERROR`, `ChangeState()` can return `true`, but the state does not change. See the disabled test
|
`ErrorFound`. The test `StateMachine.RefusesTransitionsInError` in `test/state_machine/` checks this.
|
||||||
`StateMachine.RefusesTransitionsInError` in `test/state_machine/`.
|
|
||||||
:::
|
|
||||||
|
|
||||||
For the handling of errors, see [Handle errors and timeouts](/guides/errors-timeouts/).
|
For the handling of errors, see [Handle errors and timeouts](/guides/errors-timeouts/).
|
||||||
|
|
||||||
|
|||||||
@@ -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) |
|
| [`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) |
|
| 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) |
|
| [`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) |
|
| [`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` |
|
| [`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) |
|
| 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) |
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ The macro `REGISTER_FAIRMQ_PLUGIN` registers the class under the name `mycontrol
|
|||||||
|
|
||||||
### The order of the callbacks
|
### 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:
|
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:
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
- Each new transition interrupts the transports. The transports resume at the start of the next
|
||||||
state.
|
state.
|
||||||
- In `EXITING` and `ERROR`, the state machine has stopped, and the state does not change. In
|
- 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`,
|
both states, `ChangeState()` returns `false` for all transitions except `ErrorFound` (see the
|
||||||
`ChangeState()` can return `true` (known defect, see the disabled test
|
test `StateMachine.RefusesTransitionsInError`).
|
||||||
`StateMachine.RefusesTransitionsInError`).
|
|
||||||
|
|
||||||
## Keys of the interactive controller
|
## Keys of the interactive controller
|
||||||
|
|
||||||
|
|||||||
@@ -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(" />
|
<Snippet file="fairmq/Device.cxx" start="tools::CallOnDestruction cod(" />
|
||||||
|
|
||||||
`StateMachine::ProcessWork()` uses the same pattern to go to `ERROR`
|
`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
|
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.
|
`test/tools/` covers this class. The tests of the `ERROR` state in `test/device/` show its effect.
|
||||||
|
|
||||||
|
|||||||
@@ -259,6 +259,12 @@ try {
|
|||||||
auto fsm = static_pointer_cast<FairMQFSM>(fFsm);
|
auto fsm = static_pointer_cast<FairMQFSM>(fFsm);
|
||||||
lock_guard<mutex> lock(fsm->fStateMtx);
|
lock_guard<mutex> lock(fsm->fStateMtx);
|
||||||
if (!static_cast<bool>(fsm->fNewStatePending) || transition == Transition::ErrorFound) {
|
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) {
|
switch (transition) {
|
||||||
case Transition::Auto:
|
case Transition::Auto:
|
||||||
fsm->process_event(AUTO_E());
|
fsm->process_event(AUTO_E());
|
||||||
|
|||||||
@@ -230,17 +230,14 @@ TEST(StateMachine, ErrorFoundFromEachState)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Known defect: Error is a terminate state, so the state machine ignores all
|
// Error is a terminate state: the state machine refuses all transitions in it.
|
||||||
// events in it. ChangeState() then returns the result of the previous
|
// ErrorFound succeeds, because the device is already in the Error state.
|
||||||
// transition (ErrorFound: true) instead of false. Device::RunWrapper() relies
|
TEST(StateMachine, RefusesTransitionsInError)
|
||||||
// on ChangeStateOrThrow(ErrorFound) not throwing in Error, so a fix must
|
|
||||||
// change both. Enable this test with the fix.
|
|
||||||
TEST(StateMachine, DISABLED_RefusesTransitionsInError)
|
|
||||||
{
|
{
|
||||||
Machine m;
|
Machine m;
|
||||||
ASSERT_TRUE(m.Change(Transition::ErrorFound, State::Error));
|
ASSERT_TRUE(m.Change(Transition::ErrorFound, State::Error));
|
||||||
for (auto const transition : AllTransitions()) {
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user