Skip to content

Catch exceptions in MergeTreeTransaction::afterCommit to prevent std::terminate - #99494

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
fix-aftercommit-noexcept-terminate
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
fix-aftercommit-noexcept-terminate

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Summary

  • Wrap appendCSNToVersionMetadata and setMutationCSN calls in afterCommit with try-catch blocks to prevent std::terminate when disk write fault injection triggers an exception in a noexcept function
  • Writing CSN to version metadata is an optimization - the CSN can be recovered from the transaction log in ZooKeeper on restart

Fixes #85468

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=9f55e660b5a692bc6a65ecb1935bf81ea840dfeb&name_0=MasterCI&name_1=BuzzHouse%20%28arm_asan%29

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…td::terminate`

The `afterCommit` method is `noexcept`, but it calls `appendCSNToVersionMetadata`
and `setMutationCSN` which write to disk and can throw exceptions - particularly
when fault injection triggers a `WriteFileOperation` failure. This causes
`std::terminate` since exceptions cannot escape a `noexcept` function.

Writing CSN to version metadata is an optimization - the CSN can be recovered
from the transaction log in ZooKeeper on restart, as the existing comment states:
"It's not a problem if server crash before CSN is written."

Wrap the three disk-writing call sites in try-catch blocks and log warnings
instead of terminating the server.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=9f55e660b5a692bc6a65ecb1935bf81ea840dfeb&name_0=MasterCI&name_1=BuzzHouse%20%28arm_asan%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [52e27b1]

Summary: ❌

job_name test_name status info comment
SQLLogic test failure
Report & Thresholds failure cidb

AI Review

Summary

This PR wraps MergeTreeTransaction::afterCommit metadata updates in try/catch blocks to keep the noexcept finalization path from calling std::terminate on write-time exceptions. I reviewed the single-file diff and found no high-confidence correctness, safety, or performance regressions introduced by the change.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

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

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@clickhouse-gh

clickhouse-gh Bot commented Mar 14, 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.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 80.56% (29/36, 0 noise lines excluded)
Diff coverage report
Uncovered code

@tiandiwonder tiandiwonder self-assigned this Mar 15, 2026
@tiandiwonder

Copy link
Copy Markdown
Contributor
  • Writing CSN to version metadata is an optimization - the CSN can be recovered from the transaction log in ZooKeeper on restart

It‘s wrong: the TID->CSN map can be cleaned up.

  /// Write allocated CSN, so we will be able to cleanup log in ZK. This method is noexcept.
  txn->afterCommit(allocated_csn); 
  state_guard = {};
  {
      /// Finally we can remove transaction from the list and release the snapshot
      std::lock_guard lock{running_list_mutex};
      snapshots_in_use.erase(...);
      running_list.erase(txn->tid);
  }

btw, even write is successful, there is still a race the log can be cleaned before the metadata file has been synced.

@tiandiwonder tiandiwonder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  1. rollback() should be handled too.
  2. better to have a regression. test.

}
catch (...)
{
tryLogCurrentException("MergeTreeTransaction", "Failed to write creation CSN to version metadata");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

print part name for debug.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

I've removed transactions from fuzzers: #99029
Now you are on your own.

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.

[CI crash] Transaction log finalize failed during commit

2 participants