Skip to content

Do not require the patched libc++ to compile Common/Exception.cpp - #112865

Merged
alexey-milovidov merged 1 commit into
masterfrom
exception-stack-trace-optional
Aug 1, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
exception-stack-trace-optional

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Exception.cpp opens with static_assert(STD_EXCEPTION_HAS_STACK_TRACE == 1), and that macro comes from ClickHouse's patched libc++ in contrib/libcxx-cmake. A build linking any other C++ standard library cannot compile the file at all - and it is not only the assert: six places call std::exception::get_stack_trace_frames, which only the patched libc++ has. utils/wasm-parser works around this today by leaving Exception.cpp out of its build entirely and substituting stubs.

This moves the two operations - reading the throw-site trace, and adopting one exception's trace as another's - behind getStackTraceOfThrow and copyStackTraceOfThrow in Common/StackTrace.h, so exactly one place has to know whether the standard library records a trace at all. base/defines.h defaults STD_EXCEPTION_HAS_STACK_TRACE to 0 when it is absent.

It stays fail-closed. Every currently supported platform builds contrib/libcxx-cmake - cmake/cxx.cmake is included by every cmake/*/default_libs.cmake - so Exception.cpp still hard-asserts the macro is 1 on Linux, Darwin, FreeBSD and SunOS. Losing the definition there would silently strip the stack trace from every exception, and it still cannot happen.

As a side effect this removes six copies of the same get_stack_trace_frames/get_stack_trace_size/__msan_unpoison incantation.

No behaviour change. Checked against a build of the same commit without the change: identical symbolized stack traces for a thrown DB::Exception, for the Poco::Exception conversion path and for the std::exception conversion path, and system.errors.last_error_trace populated the same way.

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

Common/Exception.cpp no longer requires ClickHouse's patched libc++ to compile, so a build linking a different C++ standard library gets exceptions without a throw-site stack trace rather than not compiling.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

`Exception.cpp` opens with `static_assert(STD_EXCEPTION_HAS_STACK_TRACE == 1)`,
and that macro comes from ClickHouse's patched libc++ in `contrib/libcxx-cmake`.
A build linking any other C++ standard library cannot compile the file at all -
and it is not only the assert: six places call
`std::exception::get_stack_trace_frames`, which only the patched libc++ has.
`utils/wasm-parser` works around this by leaving `Exception.cpp` out of its
build entirely and substituting stubs.

Move the two operations - reading the throw-site trace, and adopting one
exception's trace as another's - behind `getStackTraceOfThrow` and
`copyStackTraceOfThrow` in `Common/StackTrace.h`, so exactly one place has to
know whether the standard library records a trace at all. `base/defines.h`
defaults `STD_EXCEPTION_HAS_STACK_TRACE` to 0 when it is absent.

This stays fail-closed. Every currently supported platform builds
`contrib/libcxx-cmake` - `cmake/cxx.cmake` is included by every
`cmake/*/default_libs.cmake` - so `Exception.cpp` still hard-asserts the macro
is 1 on Linux, Darwin, FreeBSD and SunOS. Losing the definition there would
silently strip the stack trace from every exception, and it still cannot happen.

As a side effect this removes six copies of the same
`get_stack_trace_frames`/`get_stack_trace_size`/`__msan_unpoison` incantation.

No behaviour change: the refactored build produces byte-identical symbolized
stack traces, and `system.errors.last_error_trace` is populated exactly as
before.

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

clickhouse-gh Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [18a7708]

Summary: ✅


AI Review

Summary

This PR centralizes the optional embedded-exception stack-trace handling behind getStackTraceOfThrow / copyStackTraceOfThrow and defaults STD_EXCEPTION_HAS_STACK_TRACE to 0 for foreign-standard-library builds. The refactor is small, but the new fallback path still leaks a synthetic <Empty trace> string into user-visible exception formatting instead of behaving like "no throw-site stack trace", so I would not approve it as-is.

Findings

⚠️ Majors

  • [src/Common/Exception.cpp:222] On the new STD_EXCEPTION_HAS_STACK_TRACE=0 path, getStackTraceOfThrow returns an empty span, but StackTrace::toString(nullptr, 0, 0) formats <Empty trace> instead of "". That makes every caller that expects "no stack trace" emit a fake stack-trace section or persist the placeholder text into log fields. Suggested fix: return {} when trace.empty() in getExceptionStackTraceString, and likewise avoid prefixing Exception::getStackTraceString with StackTrace::toString(...) when the throw-site trace is absent.
Final Verdict

Status: ⚠️ Request changes

  • Handle the empty-trace case without formatting <Empty trace> so the new STD_EXCEPTION_HAS_STACK_TRACE=0 fallback really behaves like "no throw-site stack trace".

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Aug 1, 2026
Comment thread src/Common/Exception.cpp
__msan_unpoison(stack_trace_frames, stack_trace_size * sizeof(stack_trace_frames[0]));
return StackTrace::toString(stack_trace_frames, 0, stack_trace_size);
const auto trace = getStackTraceOfThrow(e);
return StackTrace::toString(trace.data(), 0, trace.size());

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.

On the new STD_EXCEPTION_HAS_STACK_TRACE=0 path, getStackTraceOfThrow returns an empty span here, but StackTrace::toString(nullptr, 0, 0) does not return an empty string: it formats <Empty trace> in StackTrace.cpp. That leaks into every caller that uses this helper, so the fallback build now emits a fake "Stack trace" section instead of behaving like "no throw-site stack trace", and log fields that expect an absent trace get the literal placeholder text instead. Please special-case trace.empty() here (and in Exception::getStackTraceString) so the new build mode degrades cleanly instead of producing misleading diagnostics.

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.50% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.70% -0.10%

Changed lines: Changed C/C++ lines covered: 27/35 (77.14%) · Uncovered code

Full report · Diff report

@thevar1able thevar1able self-assigned this Aug 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 1, 2026
Merged via the queue into master with commit 82383c8 Aug 1, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the exception-stack-trace-optional branch August 1, 2026 14:07
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement 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