Skip to content

Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row - #107739

Merged
alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
atsarevskiy:fix-protobuf-setcolumns-after-recovery
Jul 25, 2026
Merged

alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
atsarevskiy:fix-protobuf-setcolumns-after-recovery

Conversation

@atsarevskiy

@atsarevskiy atsarevskiy commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #107644
Related: #106905

Description

Follow-up to #106905. INSERT ... FORMAT Protobuf with input_format_allow_errors_num > 0 still crashed the server (SIGSEGV) when one or more valid messages were read before a bad (skippable) message in the same block.

After a parse error, readRow's catch block calls destroyReaderAndSerializer, which nulls the serializer, and the next readRow recreates it. But setColumns was only called when row_num == 0. When a valid row had already been appended to the block (row_num > 0), the freshly recreated serializer kept null column pointers and dereferenced them in ProtobufSerializer::readRow — an exception in the debug build, SIGSEGV at address 0x18 in release.

The fix calls setColumns whenever the serializer was just (re)created, not only at the start of a block. Adds a gtest (ValidRowBeforeBadRowRecovery) that reproduces the crash (valid → bad → valid) and passes with the fix.

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 server crash (SIGSEGV) when reading Protobuf data with input_format_allow_errors_num > 0 and a valid message precedes a bad (skippable) message in the same block.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Backported to: 26.7.2.18, 26.6.2.115, 26.5.6.75, 26.3.17.77, 25.8.29.38

Follow-up to ClickHouse#106905. After a parse error, destroyReaderAndSerializer
nulls the serializer and the next readRow recreates it. setColumns was
only called when row_num == 0, so when a valid row had already been
appended to the block (row_num > 0), the freshly recreated serializer
kept null column pointers and threw an exception (in the debug build,
SIGSEGV in release) inside ProtobufSerializer::readRow.

Call setColumns whenever the serializer was just (re)created, not only
at the start of a block.

Closes ClickHouse#107644
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 17, 2026
@alexey-milovidov alexey-milovidov self-assigned this Jun 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e8b48f8]

Summary: ✅


AI Review

Summary

This PR fixes ProtobufRowInputFormat so a serializer recreated after skipping a bad length-delimited message is rebound to the current block columns before the next row is read. I checked the current diff, the existing review discussion, the surrounding readRow / syncAfterError recovery path, and the added functional regression test; the fix matches the reported valid -> bad -> valid failure mode, and I did not find a remaining correctness issue in the touched surface. Current Praktika CI for e8b48f8b8ffe72786b885d9aceb4462edf07437f shows no failed reports.

Final Verdict

Status: ✅ Approve

@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 Jun 17, 2026
@atsarevskiy

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov Can we merge this PR?

Don't think performance failures related to my change, it fails both old/new on TSV format

2026-06-17T18:06:20.9887128Z [2026-06-17 18:06:14] /*stdin*\            :  1.83%   (7790448640 => 142236839 bytes, ./logs.tar.zst) 
2026-06-17T18:06:20.9887287Z [2026-06-17 18:06:14] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2026-06-17T18:06:20.9887469Z [2026-06-17 18:06:14] Fail [Performance Comparison (arm_release, master_head, 6/6)]
2026-06-17T18:06:20.9887597Z [2026-06-17 18:06:14]   | 1 too long, 2 faster, 6 slower, 6 unstable
2026-06-17T18:06:20.9887683Z [2026-06-17 18:06:14]   Fail [Check Results]
2026-06-17T18:06:20.9887873Z [2026-06-17 18:06:14]     | 1 too long, 2 faster, 6 slower, 6 unstable
2026-06-17T18:06:20.9888010Z [2026-06-17 18:06:14]     Fail [group_by_fixed_keys #7::old]
2026-06-17T18:06:20.9888081Z [2026-06-17 18:06:14]       | unstable
2026-06-17T18:06:20.9888197Z [2026-06-17 18:06:14]     Fail [group_by_fixed_keys #7::new]
2026-06-17T18:06:20.9888265Z [2026-06-17 18:06:14]       | unstable
2026-06-17T18:06:20.9888381Z [2026-06-17 18:06:14]     Fail [group_by_fixed_keys #8::old]
2026-06-17T18:06:20.9888457Z [2026-06-17 18:06:14]       | unstable

alexey-milovidov and others added 3 commits June 24, 2026 07:05
Address review feedback (prefer functional tests over unit tests): drop the
`ValidRowBeforeBadRowRecovery` gtest added for this fix and add an equivalent
stateless functional test `04409_protobuf_skip_bad_row_after_valid`.

The test feeds a length-delimited Protobuf stream of valid, bad, valid messages
into `INSERT ... FORMAT Protobuf` with `input_format_allow_errors_num` and checks
that the bad message is skipped and both valid rows are inserted. Without the fix
the server crashes (SIGSEGV) in `ProtobufSerializer::readRow`; the crash was
verified to reproduce on the current build and the test passes with the fix.

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

Copy link
Copy Markdown
Member

Merged fresh `master` into the branch to retrigger CI.

The previous run on `2ba39bb6` showed a long list of red jobs, but these were all the second-wave jobs (extended builds, stress, performance, full integration) that never started — the whole workflow run was cancelled at 2026-06-24T13:53:26Z (all of them report 0s duration). The first-wave jobs all passed: every Build (*), Fast test, the parallel/targeted Stateless/Integration/Unit tests and the targeted AST fuzzer were green, and the new functional test ran green there too — 04409_protobuf_skip_bad_row_after_valid passed 7/7 in Stateless tests (amd_debug, parallel).

No code change was needed: the merge is clean (master only touched the embedded docs in ProtobufRowInputFormat.cpp, not the readRow fix), and the net diff is unchanged (4 files, +57/-1). The fix and the AI Review verdict (✅ Approve) still stand.

@alexey-milovidov

Copy link
Copy Markdown
Member

Status after re-checking the fresh CI run on 34d1a95d (the master merge from this morning):

  • Mergeable, 0 behind master, approved (incl. AI Review ✅), 0 unresolved review threads. Net diff unchanged — 4 files (ProtobufRowInputFormat.cpp + the 04409_protobuf_skip_bad_row_after_valid test/schema/reference).
  • The only red across all three reports is the TSan Hung check failed, possible deadlock found on Stress test (amd_tsan) and Stress test (arm_tsan). This is the known master-wide flake tracked in Hung check failed, possible deadlock found #107941 (labelled comp-query-analyzer), unrelated to the Protobuf serializer fix in this PR. It hit 77 distinct PRs on 2026-06-30 (170 failures) and 76 PRs the day before, so it is hammering nearly every open PR rather than anything specific here.
  • The "Top short messages" rows in the stress report (e.g. Cannot TRUNCATE dictionary) are general server-log statistics, not test failures.
  • All first-wave jobs are green: every Build (*), Fast test, the parallel/targeted Stateless/Integration/Unit tests, AST fuzzer, both Bugfix validation runs, and arm_tidy. Changed-line coverage is 35/35 (100%). A few second-wave jobs (coverage stateless, Performance Comparison, SQLLogic test) are still in progress and do not touch the Protobuf code path.

No code change was warranted. Ready to merge once the in-progress jobs settle; the red stress check is the #107941 flake and can be merged through.

@alexey-milovidov

Copy link
Copy Markdown
Member

The two red jobs on 34d1a95d (Stress test (amd_tsan) and Stress test (arm_tsan), both Hung check failed, possible deadlock found) are the chronic master-wide flake #107941, not a regression from this change.

Evidence:

  • The CI bot already links both to Hung check failed, possible deadlock found #107941.
  • The hung stacks are query analysis of a fuzzed SELECT — DB::QueryAnalysisPass::run → DB::QueryTreePassManager::run → DB::InterpreterSelectQueryAnalyzer → DB::executeQueryImpl — with a Fuzzed query: {} trace. This PR only touches the Protobuf INSERT input path (ProtobufRowInputFormat::readRow), which never runs under InterpreterSelectQueryAnalyzer. There are no Protobuf/serializer frames in either dump.
  • The flake is massively master-wide: in the last 24h alone it fired 209 times across 93 distinct PRs, 30 of them on master itself (pull_request_number = 0).

Everything else is green: all Build (*), Fast test, Style check, Code Review, both Bugfix validation (functional tests), and the parallel/targeted Stateless/Integration/Unit tests. The new functional test 04409_protobuf_skip_bad_row_after_valid passes. The net diff is unchanged (4 files, +57/-1), the branch is MERGEABLE and only 4 commits behind a merge-base from a few hours ago, and the AI Review verdict is ✅ Approve.

@groeneai, FYI — these two Stress reds are the #107941 hung-check flake (query-analysis hang, unrelated to this Protobuf fix).

@alexey-milovidov

Copy link
Copy Markdown
Member

Update — the second-wave jobs that were still in progress at the time of the previous status comment (coverage stateless, Performance Comparison, SQLLogic test) have now all settled green on 34d1a95d:

  • Performance Comparison (arm_release) 6/6 ✅ (amd_release is SKIPPED), SQLLogic test ✅, LLVM Coverage ✅ (changed C/C++ lines 11/11 = 100%), and every coverage Stateless/Integration/Unit shard ✅.
  • CH Inc sync ✅, Mergeable Check ✅, Style check ✅.

Final tally on this head: 150 success, 18 skipped, 2 failure — the only two reds are the same Hung check failed, possible deadlock found on Stress test (amd_tsan) and Stress test (arm_tsan), re-verified on 34d1a95d as the chronic master-wide #107941 flake: the hung stack is query analysis of a fuzzed SELECT (DB::QueryAnalysisPass::run → DB::QueryTreePassManager::run → DB::InterpreterSelectQueryAnalyzer, with a Fuzzed query: {} trace), with no Protobuf/serializer frames. This PR only touches the Protobuf INSERT input path (ProtobufRowInputFormat::readRow), which never runs under InterpreterSelectQueryAnalyzer.

So the PR is now in the merge-ready state described above: MERGEABLE, 0 unresolved threads, AI Review ✅, and the only outstanding red is the #107941 stress flake, which can be merged through.

alexey-milovidov and others added 3 commits July 15, 2026 15:43
The CI flaky check maps a changed fixture under `format_schemas/` back to
the tests that textually reference its filename; when no test contains the
literal `04409_protobuf_skip_bad_row_after_valid.proto`, it falls back to
running every `04409_*` test. That made the flaky check (and the CH Inc
sync run) repeatedly execute the unrelated, heavy
`04409_filesystem_cache_reserve_granularity`, which fails with "Test runs
too long" under ASan:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107739&sha=b37d1cc3dbb68cb2c2e3f82de590b84d0e79e2bd&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

Using the explicit extension in `format_schema` is already common in the
test suite and makes the fixture-to-test mapping precise.

Co-Authored-By: Claude Fable 5 <[email protected]>
pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Jul 19, 2026
…its owning test

`_tests_owning_data_file` maps a changed auxiliary file (e.g. a
`format_schemas/*.proto` schema) back to the tests that reference it, and
falls back to running every same-number-prefix test when no candidate
references the literal filename. Format schemas are conventionally
referenced without the extension (`format_schema = 'NNNNN_foo:Message'`),
so any PR adding a schema fixture hit the fallback and the flaky check ran
unrelated sibling tests. Seen on PR ClickHouse#107739, where the flaky check ran the
heavy, unrelated `04409_filesystem_cache_reserve_granularity` (prefix
sibling of the PR's own test) and failed with "Test runs too long":
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107739&sha=b37d1cc3dbb68cb2c2e3f82de590b84d0e79e2bd&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

Also match the extensionless stem: candidates are already narrowed to the
fixture's five-digit prefix, so the stem is still an unambiguous reference.

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

clickhouse-gh Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 86.20% +0.60%
Functions 91.90% 92.00% +0.10%
Branches 77.50% 78.40% +0.90%

Changed lines: Changed C/C++ lines covered: 11/11 (100.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 25, 2026
Merged via the queue into ClickHouse:master with commit 5d55769 Jul 25, 2026
178 checks passed
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 25, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 25, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 25, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 25, 2026
Backport #107739 to 26.6: Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
clickhouse-gh Bot added a commit that referenced this pull request Jul 25, 2026
Backport #107739 to 26.7: Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @atsarevskiy @alexey-milovidov — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently incomplete):

  • Why: The body states 'Adds a gtest (ValidRowBeforeBadRowRecovery) that reproduces the crash', but the diff actually adds a stateless shell test (04409_protobuf_skip_bad_row_after_valid.sh) plus a .reference and a .proto schema — no gtest is added. The test-type claim is inaccurate.

  • Suggested addition:

    Correct the test description: the regression coverage is a stateless shell test tests/queries/0_stateless/04409_protobuf_skip_bad_row_after_valid.sh (with a .proto schema and .reference), not a gtest. It feeds a length-delimited Protobuf stream (valid -> bad -> valid) with input_format_allow_errors_num > 0 and asserts both valid rows are inserted without a crash.

Ignore this if the current title/body is intentional.

alexey-milovidov added a commit that referenced this pull request Jul 26, 2026
Backport #107739 to 26.5: Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
alexey-milovidov added a commit that referenced this pull request Jul 26, 2026
Backport #107739 to 26.3: Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
alexey-milovidov added a commit that referenced this pull request Jul 26, 2026
…-contained

The functional test added by #107739
(`04409_protobuf_skip_bad_row_after_valid`) crashed with `SIGSEGV` on `25.8`
because the prerequisite fix from #106905 is missing on this branch: its
backport #107627 was closed without merging, so `25.8` still has

    void ProtobufRowInputFormat::syncAfterError()
    {
        reader->endMessage(true);
    }

After a parse error, `readRow`'s catch block calls
`destroyReaderAndSerializer`, which nulls `reader`. `IRowInputFormat::read`
then calls `syncAfterError`, which dereferences the null `reader` -> `SIGSEGV`.
The `setColumns` fix backported here is therefore necessary but not sufficient
on `25.8`.

Cherry-pick the `ProtobufRowInputFormat.cpp` part of #106905
(`cda04909d946c83546b4a9073ddf7032e9616669`): resync via `endMessage(true)`
inside `readRow`'s catch while `reader` is still alive, make `syncAfterError` a
no-op, and restrict `allowSyncAfterError` to the length-delimited case. The
resulting `readRow` / `syncAfterError` / `allowSyncAfterError` logic is now
byte-identical to `master`.

The unit test from #106905 is not carried over: `25.8` has no
`src/Processors/Formats/Impl/tests` directory, and the functional test
`04409_protobuf_skip_bad_row_after_valid` already covers this path.

Original pull-request: #107739
Prerequisite pull-request: #106905
Related: #107627
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111910&sha=latest&name_0=BackportPR&name_1=Stateless%20tests%20(amd_asan%2C%20distributed%20plan%2C%20parallel%2C%202%2F2)

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Jul 29, 2026
Backport #107739 to 25.8: Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
joelynch pushed a commit to aiven/ClickHouse that referenced this pull request Aug 17, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…tcolumns-after-recovery

Fix crash in ProtobufRowInputFormat when a valid row precedes a bad row
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…its owning test

`_tests_owning_data_file` maps a changed auxiliary file (e.g. a
`format_schemas/*.proto` schema) back to the tests that reference it, and
falls back to running every same-number-prefix test when no candidate
references the literal filename. Format schemas are conventionally
referenced without the extension (`format_schema = 'NNNNN_foo:Message'`),
so any PR adding a schema fixture hit the fallback and the flaky check ran
unrelated sibling tests. Seen on PR ClickHouse#107739, where the flaky check ran the
heavy, unrelated `04409_filesystem_cache_reserve_granularity` (prefix
sibling of the PR's own test) and failed with "Test runs too long":
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107739&sha=b37d1cc3dbb68cb2c2e3f82de590b84d0e79e2bd&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

Also match the extensionless stem: candidates are already narrowed to the
fixture's five-digit prefix, so the stem is still an unambiguous reference.

Co-Authored-By: Claude Fable 5 <[email protected]>
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
PROTO: Protobuf with allow_errors, a valid message then a bad one -> crash.
Effect in the production build a394e92: its upstream test(s) 04409_protobuf_skip_bad_row_after_valid fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 04409_protobuf_skip_bad_row_after_valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
PROTO: ProtobufRowInputFormat with input_format_allow_errors_num/ratio: after a parse error, readRow destroyed
the reader and IRowInputFormat then called syncAfterError, which dereferenced the null reader -> crash.
Dependency of the ClickHouse#107739 backport (50b1854): the 26.3 branch had this fix (e6675f6, 2026-06-16) before
ClickHouse#107739 (1c4cb1f, 2026-07-25), and the fork's v26.3.12.3 base predates it. Found by the CAND4 release gate:
the ClickHouse#107739 test 04409_protobuf_skip_bad_row_after_valid crashed CAND4 (e7432ca) with SIGSEGV in
ProtobufReader::endMessage via IRowInputFormat::read (evidence/fix_ci_20260925/release_cand4/logs/).
Upstream patch unchanged (hunks at an 8-line offset because of ClickHouse#107739), with its gtest
gtest_protobuf_sync_after_error.cpp; the stateless test is ClickHouse#107739's 04409.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-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

5 participants