From 442b203d0dc2e594e8f34f73f1d3d2601bb87357 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 4 Nov 2026 21:12:48 +0000 Subject: [PATCH] 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 --- docs/src/content/docs/concepts/states.mdx | 6 ++---- docs/src/content/docs/concepts/threads.mdx | 2 +- docs/src/content/docs/examples/custom-controller.mdx | 2 +- docs/src/content/docs/reference/states.mdx | 5 ++--- docs/src/content/docs/reference/tools.mdx | 2 +- fairmq/StateMachine.cxx | 6 ++++++ test/state_machine/_state_machine.cxx | 11 ++++------- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/src/content/docs/concepts/states.mdx b/docs/src/content/docs/concepts/states.mdx index 4b11f38e..ae9cad12 100644 --- a/docs/src/content/docs/concepts/states.mdx +++ b/docs/src/content/docs/concepts/states.mdx @@ -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/). diff --git a/docs/src/content/docs/concepts/threads.mdx b/docs/src/content/docs/concepts/threads.mdx index ffddb03e..e431ba3e 100644 --- a/docs/src/content/docs/concepts/threads.mdx +++ b/docs/src/content/docs/concepts/threads.mdx @@ -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()` and `SetProperty()` | 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) | diff --git a/docs/src/content/docs/examples/custom-controller.mdx b/docs/src/content/docs/examples/custom-controller.mdx index ff972c92..05d4f9ef 100644 --- a/docs/src/content/docs/examples/custom-controller.mdx +++ b/docs/src/content/docs/examples/custom-controller.mdx @@ -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: diff --git a/docs/src/content/docs/reference/states.mdx b/docs/src/content/docs/reference/states.mdx index 05486751..41b2e434 100644 --- a/docs/src/content/docs/reference/states.mdx +++ b/docs/src/content/docs/reference/states.mdx @@ -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 diff --git a/docs/src/content/docs/reference/tools.mdx b/docs/src/content/docs/reference/tools.mdx index 101cbadb..08899cc4 100644 --- a/docs/src/content/docs/reference/tools.mdx +++ b/docs/src/content/docs/reference/tools.mdx @@ -275,7 +275,7 @@ or a data callback throws, the destructor requests the transition `ERROR FOUND`. `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. diff --git a/fairmq/StateMachine.cxx b/fairmq/StateMachine.cxx index 41d6c2d8..45254021 100644 --- a/fairmq/StateMachine.cxx +++ b/fairmq/StateMachine.cxx @@ -259,6 +259,12 @@ try { auto fsm = static_pointer_cast(fFsm); lock_guard lock(fsm->fStateMtx); if (!static_cast(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()) { + return transition == Transition::ErrorFound; + } switch (transition) { case Transition::Auto: fsm->process_event(AUTO_E()); diff --git a/test/state_machine/_state_machine.cxx b/test/state_machine/_state_machine.cxx index f6295065..83a6237b 100644 --- a/test/state_machine/_state_machine.cxx +++ b/test/state_machine/_state_machine.cxx @@ -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"; } }