Skip to content

Fix server abort in ExtremesTransform when transform throws mid-population - #105264

Merged
Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-extremes-transform-partial-populate-on-exception
May 20, 2026
Merged

Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-extremes-transform-partial-populate-on-exception

Conversation

@groeneai

@groeneai groeneai commented May 18, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

ExtremesTransform::transform, on the first chunk, resized extremes_columns to N null entries and filled them column by column. If anything inside that loop threw, the member was left half-populated: leading indices held size-2 columns, the failing index held a column with fewer than 2 rows, and trailing indices were still null.

The exception flowed downstream through ISimpleTransform::work's catch block. During pipeline shutdown the input port was closed, so ExtremesTransform::prepare advanced ISimpleTransform::prepare to Status::Finished, set finished_transform = true, and then returned Ready because !extremes && !extremes_columns.empty(). work then called extremes.setColumns(std::move(extremes_columns), 2). The row-count check inside Chunk::checkNumRowsIsConsistent spotted the short column, tried to format the error message via Chunk::dumpStructure, dereferenced one of the still-null intrusive pointers, and aborted the server with Assertion 'px != 0' failed (STID: 2286-2cea) on boost::intrusive_ptr<IColumn>::operator->.

In the AST fuzzer report at #104000 the trigger was MEMORY_LIMIT_EXCEEDED raised by insert while copying a 1 MiB FixedString value under a 4 MiB max_memory_usage budget, but the bug applies to any exception inside the loop.

Reproducer

Reproduces deterministically on a debug build:

SELECT DISTINCT 1025, toFixedString('%', 1048576), 100
UNION ALL
SELECT 9223372036854775807, '\0', materialize(toLowCardinality(1025))
SETTINGS extremes = 1, max_memory_usage = '4Mi'
FORMAT Null;

Before the fix: server aborts with Assertion 'px != 0' failed. After the fix: client receives MEMORY_LIMIT_EXCEEDED and the server stays up.

Fix

Build into a local vector and move it into extremes_columns only on success. A throw during construction destroys the local vector on stack unwind and leaves extremes_columns in its documented "no chunk processed yet" empty state, so work no longer materialises a partial chunk. The exception propagates downstream normally and the client sees the original error code.

The else branch (subsequent chunks) was already exception-safe — it writes through a local MutableColumnPtr and only moves into extremes_columns[i] after both insert calls succeed — so no change is needed there.

Tests

Added 04256_extremes_transform_partial_populate_on_exception.sql. It runs the reproducer above and asserts the original failure path produces MEMORY_LIMIT_EXCEEDED, then issues a follow-up SELECT 'alive' to confirm the server is still serving queries.

Reference

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):

Fix server abort with Assertion 'px != 0' failed in ExtremesTransform when a query with extremes = 1 hits an exception (for example MEMORY_LIMIT_EXCEEDED) while the transform is building its extremes columns from the first chunk. The client now receives the original exception instead of crashing the server.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.889

…ation

`ExtremesTransform::transform` resized `extremes_columns` to `N` null entries
on the first chunk and filled them one column at a time. If any step inside the
loop threw — for example `MEMORY_LIMIT_EXCEEDED` raised by `insert` while
copying a large value into the freshly cloned column — the member was left in
a half-populated state: leading indices held size-2 columns, the failing index
held a column with fewer than 2 rows, and trailing indices were still null.

The exception flowed downstream via `ISimpleTransform::work`'s `catch` block,
the input port was closed during pipeline shutdown, and
`ExtremesTransform::prepare` then ran `ISimpleTransform::prepare` to
`Status::Finished`, set `finished_transform = true`, and returned `Ready`
because `!extremes && !extremes_columns.empty()`. `work` then called
`extremes.setColumns(std::move(extremes_columns), 2)`. The row-count check
inside `Chunk::checkNumRowsIsConsistent` spotted the short column, tried to
format the error via `Chunk::dumpStructure`, dereferenced one of the still-null
intrusive pointers, and aborted the server with
`Assertion 'px != 0' failed (STID: 2286-2cea)` on
`boost::intrusive_ptr<IColumn>::operator->`.

The fix builds into a local vector and moves into the member only on success.
A throw during construction destroys the local vector during stack unwinding
and leaves `extremes_columns` empty, which is the documented "no chunk
processed yet" state. `work` no longer materialises a partial chunk and the
client receives a normal `MEMORY_LIMIT_EXCEEDED` (or whatever the original
exception was) instead of a server crash.

Reference CI report:
https://s3.amazonaws.com/clickhouse-test-reports/PRs/104000/66e1bac24c924afddb3c58be48a8174b37f0f2e3/ast_fuzzer_amd_debug/
Reported via PR: ClickHouse#104000

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

Copy link
Copy Markdown
Collaborator Author

cc @KochetovNicolai @vdimir — could you take a look? This fixes a server-abort in ExtremesTransform::transform when an exception (e.g. MEMORY_LIMIT_EXCEEDED) fires mid-way through populating extremes columns from the first chunk. The reproducer is in the description. AST fuzzer report came from #104000 (STID 2286-2cea).

@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label May 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3020cff]

Summary: ❌

job_name test_name status info comment
Bugfix validation (functional tests) FAIL
04256_extremes_transform_partial_populate_on_exception FAIL cidb IGNORED
Integration tests (arm_binary, distributed plan, 1/4) FAIL
test_global_overcommit_tracker/test.py::test_global_overcommit FAIL cidb IGNORED

AI Review

Summary

This PR fixes an exception-safety hole in ExtremesTransform::transform by constructing first-chunk extremes_columns in a local vector and committing only after successful full population. That preserves the invariant that extremes_columns is either empty or fully valid, preventing the downstream abort path seen when a mid-loop exception leaves null entries. The added stateless regression test exercises memory-limit-triggered exceptions across multiple budgets and confirms the server remains alive. I did not find unresolved correctness, safety, or metadata issues in the current PR state.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 18, 2026
@Avogar Avogar self-assigned this May 19, 2026
Comment on lines +25 to +29
SELECT DISTINCT 1025, toFixedString('%', 1048576), 100
UNION ALL
SELECT 9223372036854775807, '\0', materialize(toLowCardinality(1025))
SETTINGS extremes = 1, max_memory_usage = '4Mi'
FORMAT Null; -- { serverError MEMORY_LIMIT_EXCEEDED }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my local setup this doesn't reproduce the abort, but 10MB memory limit does. Let's either set it to 10 or make a few queries with different memory limit

Per @Avogar review: 4Mi did not reproduce the abort in his setup;
10Mi did. Add four variants spanning 4Mi to 10Mi so at least one hits
the partial-population path on each build (debug/release/sanitizer).
@groeneai

Copy link
Copy Markdown
Collaborator Author

Addressed in 3020cff. Test now runs four variants spanning 4Mi to 10Mi (FixedString sized to ~limit/4 so MEMORY_LIMIT_EXCEEDED is reliably reached). Locally, all four trigger the abort path on the unfixed binary in my env (debug); your 10Mi variant should hit it in yours. With the fix, each variant throws MEMORY_LIMIT_EXCEEDED cleanly and the server stays alive, returning alive at the end.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (TASK.md Phase 4):

a) Deterministic repro: clickhouse-client -q "SELECT DISTINCT 1025, toFixedString('%', 1048576), 100 UNION ALL SELECT 9223372036854775807, '\0', materialize(toLowCardinality(1025)) SETTINGS extremes = 1, max_memory_usage = '4Mi' FORMAT Null" aborts the unfixed server 100% in my env.
b) Root cause: ExtremesTransform::transform resized extremes_columns to N nulls, then filled column-by-column. A throw mid-loop left some entries null. The next work() saw a row-count mismatch, formatted the error via Chunk::dumpStructure, and dereferenced a null boost::intrusive_ptr<IColumn>, aborting on Assertion 'px != 0' failed.
c) Fix matches cause: Build into a local MutableColumns, then std::move into the member on success. A mid-loop throw destroys the local vector; the member stays empty (invariant: empty OR fully populated).
d) Test intent: Test asserts the server stays alive after each variant errors with MEMORY_LIMIT_EXCEEDED. Multi-variant memory-limit spread covers different env overheads (4Mi for low-overhead debug, 10Mi for higher-overhead release/sanitizer per @Avogar).
e) Both directions verified locally:

  • Unfixed binary: 4Mi/1MiB and 6Mi/1.5MiB variants abort the server (Assertion px != 0' failed). 8Mi/2MiB and 10Mi/2.5MiB hit MEMORY_LIMIT_EXCEEDED upstream in my low-overhead debug env (still serve as cover for higher-overhead envs).
  • Fixed binary: all four variants throw MEMORY_LIMIT_EXCEEDED cleanly, server stays alive, SELECT 'alive' returns alive.
    f) General fix, not narrow patch: The fix preserves the "build then commit" invariant for the whole loop, not just one branch. Symmetric ColumnConst / extremes branches both go through the local vector. No similar pattern exists in sibling Transforms (verified via grep on extremes_columns.resize).

Session: cron:clickhouse-ci-task-worker:20260519-161500

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (10/10) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue May 20, 2026
Merged via the queue into ClickHouse:master with commit 6b0db68 May 20, 2026
162 of 165 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.4, 26.3 (LTS), 26.2, 25.8 (LTS)? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: ExtremesTransform::transform exception safety during first-chunk extremes population — core code present in all supported branches.

Why this might apply: This is a server crash bug (null pointer dereference via dumpStructure when exception occurs during extremes population). The ExtremesTransform code predates all supported branches (git log shows it was added in the 2019 dbms/ → src/ rename). Any user with extremes=1 who triggers MEMORY_LIMIT_EXCEEDED during first chunk processing could hit this crash.

Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider pr-must-backport or a version label like v26.4-must-backport.

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…mes-transform-partial-populate-on-exception

Fix server abort in ExtremesTransform when transform throws mid-population
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 pr-bugfix Pull request with bugfix, not backported by default 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