Skip to content

Work around Darwin pthread_rwlock losing wakeups under signals - #112267

Merged
Algunenano merged 6 commits into
ClickHouse:masterfrom
Algunenano:darwin-pthread-rwlock-shim
Jul 30, 2026
Merged

Algunenano merged 6 commits into
ClickHouse:masterfrom
Algunenano:darwin-pthread-rwlock-shim

Conversation

@Algunenano

@Algunenano Algunenano commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

macOS pthread_rwlock permanently deadlocks when waiting threads receive signals at a high rate: Darwin's psynch rwlock retries an interrupted kernel wait with the sequence snapshot captured before the first sleep (_pthread_rwlock_lock_wait in libpthread), so a waiter that takes EINTR while the lock generation moves on re-enters a wait that is never signaled again, and the lock wedges with no holder. Reported to Apple as FB24027930; a standalone reproducer wedges in about 2 seconds.

With the sampling query profiler enabled on macOS (#109825), the profiler's pthread_kill delivery provides exactly that signal storm, and the hottest pthread_rwlock in the process is libfiu's fail point cache lock, taken on every fail point check (ThreadStatus::attachToGroupImpl on every query start, TCPHandler::runImpl, SystemLog::flushImpl, ...). Once it wedges, every new query, connection and system log flush blocks: this is the hang half of the Fast test (arm_darwin) Server died failures (~3% of runs), which survived the crash fix #111656.

This PR interposes pthread_rwlock_* for all code linked into ClickHouse binaries on Darwin (base/darwin-compatibility): a mutex + condition variable read-write lock taken from FreeBSD libc_r (BSD-2-Clause, including its recursive-rdlock workaround), with the state stored inside pthread_rwlock_t itself and lazy initialization for statically initialized locks. Condition variables tolerate spurious wakeups, so signal interruption has no correctness role. This covers every static pthread_rwlock user at once (libfiu, OpenSSL CRYPTO_THREAD, Poco::RWLock, RocksDB, kj, ...); calls inside Apple's dylibs keep the system implementation, as they must.

Validation: the standalone reproducer runs indefinitely with the shim (wedges in 2 s without); a server under fail point churn plus profiler storm wedged twice in ~90 s on the pre-fix binary with thread dumps matching the CI hang signature exactly, and ran clean with the shim; POSIX semantics covered by a dedicated test (recursion past a blocked writer, try* under contention, destroy/re-init, pshared rejection).

Closes: #111579
Related: #111656
Related: #109825

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 permanent server hang on macOS caused by pthread_rwlock losing wakeups when the sampling query profiler is enabled (Apple bug FB24027930): pthread_rwlock is now replaced with a signal-robust implementation on Darwin.

Version info

  • Merged into: 26.8.1.435 (included in 26.8 and later)
  • Backported to: 26.7.2.42

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fff5bd6]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL IGNORED

AI Review

Summary

This PR interposes pthread_rwlock_* on Darwin to work around the lost-wakeup deadlock triggered by sampling-profiler signals, and it also disables the query profiler under Darwin TSan where the shim cannot be linked. The TSan-specific concern from the earlier review is fixed, but two previously discussed issues still hold in the current head: the shim weakens writer-priority semantics across unrelated locks, and the promised focused regression coverage is still absent from the tree.

Findings

⚠️ Majors

  • [base/darwin-compatibility/pthread_rwlock_shim.c:275-287] [dismissed by author -- https://github.com/Work around Darwin pthread_rwlock losing wakeups under signals #112267#discussion_r3665941448] The recursive-read escape is keyed to one thread-global rdlock_count, not to the current rwlock. That means a thread holding a read lock on A can bypass writer priority on an unrelated lock B, so a blocked writer on B can be starved by fresh readers that never recursively locked B at all. For a process-wide pthread_rwlock replacement this is still a semantic weakening for every in-binary Darwin user, not just the pathological self-recursion case the donor comment talks about. Suggested fix: keep the deadlock escape, but key it to same-lock recursion (or another per-lock ownership check) instead of a thread-global counter.
  • [base/darwin-compatibility/pthread_rwlock_shim.c:29-64] [dismissed by author -- https://github.com/Work around Darwin pthread_rwlock losing wakeups under signals #112267#discussion_r3665278456] The PR description says that POSIX semantics are "covered by a dedicated test", but the diff adds no in-tree coverage at all. CI only proves that the interposer builds and that unrelated suites stay green; it does not pin the contracts that matter for a process-wide override such as lazy PTHREAD_RWLOCK_INITIALIZER setup, reader/writer contention, try*, destroy/re-init, pshared rejection, and the signal-storm reproducer that motivated the change. Suggested fix: add the focused Darwin test binary the description refers to and run it in CI.
Final Verdict

Not ready to approve. I did not post a new inline batch because the remaining concerns are the already-discussed ones above rather than fresh diff-local findings, but they still hold in the current code.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 28, 2026
Comment thread cmake/darwin/default_libs.cmake Resolved
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jul 28, 2026
Comment thread cmake/darwin/default_libs.cmake Resolved
@Algunenano Algunenano removed the submodule changed At least one submodule changed in this PR. label Jul 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.30% +0.10%
Functions 92.00% 92.10% +0.10%
Branches 78.30% 78.40% +0.10%

Changed lines: Changed C/C++ lines covered: 9/10 (90.00%) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano enabled auto-merge July 30, 2026 13:42
@Algunenano
Algunenano added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit 4bb7efd Jul 30, 2026
350 of 354 checks passed
@Algunenano
Algunenano deleted the darwin-pthread-rwlock-shim branch July 30, 2026 14:11
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 30, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 30, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 30, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 30, 2026
Backport #112267 to 26.7: Work around Darwin pthread_rwlock losing wakeups under signals
This was referenced Jul 31, 2026
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 13, 2026
macOS libc guards its time zone state with a private pthread_rwlock
(`lcl_rwlock` in Libc stdtime/FreeBSD/localtime.c), taken by strftime(_l),
wcsftime(_l), strptime(_l), localtime(_r), ctime(_r), mktime, timelocal,
time2posix, posix2time, tzset and tzsetwall. It is the psynch rwlock that
loses wakeups when its waiters are interrupted by signals (FB24027930), and
the macOS query profiler signals every registered thread. The pthread_rwlock
shim from ClickHouse#112267 does not reach it, because libsystem_c binds to libpthread
through the two-level namespace.

Seen in Fast test (arm_darwin) on ClickHouse#117253: S3 request signing
(Aws::Utils::DateTime::ToGmtString), EnabledQuota::Impl::throwQuotaExceed and
ExternalLoader::LoadingDispatcher::calculateNextUpdateTime (holding the
ExternalLoader mutex) all wait in strftime_l -> _st_tzset_basic with no holder.

Define these 16 functions in the force-loaded darwin-compatibility archive.
Each resolves the libc function with dlsym(RTLD_NEXT) and calls it under one
recursive mutex, so at most one thread of the binary is inside libc's time
zone code and none waits on its rwlock. errno is preserved across the mutex,
because a contended Darwin mutex retries an interrupted __psynch_mutexwait /
__psynch_mutexdrop and can leave EINTR behind on success.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 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

4 participants