Repository navigation
Fix segfault and silent data corruption when appending a non-appendable format to a file - #112839
Conversation
…geFileSink A format that registers markFormatHasNoAppendSupport cannot be appended to, and StorageFile::write enforces that. The check lives inside if (!paths.empty()), so two routes into StorageFileSink never reach it: a write through a file descriptor never populates paths, and a partitioned write returns its sink before the check. Both then let StorageFileSink::initialize suppress the format prefix for a target that already has bytes. With rows that appends a second Avro header after the existing data, so the query succeeds with no diagnostic and the resulting file no longer reads back. With zero rows nothing calls DataFileWriterBase::syncIfNeeded, which is the only thing that binds the encoder to a stream; DataFileWriterBase::sync then flushes the encoder as its first statement and binds the stream one line later, so StreamWriter::out_ is still the default nullptr and the server dies with SIGSEGV. The second one is the failure reported on ClickHouse#111164. Ask the format's own append-support checker in StorageFileSink::initialize, where the authoritative per-sink size is computed and one statement before the prefix is suppressed. That covers both symptoms and both uncovered routes, and it also closes the window between the query-start stat in StorageFile::write and the per-sink fstat. Adding syncIfNeeded to AvroRowOutputFormat::finalizeImpl was tried first and rejected on measurement: it stops the fault but still writes an unreadable file, so it trades a loud failure for silent corruption. On these two routes an INSERT into a non-empty file now fails for Arrow, ArrowStream, JSON, Npy, ORC and Parquet where it previously returned success. Measuring all thirteen always-non-appendable formats there shows none of them ever appended: five read back only the original row and silently discard the appended one, Parquet fails a column read, and the rest already errored. The File engine documentation is qualified accordingly in the same commit.
…ed checker Review round 1 on the StorageFileSink no-append guard. The documentation sentence added with the guard advised the `engine_file_truncate_on_insert` and `engine_file_allow_create_multiple_files` settings unconditionally, but neither setting applies on the file-descriptor route: `StorageFileSink::initialize` builds a `WriteBufferFromFileDescriptor` without consulting `flags`, and the multiple-files branch rewrites `paths`, which is empty there. The guard's own error message already omits that advice for this reason, so the two artifacts contradicted each other about the behaviour this change introduces. The sentence is now scoped to a plain file path and states why the descriptor route is different. The settings-dependent-checker test cell only drove the checker's appendable branch, where a guard keyed on a hardcoded format list and no guard at all both succeed, so the cell could not distinguish them; removing the guard left it green. It now also asserts the non-appendable configuration is rejected, which fails under both bypasses and proves the per-statement setting reaches the checker through `getFormatSettings`. The test number moved from 04660 to 04668: 04660 was free on master when the test was written but is now claimed by other unpublished work and by an open pull request. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (click to expand)Two independent review rounds ran against this change before it was opened: an Fixed before opening❌ The documentation sentence gave route-invalid remediation advice (blocker, round 1). ❌ The settings-dependent-checker test cell could not distinguish the fix from two
Verified and recorded, not blocking
💡 The new test's number shares its five-digit prefix with other in-flight work. Measured DroppedThe documentation sentence names six formats with "such as" rather than all thirteen What was checked beyond the findingsThe invariant carrier set was enumerated independently and is closed: Session id: cron:clickhouse-review-slot-10:20260731-233800 |
Pre-PR validation gate (click to expand)
Regression sweep: 180 selected tests exercising Session id: cron:clickhouse-review-slot-10:20260731-233800 |
|
cc @scanhex12 @vitlibar, could you review this? The format's no-append contract is enforced in |
|
Workflow [PR], commit [1ad0aa7] Summary: ✅
AI ReviewSummaryThis PR moves the non-appendable-format guard from Final Verdict✅ No new findings. The fix, regression coverage, and changelog metadata are consistent with the user-visible behavior change. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 17/17 (100.00%) · Uncovered code |
…n appending a non-appendable format to a file
Backport #112839 to 26.7: Fix segfault and silent data corruption when appending a non-appendable format to a file
Backport #112839 to 26.6: Fix segfault and silent data corruption when appending a non-appendable format to a file
Cherry pick #112839 to 26.5: Fix segfault and silent data corruption when appending a non-appendable format to a file
Cherry pick #112839 to 26.3: Fix segfault and silent data corruption when appending a non-appendable format to a file
Cherry pick #112839 to 25.8: Fix segfault and silent data corruption when appending a non-appendable format to a file
…n appending a non-appendable format to a file
…n appending a non-appendable format to a file
…n appending a non-appendable format to a file
Backport #112839 to 26.3: Fix segfault and silent data corruption when appending a non-appendable format to a file
Backport #112839 to 25.8: Fix segfault and silent data corruption when appending a non-appendable format to a file
Backport #112839 to 26.5: Fix segfault and silent data corruption when appending a non-appendable format to a file
…uption when appending a non-appendable format to a file
PROTO: File/file() append in a non-appendable format (fd or partitioned path) -> crash plus corruption. Effect in the production build a394e92: its upstream test(s) 04668_file_sink_no_append_format 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): 04668_file_sink_no_append_format. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #111164
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a segfault and silent data corruption when an
INSERTinto theFileengine or thefiletable function appends to a non-empty file in a format that does not support appending, such asAvro. Writing through a file descriptor or a partitioned path bypassed the existing check, so the format prefix was suppressed and a second header was written after the existing bytes, leaving the file unreadable. Such anINSERTis now rejected withCANNOT_APPEND_TO_FILE, as it already is for a plain path.Description
ClickHouse declares that some output formats cannot be appended to and enforces that in
StorageFile::write, but the check is insideif (!paths.empty()). Writes through a file descriptor never populatepaths, and partitioned writes return a sink earlier, so neither reaches it and both letStorageFileSink::initializesuppress the format prefix for a target that already has bytes.With rows, that appends a second Avro header after the existing data: exit code 0, no diagnostic, and the file no longer reads back. With zero rows nothing binds the Avro encoder to a stream, and
close()flushes the encoder beforesync()binds it, so the server dereferences a null pointer and dies with SIGSEGV, which is the failure reported on #111164.The fix moves the format's own append check into
StorageFileSink::initialize, next to where the per-sink size is computed and one statement before the prefix is suppressed. That is the only place the authoritative size is known, so it also closes the window between the query-startstatand the per-sinkfstat. All four routes now agree, and the error is the code the plain-path route already raises.Behavior change: on these two routes an
INSERTinto a non-empty file now fails forArrow,ArrowStream,JSON,Npy,ORCandParquetwhere it previously returned success. Measuring all thirteen non-appendable formats there shows none ever appended: five read back only the original row, discarding the appended one,Parquetfails a column read, and the rest already errored. The documented promise never held here, so this is a bug fix rather than a backward incompatible change. Both escape-hatch settings are unaffected, and theFileengine documentation is qualified in the same commit.The reported CI query was path-based and fuzzer-mutated, while the deterministic reproducer is descriptor-based; both reach the same state.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111164&sha=1ddf512de11b00b3261684cb133ea3ab93ed2763&name_0=PR&name_1=Stress+test+%28arm_asan_ubsan%29
Version info
26.8.1.597(included in26.8and later)26.7.2.56,26.6.2.142,26.5.6.105,26.3.17.100,25.8.29.43