fix(config): reject channel properties of the wrong type

- ptree::get(key, default) returns the default also for a value that
  does not convert, so a typo (e.g. sndBufSize=big) silently gave the
  default value
- Use the default only for a missing key; throw ParserError with the
  key and the value for a value of the wrong type
- Rethrow parser errors in the config plugin: before, the device
  logged the error and continued without its channel configuration
- Enable JSONParser and SuboptParser RejectsValuesOfTheWrongType
- 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 20:31:06 +00:00
committed by Dennis Klein
parent 442b203d0d
commit b70595cebe
9 changed files with 73 additions and 55 deletions

View File

@@ -107,7 +107,7 @@ The configuration of a sub-channel is a group of properties with the keys `chans
## The config plugin
The config plugin is a part of each device made with `runDevice.h`. It declares the options of the groups Device and Shared memory in the [table of options](/reference/options/), and reads the channel configuration ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L25-L53)).
The config plugin is a part of each device made with `runDevice.h`. It declares the options of the groups Device and Shared memory in the [table of options](/reference/options/), and reads the channel configuration ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L25-L54)).
When the device goes into the state `INITIALIZING DEVICE`, the config plugin:
@@ -115,7 +115,7 @@ When the device goes into the state `INITIALIZING DEVICE`, the config plugin:
2. Reads the JSON file of `--mq-config` for this value. If there is no `--mq-config`, it reads `--channel-config`.
3. Stores the channel properties with `SetProperties()`.
If the parser throws an exception, the plugin logs the message with the severity `error`, and the device continues without these channels. If `--id` and `--config-key` are empty, the plugin logs a warning and reads nothing. [JSON configuration](/reference/json/#errors) lists the errors.
If the parser throws an exception, the plugin logs the message with the severity `error` and throws the exception again. The device goes to `ERROR` and does not start. If `--id` and `--config-key` are empty, the plugin logs a warning and reads nothing. [JSON configuration](/reference/json/#errors) lists the errors.
The device runner makes the plugins of `-P` before the config plugin. In `INITIALIZING DEVICE`, their callbacks thus run first. The config plugin then replaces the values of all channel properties that the configuration also gives.

View File

@@ -40,7 +40,7 @@ The file has one entry for each device in `fairMQOptions.devices`. This is the e
A device uses the entry whose `id` is equal to its `--id`. With `--config-key processor`, the device uses
the entry with `"key": "processor"` instead ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L29-L33),
[JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L69)). Thus `processor1` and `processor2` have different
[JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L90)). Thus `processor1` and `processor2` have different
IDs, but the same channels. The [JSON configuration](/reference/json/) page describes the format.
## Build the example

View File

@@ -255,9 +255,9 @@ The test `Config.SetConfig` parses two options and gives the object to the devic
### The device options and the channels
The device options, for example `--id`, `--transport`, `--session`, `--mq-config` and `--channel-config`, are the
options of the config plugin ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L56-L87)). In
options of the config plugin ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L57-L88)). In
`INITIALIZING DEVICE`, the config plugin also turns `--mq-config` or `--channel-config` into channel properties
([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L22-L53)). A program without `DeviceRunner` has two ways to get
([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L22-L54)). A program without `DeviceRunner` has two ways to get
them:
- Load the config plugin with a plugin manager, and add its options before the parse. See

View File

@@ -24,7 +24,7 @@ The keys are case-sensitive. The `--channel-config` parser logs a warning for an
## All properties
The type is the C++ type of the property. The parsers of `--channel-config` and of JSON files convert the values to this type. A value that the parser cannot convert gives the default, without a message ([_jsonparser.cxx](https://github.com/FairRootGroup/FairMQ/blob/dev/test/properties/_jsonparser.cxx#L157-L161)).
The type is the C++ type of the property. The parsers of `--channel-config` and of JSON files convert the values to this type. For a value that it cannot convert, the parser throws `fair::mq::ParserError` ([_jsonparser.cxx](https://github.com/FairRootGroup/FairMQ/blob/dev/test/properties/_jsonparser.cxx#L158-L166)). A missing key gives the default.
| Key | Type | Default | Allowed values | Effect |
| --- | --- | --- | --- | --- |
@@ -44,15 +44,15 @@ The type is the C++ type of the property. The parsers of `--channel-config` and
| [`portRangeMin`](#portrangemin-portrangemax-and-autobind) | int | [`22000`](/source/fairmq/Channel.h/#L380) | A TCP port | Lowest port for autobind. |
| [`portRangeMax`](#portrangemin-portrangemax-and-autobind) | int | [`23000`](/source/fairmq/Channel.h/#L381) | A TCP port | Highest port for autobind. |
| [`autoBind`](#portrangemin-portrangemax-and-autobind) | bool | [`true`](/source/fairmq/Channel.h/#L382-L386) | `true`, `false`, `1`, `0` | Bind to a random port of the range when the bind fails. |
| [`numSockets`](#numsockets) | int | [`0`](/source/fairmq/JSONParser.cxx/#L108) | 0 or more | Number of sub-channels with the same properties. |
| [`numSockets`](#numsockets) | int | [`0`](/source/fairmq/JSONParser.cxx/#L129) | 0 or more | Number of sub-channels with the same properties. |
The JSON parser uses the same defaults ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L90-L105)). The device also uses them for a property that is not set ([Channel.cxx](/source/fairmq/Channel.cxx/#L92-L112)).
The JSON parser uses the same defaults ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L111-L126)). The device also uses them for a property that is not set ([Channel.cxx](/source/fairmq/Channel.cxx/#L92-L112)).
## The properties in detail
### `name`
The name of the channel. In `--channel-config`, write `name=data`, or put the name and a colon before the first key: `data:type=push,...` ([SuboptParser.cxx](/source/fairmq/SuboptParser.cxx/#L91-L98)). Each channel must have a name. Without it, the parsers of JSON files and of `--channel-config` throw `boost::property_tree::ptree_bad_path` ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L107)). The `--channel-config` parser first logs `missing channel name in argument of option --channel-config`. The config plugin logs the exception, and the device gets no channels from this configuration. In the property key, the name is the `<name>` part of `chans.<name>.<index>.<key>`.
The name of the channel. In `--channel-config`, write `name=data`, or put the name and a colon before the first key: `data:type=push,...` ([SuboptParser.cxx](/source/fairmq/SuboptParser.cxx/#L91-L98)). Each channel must have a name. Without it, the parsers of JSON files and of `--channel-config` throw `boost::property_tree::ptree_bad_path` ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L128)). The `--channel-config` parser first logs `missing channel name in argument of option --channel-config`. The config plugin logs the exception and throws it again, and the device goes to `ERROR`. In the property key, the name is the `<name>` part of `chans.<name>.<index>.<key>`.
A sub-channel has the name `<name>[<index>]`, for example `data[0]`. Before the device attaches a sub-channel, [`Channel::Validate()`](/api/fair/mq/Channel/#Validate) checks this full name ([Channel.cxx](/source/fairmq/Channel.cxx/#L183-L190)). The allowed characters are `a`–`z`, `A`–`Z`, `0`–`9`, `-`, `_`, `[`, `]` and `#`. For another character, the device logs `channel name contains illegal character` and does not attach the sub-channel. The device then goes to `ERROR`: at once for a binding sub-channel, after the time of `--init-timeout` for a connecting sub-channel ([Device.cxx](/source/fairmq/Device.cxx/#L217-L265)).
@@ -176,7 +176,7 @@ If the configured port is in use, autobind binds to a random port. The device lo
### `numSockets`
Only in `--channel-config` and on the channel level of a JSON file. For a value above 0, the parser makes this number of sub-channels, all with the properties of the channel ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L108-L122)).
Only in `--channel-config` and on the channel level of a JSON file. For a value above 0, the parser makes this number of sub-channels, all with the properties of the channel ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L129-L143)).
- In JSON, the parser ignores the `sockets` array of the channel. All sub-channels get the `address` of the channel.
- In `--channel-config`, the parser ignores the `address=` keys. The sub-channels have no address.

View File

@@ -18,7 +18,7 @@ The file of the [1-n-1 example](/examples/1-n-1/) has three entries: one for the
## Structure
The parser reads this tree ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L39-L190)):
The parser reads this tree ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L39-L211)):
```txt
fairMQOptions object, required
@@ -63,10 +63,10 @@ An entry needs `id` or `key`, otherwise no device can select it.
## How the device finds its entry
The [config plugin](/concepts/configuration/#the-config-plugin) reads the file when the device goes into the state `INITIALIZING DEVICE` ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L25-L53)):
The [config plugin](/concepts/configuration/#the-config-plugin) reads the file when the device goes into the state `INITIALIZING DEVICE` ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L25-L54)):
1. The plugin takes the value of `--config-key`. Without `--config-key`, it takes the value of `--id`.
2. The parser compares this value with the `key` of each device entry. For an entry without `key`, it compares the value with `id` ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L69-L74)).
2. The parser compares this value with the `key` of each device entry. For an entry without `key`, it compares the value with `id` ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L90-L95)).
3. The parser reads the channels of each entry that matches.
An entry with a `key` does not match its `id`. Several devices can use the same entry. In the start script of the 1-n-1 example, both processors have their own ID and the same key `processor`:
@@ -96,15 +96,13 @@ The entry with the ID `other` has the key `sink`. `--config-key sink` selects it
## `numSockets`
With `numSockets` above 0, the parser makes this number of sub-channels. All of them get the values of the channel, also the `address`. The parser then ignores the `sockets` array ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L108-L122)). [Channel properties](/reference/channel-properties/#numsockets) describes the effect on binding sub-channels.
With `numSockets` above 0, the parser makes this number of sub-channels. All of them get the values of the channel, also the `address`. The parser then ignores the `sockets` array ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L129-L143)). [Channel properties](/reference/channel-properties/#numsockets) describes the effect on binding sub-channels.
## Value types
Write strings as JSON strings, numbers as JSON numbers, and booleans as `true` or `false`. [Channel properties](/reference/channel-properties/) gives the type of each property.
:::caution[Wrong types]
The parser reads each value with a default ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L90-L105)). If it cannot convert a value, it uses the default and shows no error. For example, `"sndBufSize": "big"` gives `sndBufSize` 1000. To see the values, start the device with `--severity trace`: the parser then logs each property of each sub-channel.
:::
The parser uses the default of a property only when the key is missing ([JSONParser.cxx](/source/fairmq/JSONParser.cxx/#L111-L126)). If it cannot convert a value, it throws [`fair::mq::ParserError`](/api/fair/mq/ParserError/). For example, `"sndBufSize": "big"` gives `Invalid value 'big' for channel property 'sndBufSize'`. To see the values, start the device with `--severity trace`: the parser then logs each property of each sub-channel.
## Errors
@@ -115,12 +113,12 @@ The parser reads each value with a default ([JSONParser.cxx](/source/fairmq/JSON
| A channel has no `name`. | `boost::property_tree::ptree_bad_path` |
| The ID is empty. This occurs only when you call the parser from code. | [`fair::mq::ParserError`](/api/fair/mq/ParserError/): ``no device ID provided. Provide with `--id` cmd option`` |
| No entry matches the ID or the key. | No error. The device has no channels from the file. |
| A value has the wrong type. | No error. The parser uses the default. |
| A value has the wrong type. | [`fair::mq::ParserError`](/api/fair/mq/ParserError/): `Invalid value '<value>' for channel property '<key>'` |
The config plugin catches the exceptions and logs their message with the severity `error` ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L46-L48)). The device then continues without the channels of the file. If the device has no `--id` and no `--config-key`, the plugin logs the warning `No device ID or config-key provided for the configuration parser.` and does not read the file.
The config plugin catches the exceptions, logs `Invalid channel configuration:` and the message with the severity `error`, and throws the exception again ([Config.cxx](/source/fairmq/plugins/config/Config.cxx/#L46-L49)). The device then goes to `ERROR` in `INITIALIZING DEVICE` and does not start. If the device has no `--id` and no `--config-key`, the plugin logs the warning `No device ID or config-key provided for the configuration parser.` and does not read the file.
:::caution
A device with a broken file can start without channels. If your device does not find a channel, look for an error message from the parser at the start of the log.
:::tip
If your device does not start, look for `Invalid channel configuration:` at the start of the log.
:::
## Parse a file from code

View File

@@ -58,6 +58,27 @@ Properties JSONParser(const string& filename, const string& deviceId)
namespace helper
{
namespace
{
// ptree::get(key, default) also returns the default for a value of the wrong type.
// Use the default only for a missing key.
template<typename T>
T Get(const ptree& tree, const string& key, const T& defaultValue)
{
auto const child = tree.get_child_optional(key);
if (!child) {
return defaultValue;
}
try {
return child->get_value<T>();
} catch (ptree_bad_data const&) {
throw ParserError(ToString("Invalid value '", child->data(), "' for channel property '", key, "'"));
}
}
} // namespace
Properties DeviceParser(const ptree& fairMQOptions, const string& deviceId)
{
Properties properties;
@@ -92,20 +113,20 @@ void ChannelParser(const ptree& tree, Properties& properties)
commonProperties.emplace("method", cn.second.get<string>("method", Channel::DefaultMethod));
commonProperties.emplace("address", cn.second.get<string>("address", Channel::DefaultAddress));
commonProperties.emplace("transport", cn.second.get<string>("transport", Channel::DefaultTransportName));
commonProperties.emplace("sndBufSize", cn.second.get<int>("sndBufSize", Channel::DefaultSndBufSize));
commonProperties.emplace("rcvBufSize", cn.second.get<int>("rcvBufSize", Channel::DefaultRcvBufSize));
commonProperties.emplace("sndKernelSize", cn.second.get<int>("sndKernelSize", Channel::DefaultSndKernelSize));
commonProperties.emplace("rcvKernelSize", cn.second.get<int>("rcvKernelSize", Channel::DefaultRcvKernelSize));
commonProperties.emplace("sndTimeoutMs", cn.second.get<int>("sndTimeoutMs", Channel::DefaultSndTimeoutMs));
commonProperties.emplace("rcvTimeoutMs", cn.second.get<int>("rcvTimeoutMs", Channel::DefaultRcvTimeoutMs));
commonProperties.emplace("linger", cn.second.get<int>("linger", Channel::DefaultLinger));
commonProperties.emplace("rateLogging", cn.second.get<int>("rateLogging", Channel::DefaultRateLogging));
commonProperties.emplace("portRangeMin", cn.second.get<int>("portRangeMin", Channel::DefaultPortRangeMin));
commonProperties.emplace("portRangeMax", cn.second.get<int>("portRangeMax", Channel::DefaultPortRangeMax));
commonProperties.emplace("autoBind", cn.second.get<bool>("autoBind", Channel::DefaultAutoBind));
commonProperties.emplace("sndBufSize", Get<int>(cn.second, "sndBufSize", Channel::DefaultSndBufSize));
commonProperties.emplace("rcvBufSize", Get<int>(cn.second, "rcvBufSize", Channel::DefaultRcvBufSize));
commonProperties.emplace("sndKernelSize", Get<int>(cn.second, "sndKernelSize", Channel::DefaultSndKernelSize));
commonProperties.emplace("rcvKernelSize", Get<int>(cn.second, "rcvKernelSize", Channel::DefaultRcvKernelSize));
commonProperties.emplace("sndTimeoutMs", Get<int>(cn.second, "sndTimeoutMs", Channel::DefaultSndTimeoutMs));
commonProperties.emplace("rcvTimeoutMs", Get<int>(cn.second, "rcvTimeoutMs", Channel::DefaultRcvTimeoutMs));
commonProperties.emplace("linger", Get<int>(cn.second, "linger", Channel::DefaultLinger));
commonProperties.emplace("rateLogging", Get<int>(cn.second, "rateLogging", Channel::DefaultRateLogging));
commonProperties.emplace("portRangeMin", Get<int>(cn.second, "portRangeMin", Channel::DefaultPortRangeMin));
commonProperties.emplace("portRangeMax", Get<int>(cn.second, "portRangeMax", Channel::DefaultPortRangeMax));
commonProperties.emplace("autoBind", Get<bool>(cn.second, "autoBind", Channel::DefaultAutoBind));
string name = cn.second.get<string>("name");
int numSockets = cn.second.get<int>("numSockets", 0);
int numSockets = Get<int>(cn.second, "numSockets", 0);
if (numSockets > 0) {
LOG(trace) << name << ":";
@@ -144,17 +165,17 @@ void SubChannelParser(const ptree& channelTree, Properties& properties, const st
newProperties["method"] = sn.second.get<string>("method", boost::any_cast<string>(commonProperties.at("method")));
newProperties["address"] = sn.second.get<string>("address", boost::any_cast<string>(commonProperties.at("address")));
newProperties["transport"] = sn.second.get<string>("transport", boost::any_cast<string>(commonProperties.at("transport")));
newProperties["sndBufSize"] = sn.second.get<int>("sndBufSize", boost::any_cast<int>(commonProperties.at("sndBufSize")));
newProperties["rcvBufSize"] = sn.second.get<int>("rcvBufSize", boost::any_cast<int>(commonProperties.at("rcvBufSize")));
newProperties["sndKernelSize"] = sn.second.get<int>("sndKernelSize", boost::any_cast<int>(commonProperties.at("sndKernelSize")));
newProperties["rcvKernelSize"] = sn.second.get<int>("rcvKernelSize", boost::any_cast<int>(commonProperties.at("rcvKernelSize")));
newProperties["sndTimeoutMs"] = sn.second.get<int>("sndTimeoutMs", boost::any_cast<int>(commonProperties.at("sndTimeoutMs")));
newProperties["rcvTimeoutMs"] = sn.second.get<int>("rcvTimeoutMs", boost::any_cast<int>(commonProperties.at("rcvTimeoutMs")));
newProperties["linger"] = sn.second.get<int>("linger", boost::any_cast<int>(commonProperties.at("linger")));
newProperties["rateLogging"] = sn.second.get<int>("rateLogging", boost::any_cast<int>(commonProperties.at("rateLogging")));
newProperties["portRangeMin"] = sn.second.get<int>("portRangeMin", boost::any_cast<int>(commonProperties.at("portRangeMin")));
newProperties["portRangeMax"] = sn.second.get<int>("portRangeMax", boost::any_cast<int>(commonProperties.at("portRangeMax")));
newProperties["autoBind"] = sn.second.get<bool>("autoBind", boost::any_cast<bool>(commonProperties.at("autoBind")));
newProperties["sndBufSize"] = Get<int>(sn.second, "sndBufSize", boost::any_cast<int>(commonProperties.at("sndBufSize")));
newProperties["rcvBufSize"] = Get<int>(sn.second, "rcvBufSize", boost::any_cast<int>(commonProperties.at("rcvBufSize")));
newProperties["sndKernelSize"] = Get<int>(sn.second, "sndKernelSize", boost::any_cast<int>(commonProperties.at("sndKernelSize")));
newProperties["rcvKernelSize"] = Get<int>(sn.second, "rcvKernelSize", boost::any_cast<int>(commonProperties.at("rcvKernelSize")));
newProperties["sndTimeoutMs"] = Get<int>(sn.second, "sndTimeoutMs", boost::any_cast<int>(commonProperties.at("sndTimeoutMs")));
newProperties["rcvTimeoutMs"] = Get<int>(sn.second, "rcvTimeoutMs", boost::any_cast<int>(commonProperties.at("rcvTimeoutMs")));
newProperties["linger"] = Get<int>(sn.second, "linger", boost::any_cast<int>(commonProperties.at("linger")));
newProperties["rateLogging"] = Get<int>(sn.second, "rateLogging", boost::any_cast<int>(commonProperties.at("rateLogging")));
newProperties["portRangeMin"] = Get<int>(sn.second, "portRangeMin", boost::any_cast<int>(commonProperties.at("portRangeMin")));
newProperties["portRangeMax"] = Get<int>(sn.second, "portRangeMax", boost::any_cast<int>(commonProperties.at("portRangeMax")));
newProperties["autoBind"] = Get<bool>(sn.second, "autoBind", boost::any_cast<bool>(commonProperties.at("autoBind")));
LOG(trace) << "" << channelName << "[" << i << "]:";
for (auto& p : newProperties) {

View File

@@ -44,7 +44,8 @@ Config::Config(const string& name, Plugin::Version version, const string& mainta
LOG(info) << "fair::mq::plugins::Config: no channels configuration provided via --mq-config or --channel-config";
}
} catch (exception& e) {
LOG(error) << e.what();
LOG(error) << "Invalid channel configuration: " << e.what();
throw;
}
} else {
LOG(warn) << "No device ID or config-key provided for the configuration parser.";

View File

@@ -154,16 +154,15 @@ TEST(JSONParser, Errors)
boost::property_tree::ptree_bad_path);
}
// Known defect: ChannelParser() reads the values with ptree::get(key, default).
// That overload returns the default for a value of the wrong type, so a typo
// (e.g. "sndBufSize": "big") silently gives the default. Enable this test
// with the fix.
TEST(JSONParser, DISABLED_RejectsValuesOfTheWrongType)
// A typo (e.g. "sndBufSize": "big") must not silently give the default.
TEST(JSONParser, RejectsValuesOfTheWrongType)
{
EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"name": "c", "sndBufSize": "big"}]}]}})", "a"),
boost::property_tree::ptree_bad_data);
ParserError);
EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"name": "c", "autoBind": "maybe"}]}]}})", "a"),
boost::property_tree::ptree_bad_data);
ParserError);
EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"name": "c", "sockets": [{"linger": "1.5"}]}]}]}})", "a"),
ParserError);
}
TEST(JSONParser, File)

View File

@@ -91,10 +91,9 @@ TEST(SuboptParser, Errors)
EXPECT_THROW(SuboptParser({"name=data"}, ""), ParserError);
}
// Known defect: see JSONParser.DISABLED_RejectsValuesOfTheWrongType.
TEST(SuboptParser, DISABLED_RejectsValuesOfTheWrongType)
TEST(SuboptParser, RejectsValuesOfTheWrongType)
{
EXPECT_THROW(SuboptParser({"name=data,sndBufSize=big"}, "dev"), boost::property_tree::ptree_bad_data);
EXPECT_THROW(SuboptParser({"name=data,sndBufSize=big"}, "dev"), ParserError);
}
} // namespace