Skip to content

Return OK from getCurrentExceptionCode when no exception is being handled - #122891

Merged
murphy-4o merged 3 commits into
ClickHouse:masterfrom
murphy-4o:unique-key/fix/rollback-unwinding-crash
Sep 30, 2026
Merged

murphy-4o merged 3 commits into
ClickHouse:masterfrom
murphy-4o:unique-key/fix/rollback-unwinding-crash

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

getCurrentExceptionCode and getCurrentExceptionMessageAndPattern (behind getCurrentExceptionMessage and tryLogCurrentException) inspect the current exception by rethrowing it with throw;. That is only legal while an exception is being handled; with none, throw; calls std::terminate.

A destructor that runs during stack unwinding hits exactly that case:

{
    MergeTreeTransactionHolder holder(txn);   // RUNNING transaction, autocommit = false
    throw Exception(...);                     // an exception escapes the scope
}
// stack unwinding, no catch has run yet:
~MergeTreeTransactionHolder()
    TransactionManager::rollbackTransaction(txn)
        LOG_TRACE(log, "Rolling back transaction {}{}", txn->tid,
            std::uncaught_exceptions()                    // 1: an exception is in flight
                ? fmt::format(" ... (code: {})", getCurrentExceptionCode())
                : "");
            getCurrentExceptionCode()
                try { throw; }                            // nothing is being handled
                → std::terminate()                        // the server dies

Both helpers now return early when std::current_exception() is null: OK and an empty message. tryLogCurrentExceptionImpl gets the same guard, since it rethrows once more to mark the exception as logged. rollbackTransaction prints the code only when an exception is being handled; during unwinding the in-flight exception is not observable, so the trace omits the code instead of printing 0.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/122891]

Version info

  • Merged into: 26.10.1.1036-master (included in 26.10 and later)

…dled

getCurrentExceptionCode and getCurrentExceptionMessageAndPattern rethrow with
`throw;`, which calls std::terminate when no exception is being handled. That
happens in a destructor during stack unwinding, where std::uncaught_exceptions()
is already non-zero: TransactionManager::rollbackTransaction calls
getCurrentExceptionCode exactly then when a MergeTreeTransactionHolder is
destroyed by an escaping exception. Return OK / an empty message instead.

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

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2291bdc]

Summary: ✅


AI Review

Summary

This PR hardens the current-exception helpers so they no longer call std::terminate when invoked without a handled exception, adds focused coverage for the no-handler and destructor-unwinding cases, and avoids logging a false code: 0 from rollbackTransaction in that path. The current head also fixes the two earlier inline bot concerns around tryLogCurrentException and the rollback trace, and I did not find any remaining review-worthy issues in the diff.

Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit 2291bdc.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 29, 2026
@murphy-4o murphy-4o removed the pr-bugfix Pull request with bugfix, not backported by default label Sep 29, 2026
@murphy-4o
murphy-4o marked this pull request as ready for review September 29, 2026 13:45
@clickhouse-gh clickhouse-gh Bot added the comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). label Sep 29, 2026
clang-analyzer-deadcode.DeadStores flagged the aggregate probe whose
destructor writes through references.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 29, 2026
Comment thread src/Common/Exception.cpp Resolved
Comment thread src/Common/Exception.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2291bdcf5 with master a95819412 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 16 s compile time in total, 9 of them have a recent master baseline.

Job report

… trace

tryLogCurrentException still reached a bare throw; with no handled
exception. The rollback trace printed code 0 for an exception still in
flight during unwinding.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Common/ExceptionExt.h Resolved
@tuanpach tuanpach self-assigned this Sep 30, 2026
@murphy-4o
murphy-4o added this pull request to the merge queue Sep 30, 2026
Merged via the queue into ClickHouse:master with commit 189abf6 Sep 30, 2026
149 checks passed
@murphy-4o
murphy-4o deleted the unique-key/fix/rollback-unwinding-crash branch September 30, 2026 05:02
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants