Repository navigation
Fix stack overflow in Avro schema inference on a deep chain of named types - #124644
Conversation
`AvroSchemaReader::avroNodeToDataTypeImpl` follows named type references, so its recursion depth is the length of the reference chain, not the JSON nesting that contrib/avro bounds with `MAX_AVRO_SCHEMA_DEPTH`. A valid file whose schema chains 100000 named records, each defined in its own array field, made `DESC file(..., Avro)` recurse ~200000 frames and overflow the stack (SIGSEGV on 26.8.18.2 server and on master, after ~35 s and 13 GB). The per-level `checkStackSize()` added by ClickHouse#102417 ended up in the non-recursive wrapper `avroNodeToDataType` when ClickHouse#102853 split the recursion into `avroNodeToDataTypeImpl`, so it ran once per root column. Count the recursion depth against `max_parser_depth` (0 = unlimited) and throw `TOO_DEEP_RECURSION`, as ClickHouse#107341 does for MsgPack, BSON, ORC, Parquet and JSON schema inference, and keep `checkStackSize()` per level as a backstop for a raised or unlimited limit. The limit is passed by all three callers (schema inference, the Variant read path and the Iceberg manifest reader), and `max_parser_depth` is added to the Avro/AvroConfluent schema cache key, so a schema inferred under a higher limit is not served to a query with a lower one (same as a839800 did for ORC). Co-Authored-By: Claude Opus 5.5 <[email protected]>
An explicit Variant column converts every union branch with avroNodeToDataType before matching it, so the depth limit passed there is reachable without schema inference. Add a file whose union has a deep named-record branch and read it as 'last Variant(Int64)': with the limit the read fails with TOO_DEEP_RECURSION, without it the branch is converted unbounded and the read fails with ILLEGAL_COLUMN instead. 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; 2 rounds, 6 findings) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261008-134456 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261008-130545 |
|
Workflow [PR], commit [71feabe] Summary: ✅
AI ReviewSummaryThis PR fixes the real stack-overflow root cause by moving Missing context / blind spots
Final Verdict✅ No new inline findings. From a code-review perspective this looks good to merge once the pending non-fast CI jobs finish. LLVM Coverage ReportMeasured on commit 71feabe.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
| const avro::NodePtr & node, std::unordered_set<std::string> & seen_names, bool allow_nullable_tuple_type, size_t max_depth, size_t depth) | ||
| { | ||
| /// Named type references are followed, so the depth is not bounded by the nesting of the schema JSON. | ||
| if (max_depth != 0 && depth > max_depth) |
There was a problem hiding this comment.
@groeneai why can't you use the checkStackSize() function?
There was a problem hiding this comment.
It does: this PR calls checkStackSize() on every level of avroNodeToDataTypeImpl (right below this check), and that alone fixes the crash. On master it runs only once, in the non-recursive wrapper avroNodeToDataType (26.3 still has it per level).
The max_parser_depth counter on top follows #107341 (the same counter plus a checkStackSize() backstop in MsgPack, BSON and Parquet schema inference). checkStackSize() throws only after half of the thread's stack is used (1/20 under TSan), so how deep a schema gets, and what it costs before the error, depends on the build and the thread's stack size. Measured with the 100000-level file from #123998, DESC file(..., Avro):
| Binary | Error | Time | Peak RSS |
|---|---|---|---|
official 26.3.42.3 (per-level checkStackSize(), no counter) |
Stack size too large |
8.8-10.8 s | 15.3 GiB |
this PR, debug, max_parser_depth = 0 (stack check only) |
Stack size too large |
7.6 s | 2.6 GiB |
| this PR, debug, default | nested deeper than the limit (1000) |
1.0 s | 0.56 GiB |
For reference, 26.3 reads the same file with an explicit structure in 1.1 s / 2.1 GiB. The counter also lets the test use a 40-level file with max_parser_depth = 50 instead of this one.
If you still prefer only checkStackSize(), I'll drop the counter and its plumbing (the max_depth parameter at the three call sites and the schema cache key).
There was a problem hiding this comment.
Yes, I prefer checkStackSize()
There was a problem hiding this comment.
Done in 71feabe: avroNodeToDataTypeImpl now calls checkStackSize() on every level, and the counter, the max_depth parameter and the cache key change are gone.
The test runs the deep chain under ulimit -s 1024: inference builds every field's type before the deepest one throws, so its cost grows with the stack size (15 GiB peak RSS on official 26.3 with an 8 MiB stack, 1.3 GiB with 1 MiB). With 1 MiB, master still crashes on a 10000-record chain (debug and official 26.8.18.2), and the fix throws in 0.4 s.
Per review, drop the max_parser_depth counter and its plumbing (the max_depth parameter at the three callers and the schema cache key). avroNodeToDataTypeImpl now calls checkStackSize() on every level instead of once in the non-recursive wrapper, which is what fixes the crash. The test now runs the deep case under a 1 MiB stack: inference builds every field's type before the deepest one throws, so its cost grows with the stack size (official 26.3, which has the same per-level check: 15 GiB peak RSS with an 8 MiB stack, 1.3 GiB with 1 MiB). With 1 MiB the unfixed build still crashes on a 10000-record chain (debug and official 26.8.18.2). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #124644 to 26.9: Fix stack overflow in Avro schema inference on a deep chain of named types
Backport #124644 to 26.8: Fix stack overflow in Avro schema inference on a deep chain of named types
… on a deep chain of named types
… on a deep chain of named types
Related: #123998
Related: #124053
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash (stack overflow) on an Avro schema with a long chain of named type references, in schema inference (
DESC, orSELECTwithout an explicit structure, forAvroandAvroConfluent), in reads into aVariantcolumn and in Iceberg manifest reading. Such a schema now fails with an error instead.Description
On 26.8.18.2,
DESC file('deep.avro', Avro)over a valid 12 MB file whose schema chains 100000 named records (A_k { x: A_(k-1) }, each defined in its own array field) crashes the server with a stack overflow. It is the input of #123998, whose header-parsing half was fixed in contrib/avro by #124053; this is the schema-inference half, insrc/.AvroSchemaReader::avroNodeToDataTypeImplfollows named type references, so its recursion depth is the length of the reference chain, which contrib'sMAX_AVRO_SCHEMA_DEPTHdoes not bound. The per-levelcheckStackSize()added by #102417 stayed in the non-recursive wrapper when #102853 split the recursion out, so it runs once per column. 26.3 is not affected; 26.4 to 26.9 and master are.avroNodeToDataTypeImplnow callscheckStackSize()on every level, as on 26.3, so all three callers (schema inference, the Variant read path, the Iceberg manifest reader) getTOO_DEEP_RECURSIONinstead of a stack overflow.Validation (debug build): the 100000-chain file goes from SIGSEGV after 35 s and 13 GB RSS to an error caused by
TOO_DEEP_RECURSIONafter 10 s and 3.3 GB. Inference builds every field's type before the deepest one throws, so its cost grows with the stack size (15 GiB peak RSS on official 26.3 with an 8 MiB stack). The test therefore runs a 10000-record chain underulimit -s 1024: master crashes there (debug and official 26.8.18.2), the fix throws in 0.4 s; a 40-record chain still infers. It fails on master and passes with the fix (30/30 runs).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124644]
Version info
26.10.1.1889-master(included in26.10and later)