Skip to content

Fix macOS crash capturing stack traces on fiber stacks - #111656

Merged
Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:fix-macos-fiber-stacktrace
Jul 24, 2026
Merged

Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:fix-macos-fiber-stacktrace

Conversation

@Algunenano

@Algunenano Algunenano commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Fixes a server crash (SIGSEGV in Darwin's __thread_stack_pcs) on macOS/aarch64 when a stack trace is captured while a thread is running on a boost::context fiber stack — for example the memory profiler sampling an allocation on the async reader of a remote(...) query.

Root cause: macOS captures stacks with backtrace(), a frame-pointer walk, and a fiber's saved frame-pointer terminator is left uninitialized by make_fcontext, so the walk follows that junk off the fiber stack and dereferences an unmapped address. Linux is unaffected because it unwinds via CFI (libunwind), which stops at the fiber boundary. This became reachable on macOS when the profilers / trace_log were enabled there (#109825); the memory profiler's synchronous capture is unguarded, so it crashes (the query profiler's signal-handler capture already had a siglongjmp recovery).

Two changes:

  • Bump contrib/boost to clickhouse-1.90.0, which zeroes the fiber frame-pointer terminator in the arm64 make_fcontext (aarch64: zero the fiber frame-pointer terminator in make_fcontext boost#47, merged). With a null terminator, any frame-pointer walker — backtrace() in the synchronous capture (StackTrace::tryCapture(), which must stay malloc-free for allocator hooks) and in the async-signal path, lldb, sample, the OS crash reporter — stops cleanly at the fiber boundary. This is the actual fix for the crash.
  • Unify the unwind-fault recovery (the asynchronous_stack_unwinding flag + sigsetjmp/siglongjmp) into the StackTrace(ucontext_t) constructor, replacing the copies hand-rolled in QueryProfiler.cpp and StorageSystemStackTrace.cpp, so every asynchronous capture (query profiler, system.stack_trace, and the crash handler) recovers from a fault mid-unwind by dropping the capture instead of crashing. All handlers that share this thread-local recovery state — the profilers, system.stack_trace, and the deadly-signal handlers — now mutually block the corresponding signals (SIGUSR1, SIGUSR2, STACK_TRACE_SERVICE_SIGNAL) in their sa_mask so they cannot nest and clobber the shared sigjmp_buf.

Validated on macOS/aarch64: reproduced the crash with the memory profiler on remote() fiber reads, then confirmed it is gone with the fix (no crash under sustained stress; complete MemorySample traces on fibers). A standalone reproducer built against the real boost fcontext asm shows backtrace() faulting on a fiber stack while both the patched make_fcontext and CFI unwinding walk it cleanly.

The crash cannot carry an automated regression test: it reproduces only on macOS/aarch64, where CI runs no bugfix-validation jobs. CI evidence of the crash from Fast test (arm_darwin) (see #111579 for the full CI history — 20 Server died failures across 20 PRs): https://s3.amazonaws.com/clickhouse-test-reports/PRs/109356/87764df278d5fc907268a6188b6b29dc2450c658/fast_test_arm_darwin/clickhouse-server.err.log.zst

The stopgap #111595 disabled the query and memory profilers on macOS to unblock the Fast test (arm_darwin) CI; once this lands, that can be reverted to re-enable them.

Closes: #111579
Related: ClickHouse/boost#47

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a server crash on macOS (aarch64) when a stack trace was captured (for example by the memory profiler) while running on a coroutine/fiber stack.

Version info

  • Merged into: 26.8.1.97 (included in 26.8 and later)
  • Backported to: 26.7.2.20

@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3badd41]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

AI Review

Summary

This PR fixes the macOS/aarch64 fiber-stack trace fault by moving contrib/boost to the make_fcontext frame-pointer-terminator fix and by hardening the shared unwind-recovery path across the profiler, system.stack_trace, and fatal-signal handlers. I did not find a remaining correctness or safety blocker in the current head, and the earlier nested-signal / empty-sample review issues are addressed in the latest commits.

Missing context / blind spots
  • ⚠️ I could not run a focused macOS/aarch64 reproducer locally, so confidence in the original crash fix comes from the targeted boost::context asm change, the current code paths, and the green CI summary rather than a direct runtime validation on the affected platform.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default submodule changed At least one submodule changed in this PR. labels Jul 23, 2026
Comment thread src/Common/StackTrace.cpp Resolved
Comment thread src/Common/StackTrace.cpp Resolved
alexey-milovidov and others added 2 commits July 24, 2026 05:46
…e deadly-handler mask

The deadly handler captures the crashed thread's stack via `StackTrace(ucontext_t)`,
which now uses the shared thread-local unwind-fault recovery (`asynchronous_stack_unwinding`
+ `sigjmp_buf`). The profiler and `system.stack_trace` handlers already mask each other,
but a pending `SIGUSR1`/`SIGUSR2`/`STACK_TRACE_SERVICE_SIGNAL` could still interrupt the
deadly handler mid-unwind and clobber that buffer. Block them in the deadly-handler
`sa_mask` via a new `additional_masked_signals` parameter of `addSignalHandler`.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Common/QueryProfiler.cpp Resolved
Comment thread src/Common/QueryProfiler.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.20% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 48/58 (82.76%) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano enabled auto-merge July 24, 2026 15:37
@Algunenano
Algunenano added this pull request to the merge queue Jul 24, 2026
Merged via the queue into ClickHouse:master with commit 6e25534 Jul 24, 2026
176 of 178 checks passed
@Algunenano
Algunenano deleted the fix-macos-fiber-stacktrace branch July 24, 2026 16:10
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 24, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 24, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 24, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 26, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 26, 2026
Backport #111656 to 26.7: Fix macOS crash capturing stack traces on fiber stacks
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…stacktrace

Fix macOS crash capturing stack traces on fiber stacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR. v26.7-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault in __thread_stack_pcs under the macOS query/memory profiler makes Fast test (arm_darwin) fail with Server died across PRs

5 participants