Repository navigation
Conversation
…inding
TransactionManager::rollbackTransaction is noexcept and exists to be callable
from destructors. Its opening LOG_TRACE asked for the in-flight exception's
code whenever std::uncaught_exceptions() was non-zero:
LOG_TRACE(log, "Rolling back transaction {}{}", txn->tid,
std::uncaught_exceptions() ? fmt::format(" due to uncaught exception (code: {})",
getCurrentExceptionCode()) : "");
getCurrentExceptionCode() is a bare rethrow (try { throw; } catch (...) in
src/Common/Exception.cpp), which per [except.throw]/9 is legal only while an
exception is presently being *handled*. The two states are tracked by different
counters in the Itanium ABI: __cxa_rethrow reads __cxa_eh_globals::caughtExceptions,
populated by __cxa_begin_catch, while std::uncaught_exceptions() reads
uncaughtExceptions, incremented by __cxa_throw and decremented by
__cxa_begin_catch. So a non-zero std::uncaught_exceptions() means an exception
is in flight and no handler has been entered yet, which is exactly when
caughtExceptions is null and __cxa_rethrow calls std::terminate() directly. The
guard selected the one case in which the call is illegal, and skipped the case
in which it would have been legal (inside a handler, where the counter has
already been decremented and the suffix was therefore never printed).
Wrapping the call in try/catch is not a fix: __cxa_rethrow
(contrib/llvm-project/libcxxabi/src/cxa_exception.cpp) calls std::terminate()
itself rather than throwing, so no handler can intercept it. Nothing can
retrieve the in-flight exception at that point either; during phase-2 unwinding
the exception object is reachable only from the personality routine, and
std::current_exception() reads the same caughtExceptions slot, so it is null
there too. Drop the code from the message and keep the part that is knowable.
Nothing is lost: every execution that reached the call either terminated the
server or, in the nested case, reported the already-handled exception's code
rather than the in-flight one, so the "(code: N)" suffix never once conveyed a
correct value from this call site.
Three live carriers reach the aborting path. MergeTreeTransactionHolder::onDestroy
skips its autocommit branch precisely when std::uncaught_exceptions() is non-zero
and falls through to rollbackTransaction, so the destroyed-while-unwinding path
is the one the noexcept rollback exists to serve:
* StorageMergeTree::scheduleDataProcessingJob holds a local
MergeTreeTransactionHolder across metadata and part selection whenever the
table has transactions_enabled, and its caller
BackgroundJobsAssignee::threadFunc is a function-try-block whose catch (...)
is entered only after that holder has been unwound, so any throw in
background merge or mutation scheduling on a transactional table aborted the
server;
* a Context holding an open transaction whose last reference is released while
an exception unwinds, which is how the AST fuzzer reached it;
* MergePlainMergeTreeTask::txn_holder, destroyed with the task object.
executeQuery.cpp's hoisted fuzz-context declarations carried a comment
describing the removed code; it is replaced with the reason that survives.
The failpoint is how the path is reached from SQL: of the three carriers only
background scheduling is drivable by a query, and nothing in that function
throws today. It is REGULAR rather than ONCE so that a tick on another table
cannot consume it, and it is modelled on the FAULT_INJECTED failpoints already
in the same file.
Measured on a debug build (clang-22, the ci/defs/defs.py ToolSet pin). With the
failpoint present and the log line unchanged, the new test kills the server and
the fatal stack matches the CI report frame for frame: abort <- terminate_handler
<- std::terminate <- __cxa_rethrow <- getCurrentExceptionCode <-
rollbackTransaction <- MergeTreeTransactionHolder::onDestroy <- ~holder <-
scheduleDataProcessingJob <- BackgroundJobsAssignee::threadFunc, reported as
"Terminate called without an active exception". With the fix the same test
passes 50 consecutive randomized runs, the server logs the surviving message on
that path microseconds before the assignee's handler reports the injected
exception, and the transaction is rolled back.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings;
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-48:20260918-222100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260918-213900 |
|
Workflow [PR], commit [731db53] Summary: ✅
AI ReviewSummaryThis PR removes an invalid Missing context / blind spots
Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 731db53.
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
CI finish ledger - d46c7aeEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Rule 3c, not 3d, decided at the tree that ran: praktika pins the merge commit at run creation, and the Session id: cron:our-pr-ci-monitor:20260919-020058 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units10 translation units recompiled, 43 s compile time in total, 10 of them have a recent master baseline. |
The failpoint the regression test uses is server wide: while it is enabled, every transactional MergeTree table that runs BackgroundJobsAssignee::threadFunc throws the same exception, and the assignee reports it under the same logger name (__PRETTY_FUNCTION__), so a witness row could not be attributed to the table the test drives. Measured on a server holding one extra transactional table: the old witness fires for a table whose transactions_enabled is false, i.e. one that cannot reach the failpoint at all. With the table in the message, the test matches text_log's message_format_string and value1 columns, which keep the exception's format string and its arguments, so the table is compared exactly instead of as a substring of the rendered message. Measured with the same fixture plus a same-database table whose name extends t_txn_unwind: the witness is set only when this test's own table traversed the path. 50/50 runs pass on the polluted server (the extra table emitted 6 of its own witness rows during that batch), and with the rollback fix reverted the test still fails with the abort.
|
@groeneai can simplify the change log entry? |
|
Thanks for the review. Simplified, the entry now reads:
Down from 469 to 253 characters, present tense, with the mechanism left in the description. Body edit only, no code change. |
CI finish ledger - 3a23261Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
The three other rows of the previous ledger are green at this head: Session id: cron:our-pr-ci-monitor:20260919-110331 |
|
Agreed, #122891 is the better fix: it makes The only hunk here that yours does not carry is the comment above I'm fine with closing this PR in favour of yours, and I'll leave the close to you or @ tuanpach, who approved this one. |
|
Close in favor of #122891 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a server crash that can happen when an error causes an open transaction to be rolled back, for example when a background merge or mutation fails on a table used in a transaction. Only affects servers with
allow_experimental_transactionsenabled.Description
Found by the AST fuzzer, which aborted the server with
std::terminate(STID 2631-4bbd). Provenance, the fatal stack and the validation runs are in the details block below; the carrier PR touches no transaction code.TransactionManager::rollbackTransactionisnoexceptso that destructors can call it, but its openingLOG_TRACEasked for the in-flight exception's code wheneverstd::uncaught_exceptions()was non-zero.getCurrentExceptionCode()is a barethrow;, legal only while an exception is being handled. A non-zerostd::uncaught_exceptions()means the opposite: one is in flight and no handler has been entered, which is exactly when__cxa_rethrowcallsstd::terminate(). The guard selected the illegal case and skipped the legal one. Wrapping the call cannot help, because__cxa_rethrowterminates directly rather than throwing, and nothing can retrieve the in-flight exception during unwinding anyway. So I drop the code from the message and keep the rest. Nothing is lost: every execution that reached that call either terminated the server or printed an already-handled exception's code.Besides the fuzzer, this aborted the server on any throw inside background merge or mutation scheduling for a transactional table, because
BackgroundJobsAssignee::threadFuncis a function-try-block whose handler is entered only after the transaction holder has been unwound.Not a recent regression: the line is present in 26.3 to 26.8 and in tags back to v22.7. Reachable only with
allow_experimental_transactionsenabled.The new failpoint is the only way to reach the path from SQL. Happy to drop it and the test if you consider it noise, and equally happy to drop the
executeQuery.cppcomment change, which only removes a description of the code this PR deletes.I also noticed one latent but currently unreachable sibling (
ExceptionKeepingTransform::onExceptionthroughStorageAlias.cpp, whose callback vector is provably empty on that path). No user input reaches it, so I am not raising it.Provenance, fatal stack and validation
First occurrence in 90 days, on the STID key and on
position(test_context_raw, 'MergeTreeTransactionHolder::onDestroy') > 0alike. Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118490&sha=1652fe2d258b4d758e40a92a444130f3d3e1fdf9&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29onDestroyskips its autocommit branch precisely whenstd::uncaught_exceptions() != 0, so the aborting path is the one thisnoexceptrollback exists to serve.MergePlainMergeTreeTask::txn_holderis a third carrier.Validated on a debug build. Without the fix the new test kills the server (runner
return code: 210, processSIGABRT, logTerminate called without an active exception, frames 3 to 13 identical to the report). With it, 50 consecutive randomized runs pass and the server logs the surviving message microseconds before the assignee's handler reports the injected exception. With the failpoint disabled, or with the transactional insert skipped, the asserted witness is absent, so it is keyed to the injected throw.Workflow [PR]
Sync PR [sync-upstream/pr/120929]