Skip to content

Fix segfault in ReverseIndex::insert after an exception while building the index - #128291

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-reverse-index-build-exception-safety
Nov 2, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-reverse-index-build-exception-safety

Conversation

@thevar1able

@thevar1able thevar1able commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix a segmentation fault in ReverseIndex::insert when an exception (typically MEMORY_LIMIT_EXCEEDED) was thrown while a LowCardinality dictionary was building its reverse index and the dictionary was then reused, for example by the next entry of an asynchronous insert flush.

Description

ReverseIndex::buildIndex assigned index before saved_hash = calcHashes(). If the allocation in calcHashes threw, the object was left with a non-null hash table and a null saved_hash. StreamingFormatExecutor handles the exception (an asynchronous insert flush rolls the columns back and continues with the next entry using the same result columns), and the next insert into the same LowCardinality(String) dictionary dereferenced the null saved_hash at ReverseIndex.h:506, a segmentation fault at address 0x18. Every observed occurrence happened on a server that was over its total memory limit, with Failed parsing for insert query id ... memory limit exceeded logged right before the crash.

The fix builds the hash table and the saved hashes into locals and publishes them only at the end. It also drops the index when an exception escapes insert, because the hash table may then reference a row that insertData failed to add; it is rebuilt from the column on the next use.

A ONCE failpoint inside buildIndex and a unit test reproduce the scenario. A stateless test is not possible here: the row input format builds a fresh dictionary per chunk and would consume a one-shot failpoint before the result column does.

🤖 Generated with Claude Code


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

Version info

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

…safe

`ReverseIndex::buildIndex` assigned `index` before `saved_hash = calcHashes()`.
If the allocation in `calcHashes` threw (in production: `MEMORY_LIMIT_EXCEEDED`
on a server over its total memory limit), the object was left with a non-null
hash table and a null `saved_hash`. The exception was handled by
`StreamingFormatExecutor` (asynchronous insert flush rolls the columns back and
continues with the next entry using the same result columns), and the next
`insert` into the same `LowCardinality(String)` dictionary dereferenced the null
`saved_hash` at `ReverseIndex.h:506`, a segmentation fault at address 0x18.

Build the hash table and the saved hashes into locals and publish them only at
the end. Also drop the index when an exception escapes `insert`, because the
hash table may then reference a row that `insertData` failed to add; it is
rebuilt from the column on the next use.

Add a `ONCE` failpoint inside `buildIndex` and a unit test that reproduces the
scenario: the first insert fails, the dictionary is reused, later inserts must
work. A stateless test is not possible here because the row input format builds
a fresh dictionary per chunk and consumes a one-shot failpoint before the result
column does.

Incident: ClickHouse/clickhouse-core-incidents#1422

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5959f2e]

Summary: ✅


AI Review

Summary

Reviewed #128291 at 5959f2eb93138c735aa0b4e4943c6fa32a149fcb. The change publishes the reverse index and saved hashes together, discards inconsistent index state after insertion failures, and restores uncommitted string bytes before dictionary reuse. The previous string-corruption finding is addressed by the follow-up commit and focused regression coverage; its thread has been resolved. No remaining findings.

Missing context / blind spots
  • ⚠️ The two regression tests were inspected but could not be executed locally because this checkout has no configured build. The head's CI unit-test report contained no test results at review time; a completed run of the ReverseIndex tests would close this validation gap.
Final Verdict

✅ Approve — no outstanding code-review findings. Runtime validation remains with CI.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Oct 30, 2026
Comment thread src/Columns/ReverseIndex.h Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). label Oct 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5959f2eb9 with master 2eafca8a6 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 9f79cc172; 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.

Object file sizes

4 object files changed (+44.56 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Columns/ColumnUnique.cpp.o 2.90 MiB 2.95 MiB +43.85 KiB (+1.48%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

…appending them

`ColumnString::insertData` appends to `chars` before growing `offsets`. If the
latter throws, the bytes of the row that was never added stay in `chars`, and
after the index is rebuilt they become a prefix of the next inserted value.
Trim `chars` back to the last offset in the recovery path.

Add a unit test that fails the `offsets` growth through the memory tracker,
reuses the dictionary and checks the stored values and lookups.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
@thevar1able
thevar1able marked this pull request as ready for review November 2, 2026 06:51
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 2, 2026
Merged via the queue into master with commit d488259 Nov 2, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-reverse-index-build-exception-safety branch November 2, 2026 13:11
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 2, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 2, 2026
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
Cherry pick #128291 to 26.3: Fix segfault in `ReverseIndex::insert` after an exception while building the index
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
…r an exception while building the index
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
Cherry pick #128291 to 26.8: Fix segfault in `ReverseIndex::insert` after an exception while building the index
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
…r an exception while building the index
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
Cherry pick #128291 to 26.9: Fix segfault in `ReverseIndex::insert` after an exception while building the index
robot-clickhouse added a commit that referenced this pull request Nov 2, 2026
…r an exception while building the index
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #128291 to 26.10: Fix segfault in `ReverseIndex::insert` after an exception while building the index
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #128291 to 26.9: Fix segfault in `ReverseIndex::insert` after an exception while building the index
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #128291 to 26.8: Fix segfault in `ReverseIndex::insert` after an exception while building the index
PedroTadim added a commit that referenced this pull request Nov 2, 2026
Backport #128291 to 26.3: Fix segfault in `ReverseIndex::insert` after an exception while building the index
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…r an exception while building the index
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…r an exception while building the index
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…r an exception while building the index
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…er an exception while building the index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix 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.

4 participants