mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
fix(control): handle all keys of one input in interactive mode
- The REPL polled the stdin fd, then read one key with std::cin; the other keys of the same read stayed in the std::cin buffer, and poll() did not report them again - Read one key with read(2), so poll() reports each key that is not handled yet; skip whitespace, as operator>> did - Stop reading at end of input: before, a closed stdin (e.g. < /dev/null) made the loop print the help without a pause - Enable ControlPlugin.SeveralKeysInOneInput - 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:
@@ -215,16 +215,14 @@ In the modes `dynamic`, `interactive` and `external`, a key works without Enter,
|
|||||||
A key that the current state does not accept changes nothing. The state machine logs
|
A key that the current state does not accept changes nothing. The state machine logs
|
||||||
`No transition from state … on transition …` at the severity `state`.
|
`No transition from state … on transition …` at the severity `state`.
|
||||||
|
|
||||||
:::caution[One key at a time]
|
The plugin reads the keys one at a time. When several keys arrive together, for example `cq` from a pipe, the plugin
|
||||||
The plugin reads one key each time new input arrives. When several keys arrive together, for example `cq` from a pipe,
|
handles all of them in their order. It ignores spaces and line ends. At the end of the input, it stops reading keys.
|
||||||
the plugin handles only the first one. It reads the next key only after more input arrives. See the disabled test
|
The test `ControlPlugin.SeveralKeysInOneInput` in `test/device/_runner.cxx` checks this.
|
||||||
`ControlPlugin.DISABLED_SeveralKeysInOneInput` in `test/device/_runner.cxx`.
|
|
||||||
:::
|
|
||||||
|
|
||||||
### Signals
|
### Signals
|
||||||
|
|
||||||
With `--catch-signals` greater than 0, the control plugin handles `SIGINT` and `SIGTERM`
|
With `--catch-signals` greater than 0, the control plugin handles `SIGINT` and `SIGTERM`
|
||||||
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L439-L469)):
|
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L450-L480)):
|
||||||
|
|
||||||
1. The first signal starts a graceful shutdown. The plugin logs `Received device shutdown request (signal …)` and
|
1. The first signal starts a graceful shutdown. The plugin logs `Received device shutdown request (signal …)` and
|
||||||
`Waiting for graceful device shutdown. Hit Ctrl-C again to abort immediately.`
|
`Waiting for graceful device shutdown. Hit Ctrl-C again to abort immediately.`
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ A device that starts with `fairmq/runDevice.h` can have these threads:
|
|||||||
| Thread | Started by | When it runs | What it does |
|
| Thread | Started by | When it runs | What it does |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Main thread | The process | Always | Runs `main()` and [`DeviceRunner::Run()`](/api/fair/mq/DeviceRunner/#Run). It creates the plugins and then calls [`RunStateMachine()`](/api/fair/mq/Device/#RunStateMachine). This thread is the thread of the state machine. |
|
| Main thread | The process | Always | Runs `main()` and [`DeviceRunner::Run()`](/api/fair/mq/DeviceRunner/#Run). It creates the plugins and then calls [`RunStateMachine()`](/api/fair/mq/Device/#RunStateMachine). This thread is the thread of the state machine. |
|
||||||
| Controller thread | The control plugin | With each value of `--control` except `none` | Requests the transitions. In the mode `dynamic` (default), it also reads the keys of the terminal ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L67-L92)). |
|
| Controller thread | The control plugin | With each value of `--control` except `none` | Requests the transitions. In the mode `dynamic` (default), it also reads the keys of the terminal ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L69-L94)). |
|
||||||
| Signal handler thread | The control plugin | With `--catch-signals 1` (default) | Checks every 100 ms for `SIGINT` or `SIGTERM`. After the first signal, it stops the controller thread and requests the transitions of the shutdown sequence ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L439-L469)). |
|
| Signal handler thread | The control plugin | With `--catch-signals 1` (default) | Checks every 100 ms for `SIGINT` or `SIGTERM`. After the first signal, it stops the controller thread and requests the transitions of the shutdown sequence ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L450-L480)). |
|
||||||
| Rate logger thread | The device | In `RUNNING`, if a sub-channel has `rateLogging` above 0 (default 1) | Runs [`LogSocketRates()`](/api/fair/mq/Device/#LogSocketRates). It ends when a transition is pending. The device joins it after `PostRun()` ([Device.cxx](/source/fairmq/Device.cxx/#L384-L395)). |
|
| Rate logger thread | The device | In `RUNNING`, if a sub-channel has `rateLogging` above 0 (default 1) | Runs [`LogSocketRates()`](/api/fair/mq/Device/#LogSocketRates). It ends when a transition is pending. The device joins it after `PostRun()` ([Device.cxx](/source/fairmq/Device.cxx/#L384-L395)). |
|
||||||
| Poll threads | The device | In `RUNNING`, with data callbacks on input channels of more than one transport | One thread for each transport. Each thread polls its channels and calls their data callbacks ([Device.cxx](/source/fairmq/Device.cxx/#L509-L523)). |
|
| Poll threads | The device | In `RUNNING`, with data callbacks on input channels of more than one transport | One thread for each transport. Each thread polls its channels and calls their data callbacks ([Device.cxx](/source/fairmq/Device.cxx/#L509-L523)). |
|
||||||
| ZeroMQ I/O threads | ZeroMQ | While the transport exists | Each transport, also `shmem`, has one ZeroMQ context with [`--io-threads`](/reference/options/#opt-io-threads) I/O threads (default 1) ([zeromq/TransportFactory.h](/source/fairmq/zeromq/TransportFactory.h/#L39), [shmem/TransportFactory.h](/source/fairmq/shmem/TransportFactory.h/#L72-L74)). ZeroMQ moves the data of the sockets in these threads (ZeroMQ behavior). |
|
| ZeroMQ I/O threads | ZeroMQ | While the transport exists | Each transport, also `shmem`, has one ZeroMQ context with [`--io-threads`](/reference/options/#opt-io-threads) I/O threads (default 1) ([zeromq/TransportFactory.h](/source/fairmq/zeromq/TransportFactory.h/#L39), [shmem/TransportFactory.h](/source/fairmq/shmem/TransportFactory.h/#L72-L74)). ZeroMQ moves the data of the sockets in these threads (ZeroMQ behavior). |
|
||||||
|
|||||||
@@ -431,10 +431,10 @@ The limits are in the parts of FairMQ that exist once per process:
|
|||||||
See [Logging](/tools/logging/).
|
See [Logging](/tools/logging/).
|
||||||
- **The signal handling of the control plugin is global.** The control plugin installs its handlers for `SIGINT` and
|
- **The signal handling of the control plugin is global.** The control plugin installs its handlers for `SIGINT` and
|
||||||
`SIGTERM` with `signal()`. It counts the signals in global variables, and no code resets them
|
`SIGTERM` with `signal()`. It counts the signals in global variables, and no code resets them
|
||||||
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L25-L45)). One signal starts the shutdown of each device
|
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L27-L47)). One signal starts the shutdown of each device
|
||||||
with a control plugin in the process. The next `SIGINT` ends the process with `std::abort()`. A control plugin that
|
with a control plugin in the process. The next `SIGINT` ends the process with `std::abort()`. A control plugin that
|
||||||
starts after a signal, with `--catch-signals 1`, shuts down its device at once.
|
starts after a signal, with `--catch-signals 1`, shuts down its device at once.
|
||||||
- **The interactive mode reads the standard input** ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L157-L180)).
|
- **The interactive mode reads the standard input** ([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L159-L191)).
|
||||||
Two interactive controllers in one process read the same keys.
|
Two interactive controllers in one process read the same keys.
|
||||||
- **One `DeviceRunner` for each process.** `DeviceRunner` sets the global logger, and it loads the control plugin.
|
- **One `DeviceRunner` for each process.** `DeviceRunner` sets the global logger, and it loads the control plugin.
|
||||||
FairMQ has no test with two `DeviceRunner` objects in one process. Run the other devices without `DeviceRunner`,
|
FairMQ has no test with two `DeviceRunner` objects in one process. Run the other devices without `DeviceRunner`,
|
||||||
|
|||||||
@@ -259,8 +259,8 @@ the senders sent everything.
|
|||||||
## What the controllers do
|
## What the controllers do
|
||||||
|
|
||||||
The built-in control plugin handles the end of `RUNNING` in a different way in each mode
|
The built-in control plugin handles the end of `RUNNING` in a different way in each mode
|
||||||
([interactive](/source/fairmq/plugins/control/Control.cxx/#L274-L290),
|
([interactive](/source/fairmq/plugins/control/Control.cxx/#L285-L301),
|
||||||
[static and gui](/source/fairmq/plugins/control/Control.cxx/#L403-L437)):
|
[static and gui](/source/fairmq/plugins/control/Control.cxx/#L414-L448)):
|
||||||
|
|
||||||
| `--control` | When `RUNNING` ends | What this means for the end of the run |
|
| `--control` | When `RUNNING` ends | What this means for the end of the run |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ matrix: from all states, `ErrorFound` leads to `ERROR`. The source is the `trans
|
|||||||
With `--control dynamic` (default), `interactive` or `external`, the control plugin reads keys from
|
With `--control dynamic` (default), `interactive` or `external`, the control plugin reads keys from
|
||||||
the standard input. It reads one character at a time, without Enter. The plugin prints the line
|
the standard input. It reads one character at a time, without Enter. The plugin prints the line
|
||||||
` --> [key] action` before it does the action. Source:
|
` --> [key] action` before it does the action. Source:
|
||||||
[`Control.cxx`](/source/fairmq/plugins/control/Control.cxx/#L154-L272).
|
[`Control.cxx`](/source/fairmq/plugins/control/Control.cxx/#L156-L283).
|
||||||
|
|
||||||
| Key | Action |
|
| Key | Action |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
@@ -136,15 +136,13 @@ the standard input. It reads one character at a time, without Enter. The plugin
|
|||||||
The keys `b`, `x`, `j`, `r`, `s`, `t` and `d` request only one transition. They do not wait for
|
The keys `b`, `x`, `j`, `r`, `s`, `t` and `d` request only one transition. They do not wait for
|
||||||
the `Auto` transitions. If the state does not accept the transition, the state does not change.
|
the `Auto` transitions. If the state does not accept the transition, the state does not change.
|
||||||
|
|
||||||
:::caution[Known defect]
|
If several keys arrive in one read, for example `cq` from a pipe, the plugin handles all keys in
|
||||||
If several keys arrive in one read, for example `cq` from a pipe, the plugin handles only the first
|
their order. It ignores spaces and line ends. At the end of the input, it stops reading keys.
|
||||||
key. See the disabled test `ControlPlugin.SeveralKeysInOneInput` in `test/device/_runner.cxx`.
|
|
||||||
:::
|
|
||||||
|
|
||||||
## Startup and shutdown sequences
|
## Startup and shutdown sequences
|
||||||
|
|
||||||
The control modes `dynamic`, `static` and `gui` start with the same sequence
|
The control modes `dynamic`, `static` and `gui` start with the same sequence
|
||||||
([`RunStartupSequence()`](/source/fairmq/plugins/control/Control.cxx/#L104-L142)). The plugin requests
|
([`RunStartupSequence()`](/source/fairmq/plugins/control/Control.cxx/#L106-L144)). The plugin requests
|
||||||
a transition and waits for the next states:
|
a transition and waits for the next states:
|
||||||
|
|
||||||
| Step | Request | Wait for |
|
| Step | Request | Wait for |
|
||||||
@@ -158,7 +156,7 @@ a transition and waits for the next states:
|
|||||||
|
|
||||||
A signal stops the sequence after the current step, and the plugin runs the shutdown sequence. The
|
A signal stops the sequence after the current step, and the plugin runs the shutdown sequence. The
|
||||||
state `ERROR` also stops the sequence, but without the shutdown sequence. The shutdown sequence
|
state `ERROR` also stops the sequence, but without the shutdown sequence. The shutdown sequence
|
||||||
([`RunShutdownSequence()`](/source/fairmq/plugins/control/Control.cxx/#L471-L506)) requests one
|
([`RunShutdownSequence()`](/source/fairmq/plugins/control/Control.cxx/#L482-L517)) requests one
|
||||||
transition for each state, until `EXITING` or `ERROR`:
|
transition for each state, until `EXITING` or `ERROR`:
|
||||||
|
|
||||||
| Current state | Request |
|
| Current state | Request |
|
||||||
|
|||||||
@@ -324,7 +324,7 @@ In the test, `TestTag` writes `TestTag` with its `operator<<`:
|
|||||||
## IO.h
|
## IO.h
|
||||||
|
|
||||||
Internal in practice: the control plugin uses it for the keys of the interactive mode
|
Internal in practice: the control plugin uses it for the keys of the interactive mode
|
||||||
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L162)), and `fairmq-shmmonitor -i` for
|
([Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L164)), and `fairmq-shmmonitor -i` for
|
||||||
its keys ([Monitor.cxx](/source/fairmq/shmem/Monitor.cxx/#L368)). `fairmq/Tools.h` does not include
|
its keys ([Monitor.cxx](/source/fairmq/shmem/Monitor.cxx/#L368)). `fairmq/Tools.h` does not include
|
||||||
this header.
|
this header.
|
||||||
|
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ In the state `RUNNING`, the plugin `control` also prints a list of keys. With th
|
|||||||
|
|
||||||
## Stop the devices
|
## Stop the devices
|
||||||
|
|
||||||
Press Ctrl+C in each terminal. The plugin `control` catches the signal `SIGINT`. It moves the device through the reset states to `EXITING` (see [Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L439-L469)):
|
Press Ctrl+C in each terminal. The plugin `control` catches the signal `SIGINT`. It moves the device through the reset states to `EXITING` (see [Control.cxx](/source/fairmq/plugins/control/Control.cxx/#L450-L480)):
|
||||||
|
|
||||||
```txt
|
```txt
|
||||||
[INFO] Received device shutdown request (signal 2).
|
[INFO] Received device shutdown request (signal 2).
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <fairmq/tools/IO.h>
|
#include <fairmq/tools/IO.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <cctype> // isspace
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <csignal> // catching system signals
|
#include <csignal> // catching system signals
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <poll.h> // for the interactive mode
|
#include <poll.h> // for the interactive mode
|
||||||
|
#include <unistd.h> // read
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -172,12 +174,21 @@ auto Control::RunREPL() -> void
|
|||||||
bool keepRunning = true;
|
bool keepRunning = true;
|
||||||
|
|
||||||
while (keepRunning) {
|
while (keepRunning) {
|
||||||
if (poll(cinfd, 1, 100)) {
|
if (poll(cinfd, 1, 100) > 0) {
|
||||||
if (fDeviceShutdownRequested) {
|
if (fDeviceShutdownRequested) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
cin >> input;
|
// read() without a buffer: poll() then sees each key that is not handled yet
|
||||||
|
auto const n = read(cinfd[0].fd, &input, 1);
|
||||||
|
if (n == 0) {
|
||||||
|
LOG(debug) << "End of input on stdin, interactive control stops reading keys";
|
||||||
|
cinfd[0].fd = -1; // poll() ignores a negative fd
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (n < 0 || isspace(static_cast<unsigned char>(input))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case 'c':
|
case 'c':
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ auto operator<<(std::ostream& os, KeyParam const& param) -> std::ostream&
|
|||||||
class InteractiveKey : public testing::TestWithParam<KeyParam>
|
class InteractiveKey : public testing::TestWithParam<KeyParam>
|
||||||
{};
|
{};
|
||||||
|
|
||||||
// One key, then SIGINT ends the device (see the defect below).
|
// One key, then SIGINT ends the device.
|
||||||
TEST_P(InteractiveKey, PrintsItsAction)
|
TEST_P(InteractiveKey, PrintsItsAction)
|
||||||
{
|
{
|
||||||
RunOptions options;
|
RunOptions options;
|
||||||
@@ -92,11 +92,8 @@ INSTANTIATE_TEST_SUITE_P(, InteractiveKey,
|
|||||||
KeyParam{'z', "Invalid input: [z]"}),
|
KeyParam{'z', "Invalid input: [z]"}),
|
||||||
[](testing::TestParamInfo<KeyParam> const& p) { return string(1, p.param.key); });
|
[](testing::TestParamInfo<KeyParam> const& p) { return string(1, p.param.key); });
|
||||||
|
|
||||||
// Known defect: the control plugin polls the stdin file descriptor before it
|
// Keys that arrive in one read (e.g. "cq" from a pipe) are all handled.
|
||||||
// reads the next key from std::cin. Keys that arrive in one read (e.g. "cq"
|
TEST(ControlPlugin, SeveralKeysInOneInput)
|
||||||
// from a pipe) stay in the std::cin buffer, and the plugin never handles them.
|
|
||||||
// Enable this test with the fix.
|
|
||||||
TEST(ControlPlugin, DISABLED_SeveralKeysInOneInput)
|
|
||||||
{
|
{
|
||||||
RunOptions options;
|
RunOptions options;
|
||||||
options.control = "interactive";
|
options.control = "interactive";
|
||||||
|
|||||||
Reference in New Issue
Block a user