diff --git a/docs/src/content/docs/concepts/plugins.mdx b/docs/src/content/docs/concepts/plugins.mdx index 25eef002..fa5415ee 100644 --- a/docs/src/content/docs/concepts/plugins.mdx +++ b/docs/src/content/docs/concepts/plugins.mdx @@ -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 `No transition from state … on transition …` at the severity `state`. -:::caution[One key at a time] -The plugin reads one key each time new input arrives. When several keys arrive together, for example `cq` from a pipe, -the plugin handles only the first one. It reads the next key only after more input arrives. See the disabled test -`ControlPlugin.DISABLED_SeveralKeysInOneInput` in `test/device/_runner.cxx`. -::: +The plugin reads the keys one at a time. When several keys arrive together, for example `cq` from a pipe, the plugin +handles all of them in their order. It ignores spaces and line ends. At the end of the input, it stops reading keys. +The test `ControlPlugin.SeveralKeysInOneInput` in `test/device/_runner.cxx` checks this. ### Signals 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 `Waiting for graceful device shutdown. Hit Ctrl-C again to abort immediately.` diff --git a/docs/src/content/docs/concepts/threads.mdx b/docs/src/content/docs/concepts/threads.mdx index e431ba3e..4c08309a 100644 --- a/docs/src/content/docs/concepts/threads.mdx +++ b/docs/src/content/docs/concepts/threads.mdx @@ -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 | | --- | --- | --- | --- | | 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)). | -| 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)). | +| 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/#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)). | | 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). | diff --git a/docs/src/content/docs/guides/control-from-code.mdx b/docs/src/content/docs/guides/control-from-code.mdx index 83b7287c..df64bb54 100644 --- a/docs/src/content/docs/guides/control-from-code.mdx +++ b/docs/src/content/docs/guides/control-from-code.mdx @@ -431,10 +431,10 @@ The limits are in the parts of FairMQ that exist once per process: See [Logging](/tools/logging/). - **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 - ([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 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. - **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`, diff --git a/docs/src/content/docs/guides/end-of-run.mdx b/docs/src/content/docs/guides/end-of-run.mdx index 1947bda6..1829161f 100644 --- a/docs/src/content/docs/guides/end-of-run.mdx +++ b/docs/src/content/docs/guides/end-of-run.mdx @@ -259,8 +259,8 @@ the senders sent everything. ## What the controllers do 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), -[static and gui](/source/fairmq/plugins/control/Control.cxx/#L403-L437)): +([interactive](/source/fairmq/plugins/control/Control.cxx/#L285-L301), +[static and gui](/source/fairmq/plugins/control/Control.cxx/#L414-L448)): | `--control` | When `RUNNING` ends | What this means for the end of the run | | --- | --- | --- | diff --git a/docs/src/content/docs/reference/states.mdx b/docs/src/content/docs/reference/states.mdx index 41b2e434..7c318c57 100644 --- a/docs/src/content/docs/reference/states.mdx +++ b/docs/src/content/docs/reference/states.mdx @@ -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 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: -[`Control.cxx`](/source/fairmq/plugins/control/Control.cxx/#L154-L272). +[`Control.cxx`](/source/fairmq/plugins/control/Control.cxx/#L156-L283). | 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 `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 only the first -key. See the disabled test `ControlPlugin.SeveralKeysInOneInput` in `test/device/_runner.cxx`. -::: +If several keys arrive in one read, for example `cq` from a pipe, the plugin handles all keys in +their order. It ignores spaces and line ends. At the end of the input, it stops reading keys. ## Startup and shutdown sequences 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: | 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 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`: | Current state | Request | diff --git a/docs/src/content/docs/reference/tools.mdx b/docs/src/content/docs/reference/tools.mdx index aa4c2c73..2c143cf1 100644 --- a/docs/src/content/docs/reference/tools.mdx +++ b/docs/src/content/docs/reference/tools.mdx @@ -324,7 +324,7 @@ In the test, `TestTag` writes `TestTag` with its `operator<<`: ## IO.h 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 this header. diff --git a/docs/src/content/docs/start/first-device.mdx b/docs/src/content/docs/start/first-device.mdx index 9d38c3f0..3ff9a5f0 100644 --- a/docs/src/content/docs/start/first-device.mdx +++ b/docs/src/content/docs/start/first-device.mdx @@ -180,7 +180,7 @@ In the state `RUNNING`, the plugin `control` also prints a list of keys. With th ## 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 [INFO] Received device shutdown request (signal 2). diff --git a/fairmq/plugins/control/Control.cxx b/fairmq/plugins/control/Control.cxx index 77882535..b064fb44 100644 --- a/fairmq/plugins/control/Control.cxx +++ b/fairmq/plugins/control/Control.cxx @@ -11,6 +11,7 @@ #include #include +#include // isspace #include #include // catching system signals #include @@ -19,6 +20,7 @@ #include #include // for the interactive mode +#include // read using namespace std; @@ -172,12 +174,21 @@ auto Control::RunREPL() -> void bool keepRunning = true; while (keepRunning) { - if (poll(cinfd, 1, 100)) { + if (poll(cinfd, 1, 100) > 0) { if (fDeviceShutdownRequested) { 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(input))) { + continue; + } switch (input) { case 'c': diff --git a/test/device/_runner.cxx b/test/device/_runner.cxx index 9af3f76b..6ea88003 100644 --- a/test/device/_runner.cxx +++ b/test/device/_runner.cxx @@ -73,7 +73,7 @@ auto operator<<(std::ostream& os, KeyParam const& param) -> std::ostream& class InteractiveKey : public testing::TestWithParam {}; -// One key, then SIGINT ends the device (see the defect below). +// One key, then SIGINT ends the device. TEST_P(InteractiveKey, PrintsItsAction) { RunOptions options; @@ -92,11 +92,8 @@ INSTANTIATE_TEST_SUITE_P(, InteractiveKey, KeyParam{'z', "Invalid input: [z]"}), [](testing::TestParamInfo const& p) { return string(1, p.param.key); }); -// Known defect: the control plugin polls the stdin file descriptor before it -// reads the next key from std::cin. Keys that arrive in one read (e.g. "cq" -// 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) +// Keys that arrive in one read (e.g. "cq" from a pipe) are all handled. +TEST(ControlPlugin, SeveralKeysInOneInput) { RunOptions options; options.control = "interactive";