Repository navigation
Make system.stack_trace work on macOS - #98982
Conversation
The `system.stack_trace` table was previously Linux-only because it
relied on `rt_tgsigqueueinfo`, real-time signals (`SIGRTMIN`), and
the `/proc` filesystem. This commit adds macOS support using
platform-specific alternatives:
- Thread enumeration via Mach `task_threads` API instead of `/proc/self/task`
- Signal delivery via `pthread_kill` with `SIGUSR1` instead of `rt_tgsigqueueinfo` with `SIGRTMIN`
- Thread name retrieval via `pthread_getname_np` instead of `/proc/self/task/{tid}/comm`
- Signal handler identifies expected thread via `pthread_self` check instead of `si_value` payload
- Raw virtual addresses in trace column (macOS lacks `SymbolIndex` which depends on `dl_iterate_phdr`)
Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Workflow [PR], commit [dc90ccd] Summary: ✅ AI Review1) SummaryThis PR adds 2) Missing context (if any)
3) Findings (by severity)
4) Tests & Evidence
5) ClickHouse Compliance Checklist (Yes/No + short note)
6) Performance & Safety Notes
7) User-Lens Review
8) Final Verdict
|
|
Note: this is only moderately useful - just the first step to make this table working. |
On Linux, `addressToSymbol` uses `SymbolIndex` which depends on `dl_iterate_phdr` and ELF parsing. On macOS, use `dladdr` from `<dlfcn.h>` instead - it resolves virtual addresses to symbol names directly. This is slower than `SymbolIndex` but works without any binary format parsing. Combined with `demangle`, this enables fully symbolized stack traces from `system.stack_trace` on macOS. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Yes, it works:
|
Unlike `rt_tgsigqueueinfo` (a syscall that sets `errno`), `pthread_kill` returns error codes directly per POSIX. The previous code checked `errno` which could hold a stale value, causing a spurious exception instead of gracefully skipping finished threads. Co-Authored-By: Claude Opus 4.6 <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
On Linux, `si_value.sival_int` is fixed at signal-send time, which allows the handler to detect stale signals. On macOS, `sequence_num` is read at handler execution time, creating a race window: if a handler is delayed past the `wait` timeout and `sequence_num` is incremented, the handler could read the new value and be mistaken for a response from the next thread. Fix by moving the `sequence_num` read and `expected_responding_thread` re-check to after the `signal_latch` CAS. Since only one handler can hold the latch at a time, this closes the window where these values could change between the initial pre-check and actual use. Co-Authored-By: Claude Opus 4.6 <[email protected]>
| errno = saved_errno; | ||
| return; | ||
| } | ||
| int notification_num = sequence_num.load(std::memory_order_acquire); |
There was a problem hiding this comment.
On macOS this uses sequence_num.load at handler execution time, so a delayed signal can be stamped with the current sequence and pass wait for a later request. That can attribute thread A's stack trace to thread B when the previous signal arrives late. Please capture a per-request token before pthread_kill and verify that exact token in the handler/pipe payload.
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 94.78% (109/115) |
Summary
system.stack_tracetable was previously Linux-only (guarded by#ifdef OS_LINUX) because it relied onrt_tgsigqueueinfo, real-time signals (SIGRTMIN), and the/procfilesystemtask_threads,pthread_from_mach_thread_np) for thread enumeration,pthread_killwithSIGUSR1for signal delivery, andpthread_getname_npfor thread namesatosorlldb) becauseSymbolIndexdepends ondl_iterate_phdrwhich is Linux-onlyTest plan
SELECT count() FROM system.stack_tracereturns all threads (757)thread_name,thread_id,query_idcolumns are populated correctlyChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
system.stack_tracetable now works on macOS, allowing introspection of all server thread stack traces.🤖 Generated with Claude Code
Note
Medium Risk
Adds macOS-specific thread enumeration and signal-handling paths to a low-level introspection table; errors here could cause missed traces, hangs/timeouts, or unintended signal interactions on macOS.
Overview
Enables
system.stack_traceon macOS by adding Darwin-specific thread enumeration (Machtask_threads+pthread_from_mach_thread_np), signal delivery (pthread_kill/SIGUSR1), and thread-name retrieval (pthread_getname_np).Updates synchronization/validation logic in the signal handler for macOS (thread identity checks instead of
sigqueuepayloads) and adjusts trace address handling to emit raw virtual addresses on macOS (Linux keepsSymbolIndex-based normalization).Extends
addressToSymbolto support macOS viadladdrwhenSymbolIndexisn’t available, and makessystem.stack_tracetable registration/header guards includeOS_DARWIN.Written by Cursor Bugbot for commit dc90ccd. This will update automatically on new commits. Configure here.
Version info
26.3.1.632