Repository navigation
Reset the MsgPack parser between streamed inputs - #128943
Conversation
StreamingFormatExecutor reuses one input format for many small inputs (entries of one async insert batch, Kafka/RabbitMQ/NATS messages, FileLog chunks) and calls resetParser() between them, also after a parse error. MsgPackRowInputFormat::resetParser() reset only the visitor's info_stack, not the msgpack-c parser (parse_helper), which is resumable by design and keeps m_cs, m_trail and m_stack when an object is cut short by EOF or by a visitor exception. The next input was then parsed as the continuation of the broken object: - a truncated array made end_array_item() dereference the empty array_size of column 0: LOGICAL_ERROR abort in debug builds, a libc++ hardening abort in release builds; - a truncated scalar consumed the next input's bytes as the rest of the value (four rows 1,2,3,4 became one row 16909060); - a type error inside an array made the next valid input fail too. resetParser() now calls parser.init(), msgpack-c's own reset, which also calls the visitor's init() (renamed from reset(), the name msgpack calls). For valid input this is a no-op: a complete object always leaves the parser in its header state with an empty stack. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Name parse_helper::init as f, not f(), per the in-tree style rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings; 1 full pass), plus my own cold review of the diff (1 nit).
Checked and not raised: the fix resets all of msgpack-c's cross-call parser state ( Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261103-111500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261103-104700 |
|
cc @PedroTadim @Avogar, could you review this and add the |
|
Workflow [PR], commit [c84c72b] Summary: ✅
AI ReviewSummaryReviewed PR #128943 at Missing context / blind spots
Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
77c29e3
Backport #128943 to 26.8: Reset the MsgPack parser between streamed inputs
Backport #128943 to 26.9: Reset the MsgPack parser between streamed inputs
Backport #128943 to 26.10: Reset the MsgPack parser between streamed inputs
Backport #128943 to 26.3: Reset the MsgPack parser between streamed inputs
…self StreamingFormatExecutor (async insert flushes, Kafka, Kafka2, NATS, RabbitMQ, FileLog) kept one input format object for many independent inputs and called resetParser() between them, trusting every format to bring itself back to the state of a fresh object. Nothing enforced that: 32 of the 82 input format names kept some per-input field (One `done`, Npy `read_rows`, MySQLDump `table_name` and column mapping, SQLite flags and database, JSON `parse_as_json_each_row`, DWARF section extractors, TSV `first_row`, CustomSeparated `columns` and the detected header, RowBinaryWithNamesAndTypes `read_data_types`, Template `row_num`), so a later input of an async insert batch or a later message lost rows, was rejected, or was parsed with the previous input's state. Per-format resetParser fixes (ClickHouse#128943, ClickHouse#129109, ClickHouse#129140) leave the default unsafe for the next format and the next field. Invert the default: a format is reused only when its name is registered with FormatFactory::markInputFormatSupportsResetParser; any other format is released after each input and created anew for the next one. The executor now takes a format creator and an AddingDefaultsTransform creator (the transform holds a reference to the format). The streaming engines compute their single-threaded parser resources once and share them between created formats. Marked: the 50 names whose reset is complete and 26 hot names completed here with one statement each (TSV first_row, CustomSeparated columns, WithNamesAndTypes is_header_detected, RowBinary read_data_types, Template row_num). Reuse matters for them: a fresh CapnProto, Template, Protobuf, Regexp or AvroConfluent object reads or compiles schemas. Not marked, so re-created with no per-format code: One, Npy, MySQLDump, SQLite, JSON, DWARF. DWARF's incomplete resetParser is deleted. 02187_async_inserts_all_formats now also inserts every format as one batch of two different inputs, plus targeted cases for the leaks that a same-shaped pair does not show, and a FileLog case. Closes ClickHouse#122351 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server abort, wrong rows, or a rejected valid row when a
MsgPackinput that ends in the middle of an object is followed by another input parsed by the same format, as with the entries of one asynchronous insert batch or the messages of a streaming engine such asKafkawithkafka_handle_error_mode = 'stream'.Description
No related open issue found. On the official
26.10.2.103image, two asynchronousMsgPackinserts intoCREATE TABLE ai (id UInt64, arr Array(UInt8)) ENGINE = MergeTree ORDER BY id, a truncated one followed by a valid one, abort the server at the batch flush:Release builds die with
libc++ Hardening assertion this->has_value() failed: optional operator* called on a disengaged value, debug builds withLogical error: 'info_stack.top().array_size.has_value()'. A truncateduint32(\xce\x00) followed by the rows1, 2, 3, 4inserts the one wrong row16909060instead.StreamingFormatExecutorreuses one input format for all entries of a batch (and for Kafka, RabbitMQ, NATS and FileLog messages) and callsresetParserbetween them, also after a parse error.MsgPackRowInputFormat::resetParserreset only the visitor, not the msgpack-c parser, which keeps its open containers and its partly read value when an object is cut short. The next input was parsed as the continuation of the broken one.resetParsernow callsparser.init(), msgpack-c's own reset, which also calls the visitor'sinit(renamed fromreset). For valid input it is a no-op.Npyhas a different problem in the same setting: it keeps its row counter across inputs, so the secondNpyentry of a batch is dropped; not addressed here.The new test covers a truncated scalar, a type error inside an array and a truncated array: on master it gives a wrong row, a rejected row and an abort; with the fix it passes 50/50 with randomized settings.
Workflow [PR]
Sync PR [sync-upstream/pr/128943]
Version info
26.11.1.1241-master(included in26.11and later)