root
root copied to clipboard
RNTuple: fields with mixed STL types sometimes fail to be filled
Check duplicate issues.
- [X] Checked for duplicates
Description
When having fields with mixed STL types sometimes you run into issues when trying to fill them. I've attached an example below.
Reproducer
This is a minimal script that shows that issue.
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleWriter.hxx>
#include <TSystem.h>
#include <vector>
#include <variant>
#include <optional>
using RNTupleModel = ROOT::Experimental::RNTupleModel;
using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
void ntpl_issue()
{
auto model = RNTupleModel::Create();
auto fldVvar = model->MakeField<std::vector<std::variant<std::optional<int>,float>>>("vvar");
auto ntuple = RNTupleWriter::Recreate(std::move(model), "F", "ntpl_issue.root");
for (int i = 0; i < 10; i++) {
fldVvar->clear();
for (int j = 0; j < 5; ++j) {
std::variant<std::optional<int>,float> var(1.0);
fldVvar->emplace_back(var);
}
ntuple->Fill();
}
}
And this is the error that it produces.
Fatal: (typedValue->size() % fItemSize) == 0 violated at line 2432 of `.../root_src/tree/ntuple/v7/src/RField.cxx'
aborting
Another way to get it to fail is by using std::vector<std::variant<std::atomic<int>,float>>
.
ROOT version
6.33/01 (commit eef2244)
Installation method
Built from source
Operating system
macOS
Additional context
I found this issue while trying to generate std::variant
types in an invalid state by doing something like this.
struct S {
operator int() { throw 42; }
};
std::variant<int,float> var;
try {
var = S();
} catch (int) {}
fldVvar->emplace_back(var);
The spec indicates that invalid states are supported, as shown in the line below. I wanted to ask what's the reason for them being supported given that they are not really supposed to be "legal", and it seems like one can't successfully generate an RNTuple with an invalid state. It either ends up initializing the first variant or it crashes.
https://github.com/root-project/root/blob/95307116ca3dd811ac1b5e496ad7f9828402dc51/tree/ntuple/v7/doc/specifications.md?plain=1#L780
Root cause may actually be a bug in the std::variant
field. But needs more investigation.
I cannot reproduce it on Linux, it could be a macOS specific issue.
The PR contains a test that demonstrates that user code can produce empty variants, which the I/O system needs to handle.