Skip to content

Fix a segfault in Pretty* and JSONColumns output when a memory limit is hit while the result is accumulated - #130211

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:chunk-mutate-columns-empty-on-throw
Nov 9, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:chunk-mutate-columns-empty-on-throw

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125015

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a segmentation fault in clickhouse-local and in the client embedded into the server (SSH, web terminal) when a format that accumulates the result (Pretty* with the default output_format_pretty_squash_consecutive_ms, *MonoBlock, JSONColumns, JSONCompactColumns) hit a memory limit while copying it. The query now fails with MEMORY_LIMIT_EXCEEDED.

Description

On 26.10.2.103 (also 26.8.34.13 and 26.3.60.2), clickhouse local with FORMAT PrettyCompactMonoBlock and max_memory_usage = '20Mi' over a Memory table of two 40 MB blocks segfaults instead of failing with MEMORY_LIMIT_EXCEEDED. In CI the same crash killed the server in the Stress test (amd_tsan) job of #125015, through the web-terminal client embedded in the server.

Chunk::mutateColumns moved each column into IColumn::mutate, which takes it by value, and cleared the chunk only after the loop. When mutate had to clone a shared column and the allocation threw, the chunk kept its row count with null columns. Pretty and JSONColumns accumulate the result with Chunk::append, keep that chunk after the exception and render it at finalize (or from the Pretty squash thread), dereferencing a null column.

mutateColumns now detaches the columns first and mutates the detached copies, so the chunk is empty whenever something throws. The success path is unchanged.

The new test 05615_formats_accumulating_result_memory_limit_in_local (PrettyCompactMonoBlock, PrettyCompact with the squash thread, JSONColumns) fails on master for all three formats and passes with the fix, 50/50 runs.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/130211]

Version info

  • Merged into: 26.11.1.1719-master (included in 26.11 and later)

Chunk::mutateColumns moved each column into IColumn::mutate, which takes
the column by value, and cleared the chunk only after the loop. When
mutate had to clone a shared column and the allocation threw (for example
MEMORY_LIMIT_EXCEEDED), the chunk kept its num_rows and columns.size()
with null columns.

PrettyBlockOutputFormat and JSONColumnsBlockOutputFormatBase accumulate the
result with mono_chunk.append(chunk) and keep mono_chunk after the
exception; the Pretty squash thread or writeSuffix at finalize then render
it and dereference a null column. In the stress test this killed the
server through the web-terminal client embedded in it (interface Local,
memory_tracker_fault_probability, columns shared with the columns cache).
It reproduces in clickhouse-local on 26.3, 26.8 and 26.10 releases.

Detach the columns first, then mutate the detached copies: the chunk is
empty whenever anything throws, and the success path is unchanged (same
use counts handed to mutate, same final state).

The new test covers PrettyCompactMonoBlock, PrettyCompact with the squash
thread, and JSONColumns: a Memory table hands its stored blocks to the
reader without copying, and each block is larger than max_memory_usage,
so cloning the first block on append is the allocation that fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (1 finding).

# Sev Finding Verdict Evidence / action
1 💡 Use Critical Bug Fix instead of Bug Fix DISAGREE Critical Bug Fix is left to a maintainer's call; Bug Fix holds: 26.10.2.103, 26.8.34.13 and 26.3.60.2 segfault on the repro
2 💡 ArrowIPCBlockOutputFormat::staged (opt-in output_format_arrow_record_batch_size) also keeps an appended chunk AGREE covered by the same change (the chunk is empty after a throw and is skipped at finalize); no code change

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261108-210200

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse local --tmp: a Memory table with two 5M-row UInt64 blocks, SELECT * ... FORMAT <f> SETTINGS max_threads = 1, max_memory_usage = '20Mi'. Unfixed binary dies for PrettyCompactMonoBlock, PrettyCompact (squash thread) and JSONColumns, every run.
b Root cause explained? Chunk::mutateColumns moved each column into IColumn::mutate (by value) and cleared the chunk only after the loop; a throwing clone of a shared column left the row count with null columns, which Pretty/JSONColumns keep in mono_chunk and render later (gdb catch throw: clone in IColumn::mutate <- Chunk::mutateColumns).
c Fix matches root cause? Yes. mutateColumns detaches the columns before anything can throw, so a chunk never holds a null column; no guard in the formats, no try/catch.
d Test intent preserved / new tests added? New 05615_formats_accumulating_result_memory_limit_in_local.sh covering the three accumulating paths (*MonoBlock, squash thread, JSONColumns).
e Both directions demonstrated? Unfixed (a66f7602): test FAILS for all three formats (releases 26.10.2.103/26.8.34.13/26.3.60.2 segfault; ASan+UBSan reports the CI line Chunk.cpp:65:21). Fixed (b25c1130): MEMORY_LIMIT_EXCEEDED, test passes 50/50.
f Fix is general across code paths? Yes. One change covers every holder of an appended chunk (Pretty*, *MonoBlock, JSONColumns, JSONCompactColumns, projections) and every other mutateColumns caller; Block::mutateColumns may hold null header columns by design.
g Fix generalizes across inputs (params/datatypes/wrappers)? Type-independent: IColumn::mutate recurses into subcolumns of any wrapper, and a throw anywhere now happens while the columns are detached locals.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; only the exception path changes (empty chunk instead of null columns).
i Invariants and contracts preserved? The Chunk invariant (no null column, num_rows rows per column) now also holds after an exception; unit tests for processors/merging/joins: 407 passed, 1 skipped.

Session id: cron:clickhouse-impl-slot-41:20261108-194800

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @yariks5s @tavplubix, could you review this and add the can be tested label so CI runs? Chunk::mutateColumns moved each column into IColumn::mutate and cleared the chunk only afterwards, so a MEMORY_LIMIT_EXCEEDED while cloning a shared column left a chunk with its row count and null columns, which Pretty* and JSONColumns kept and later rendered (segfault); the columns are now detached before anything can throw.

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [af0d707]

Summary: ✅


AI Review

Summary

The PR fixes a segmentation fault during result accumulation by detaching columns before Chunk::mutateColumns performs potentially throwing column mutations. The new regression test covers PrettyCompactMonoBlock, threaded PrettyCompact, and JSONColumns. No actionable correctness, safety, or compatibility findings were identified in head af0d707ea8fec591726cc5962ad11c796d2fd403.

Missing context / blind spots
  • ⚠️ No built ClickHouse binary was available locally, so the regression was not independently executed. The author reports failures before the fix and 50 successful runs after it; completed stateless and sanitizer CI results would provide independent runtime confirmation.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing af0d707ea with master af71c9ecd (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 9, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ClickHouse:master with commit 398817d Nov 9, 2026
375 of 377 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 9, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 9, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 9, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 9, 2026
Backport #130211 to 26.8: Fix a segfault in `Pretty*` and `JSONColumns` output when a memory limit is hit while the result is accumulated
clickhouse-gh Bot added a commit that referenced this pull request Nov 9, 2026
Backport #130211 to 26.10: Fix a segfault in `Pretty*` and `JSONColumns` output when a memory limit is hit while the result is accumulated
clickhouse-gh Bot added a commit that referenced this pull request Nov 9, 2026
Backport #130211 to 26.9: Fix a segfault in `Pretty*` and `JSONColumns` output when a memory limit is hit while the result is accumulated
PedroTadim added a commit that referenced this pull request Nov 9, 2026
Backport #130211 to 26.3: Fix a segfault in `Pretty*` and `JSONColumns` output when a memory limit is hit while the result is accumulated
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s` output when a memory limit is hit while the result is accumulated
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s` output when a memory limit is hit while the result is accumulated
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s` output when a memory limit is hit while the result is accumulated
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…ns` output when a memory limit is hit while the result is accumulated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants