Skip to content

Fix crash on corrupt txn_version.txt in assertHasValidVersionMetadata - #99025

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
fix-corrupt-txn-version-metadata
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
fix-corrupt-txn-version-metadata

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 9, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix server crash (and inability to restart) when txn_version.txt file is corrupt
  • The file can become corrupt on DiskObjectStorage (Azure/S3) because append operations are simulated via non-atomic read-modify-write of metadata
  • In loadVersionMetadata: catch parse errors and treat the part as prehistoric, allowing the server to start
  • In assertHasValidVersionMetadata: distinguish between actual metadata mismatches (assertion fails) and file corruption (warning only, no crash)

Closes #94142

CI reports:

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

🤖 Generated with Claude Code


Note

Medium Risk
Touches MergeTree transactional version-metadata loading/validation; incorrect fallback could hide real metadata issues or change part visibility/cleanup behavior, though changes are largely scoped to corruption handling and logging.

Overview
Prevents crashes/restart loops when a part’s txn_version.txt is unreadable (e.g., due to non-atomic append semantics on object storage): loadVersionMetadata() now catches parse failures, logs a warning, and falls back to prehistoric version metadata so the server can start and the part can be cleaned up normally.

Adjusts assertHasValidVersionMetadata() to distinguish corruption from real mismatches: metadata mismatches still fail the assertion, but parse/validation errors consistent with file corruption now only warn (with expected vs actual content) and do not fail the assertion.

Written by Cursor Bugbot for commit f604696. This will update automatically on new commits. Configure here.

…ata`

The `txn_version.txt` file can become corrupt on DiskObjectStorage
(Azure/S3) because append operations are simulated via read-modify-write
of metadata, which is not atomic. Concurrent appends (e.g., transaction
rollback clearing removal_tid + new transaction setting removal_tid +
commit writing CSN) can race, causing the metadata to lose the original
blob containing the file header (`version: 1\ncreation_tid: ...`).

This caused two crashes:
1. `assertHasValidVersionMetadata` returned false on corrupt file →
   `chassert` terminated the server in sanitizer builds.
2. After restart, `loadVersionMetadata` failed to parse the corrupt
   file → `loadOutdatedDataParts` called `std::terminate`.

Fix both:
- In `loadVersionMetadata`: catch parse errors for corrupt files and
  treat the part as prehistoric, allowing the server to start normally.
- In `assertHasValidVersionMetadata`: distinguish between actual metadata
  mismatches (in-memory vs on-disk, return false) and file corruption
  (unparseable file, return true with warning), since file corruption
  is not a logic error in version tracking.

Closes #94142

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=54911410bd8f61f5421394491d091f041ebc01d4&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_msan%29
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=06598f27ab378120683ccf2cc08c0632aca84f0c&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%2C%202%2F2%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f604696]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 9, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.


version.setCreationTID(Tx::PrehistoricTID, nullptr);
version.creation_csn = Tx::PrehistoricCSN;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Incomplete version metadata reset after corrupt file parse

Medium Severity

When version.read(*buf) partially parses a corrupt file before throwing, the catch block only resets creation_tid and creation_csn but does not reset removal_tid, removal_tid_lock, or removal_csn. VersionMetadata::read() can successfully set removal_tid and removal_tid_lock before failing on a later field, leaving the metadata in an inconsistent state where the part appears prehistoric for creation but has non-empty removal fields. This can cause wasInvolvedInTransaction() to return true due to a non-zero removal_tid_lock, leading to unexpected behavior downstream.

Fix in Cursor Fix in Web

@alexey-milovidov
alexey-milovidov marked this pull request as draft March 9, 2026 04:59
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Almost sure this is wrong.

@clickhouse-gh

clickhouse-gh Bot commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.40% 76.30% -0.10%

PR changed lines: PR changed-lines coverage: 21.28% (10/47)
Diff coverage report
Uncovered code

@tiandiwonder

Copy link
Copy Markdown
Contributor

Created a new PR#99129 to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: 'assertHasValidVersionMetadata()' (STID: 2508-48ff)

2 participants