Skip to content

Backport #108493 to 25.8: Fix crash on deeply nested expressions with raised max_parser_depth - #108896

Merged
Algunenano merged 2 commits into
25.8from
backport/25.8/108493
Jul 1, 2026
Merged

Algunenano merged 2 commits into
25.8from
backport/25.8/108493

Conversation

@robot-clickhouse

@robot-clickhouse robot-clickhouse commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Original pull-request #108493
Cherry-pick pull-request #108828

This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.

The PR source

The PR is created in the CI job

Version info

  • Merged into: 25.8.26.7

@robot-clickhouse robot-clickhouse added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix labels Jun 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [680c1a3]

`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.

`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear.

This is needed for the backported test `04412_deep_nested_literal_format`.
It is tagged `no-fasttest, no-debug, no-sanitizers`, but the stress test job
deliberately ignores `no-{build}` tags (every test must run under stress), so
`04412` runs under `Stress test (amd_tsan)`. With the quadratic parse, building
the depth-20000 literal in `formatQuery` took ~1620s under ThreadSanitizer and,
being non-cancellable, tripped the hung-check ("Hung check failed, possible
deadlock found"). The same quadratic parse also made `04412` run for ~441s under
`Stateless tests (amd_asan, distributed plan, parallel)` and the server got
killed by signal. With the linear parse the literal is built quickly and
`formatQuery` still reports `TOO_DEEP_RECURSION` (code 306) from the
`FieldVisitorToString` stack guard, exactly as the test expects.

This mirrors the fix already applied to the 26.6 backport in
#108832 and the master change
#108892.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108896&sha=0a636f95b8f50b19259d8d0280d26e62639e47a4&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Pushed 680c1a359cb — Parse nested literal collections in linear time.

Failures addressed by this commit

Stress test (amd_tsan) and Stateless tests (amd_asan, distributed plan, parallel, 2/2) both failed because of 04412_deep_nested_literal_format. ParserCollectionOfLiterals::parseImpl folded each finished nested collection into its parent with layers.back().arr.push_back(literal->value), deep-copying the whole accumulated nested Field. For a deeply nested literal ([[[ … ]]], depth 20000) this is quadratic in the depth:

  • under ThreadSanitizer, building the literal in formatQuery took ~1620s and, being non-cancellable, tripped the hung-check ("Hung check failed, possible deadlock found");
  • under ASan it ran ~441s and the server was killed by signal.

literal is a local that is discarded right after this line (only the outermost layer is kept as node), so moving its value into the parent is safe and makes the parse linear. formatQuery still reports TOO_DEEP_RECURSION (code 306) from the FieldVisitorToString stack guard, exactly as the test expects.

This mirrors the fix already applied to the 26.6 backport in #108832 (which then went green on Stress test (amd_tsan)) and the master change #108892.

Unrelated failure

Integration tests (amd_tsan, 4/6) — test_async_load_databases/test.py::test_materialized_views_cascaded_multiple (assert '42,0' == '42,2', a cascaded-MV / async-load ordering race) and ::test_materialized_views_replicated (DATABASE_ALREADY_EXISTS on CREATE DATABASE test_mv, a cascading consequence of the first test failing before cleanup). This PR only touches the parser and Field stack-size guards — nothing related to materialized views, async database loading, or replicated databases.

This is a master-wide flake: over the last 14 days each test failed 13 times across 9 distinct PRs, 5 of them on master itself (pull_request_number = 0), first seen 2026-06-18, last seen today.

@groeneai, please investigate the flaky test_async_load_databases materialized-view tests (see the CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108896&sha=0a636f95b8f50b19259d8d0280d26e62639e47a4&name_0=BackportPR&name_1=Integration%20tests%20%28amd_tsan%2C%204%2F6%29) and provide a fix in a separate PR. If a fix is already in progress, please link it here. There is no open tracking issue for it yet.

@alexey-milovidov

Copy link
Copy Markdown
Member

On the current head 680c1a359cb, the only remaining red is Integration tests (amd_asan, old analyzer, 3/6):

Four tests failed, all in the same parallel worker (integration_run_parallel_0.log), with two distinct infrastructure-level causes:

  • test_restore_replica/test.py::test_restore_replica_parallel, ::test_restore_replica_sequential, ::test_restore_replica_keeps_duplicate_parts — Code: 210. DB::NetException: Connection refused (172.16.2.7:9000). (NETWORK_ERROR) (a server node became unreachable).
  • test_distributed_ddl_parallel/test.py::test_smoke_parallel_dict_reload — Code: 159. DB::Exception: ... Pipe read timeout exceeded 10000 milliseconds: While executing ParallelParsingBlockInputFormat: While executing ShellCommandSource. (TIMEOUT_EXCEEDED) (an executable-dictionary shell command timed out, typical under ASan slowness).

This is unrelated to the change: this PR only adds checkStackSize() guards to the recursive Field/IAST visitors and makes nested-literal parsing linear — nothing that touches replica restoration, distributed DDL, or executable dictionaries. The same tests are an established flake on master CI: over the last 14 days they failed on unrelated PRs as well — #107566 (llvmlibc-mem-functions, amd_msan) and #107652 (restore-backup-no-dedup-rmt, Bugfix validation) — so they are not specific to this branch. There is no open tracking issue for them yet.

@groeneai, please investigate the flaky test_restore_replica / test_distributed_ddl_parallel::test_smoke_parallel_dict_reload failures (see the CI report above) and provide a fix in a separate PR. If a fix is already in progress, please link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov thanks, investigated both. Fix PR: #108933 (off master, test-only).

Both are CI-only flakes (0 master failures in 30 days; hits on unrelated PRs #107442, #107566, #107652, and here). No tracking issue existed. Root causes:

  • test_smoke_parallel_dict_reload (Code: 159 Pipe read timeout exceeded 10000 ms ... ShellCommandSource): the slow_dict_3/slow_dict_7 executable dictionaries use the default 10s command_read_timeout. 30 concurrent RELOAD DICTIONARY ON CLUSTER each spawn a sleep command; under sanitizer slowdown the server fails to read the command pipe within 10s, even though the command only sleeps 3-7s. Fix: set command_read_timeout=60000 on both dicts. The 30-thread queueing coverage (above cluster_b.pool_size=20) is preserved, and the commands never approach 60s so nothing is masked. Reproduced deterministically: command_read_timeout=2000+sleep 3 gives the exact error; 60000 passes.

  • test_restore_replica (Code: 210 Connection refused (NETWORK_ERROR)): the mutating queries (SYSTEM RESTART/RESTORE/SYNC REPLICA, INSERT) had no connection-level retry, so a transiently-unreachable node under sanitizer load fails the test (sibling subtests in the same run pass, so the node recovers). Fix: retry only on "Connection refused", which means the TCP connection was rejected and the query never reached the server, so it is safe even for the non-idempotent statements here.

These are pre-existing flakes unrelated to the backport, so the fix is on master only, not this branch.

@Algunenano
Algunenano merged commit 138a51e into 25.8 Jul 1, 2026
31 of 34 checks passed
@Algunenano
Algunenano deleted the backport/25.8/108493 branch July 1, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants