Repository navigation
Work around Darwin pthread_rwlock losing wakeups under signals - #112267
Conversation
|
Workflow [PR], commit [fff5bd6] Summary: ❌
AI ReviewSummaryThis PR interposes Findings
Final VerdictNot 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. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 9/10 (90.00%) · Uncovered code |
4bb7efd
Backport #112267 to 26.7: Work around Darwin pthread_rwlock losing wakeups under signals
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]>
macOS
pthread_rwlockpermanently 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_waitin libpthread), so a waiter that takesEINTRwhile 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_killdelivery provides exactly that signal storm, and the hottestpthread_rwlockin the process is libfiu's fail point cache lock, taken on every fail point check (ThreadStatus::attachToGroupImplon 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 theFast test (arm_darwin)Server diedfailures (~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 FreeBSDlibc_r(BSD-2-Clause, including its recursive-rdlock workaround), with the state stored insidepthread_rwlock_titself and lazy initialization for statically initialized locks. Condition variables tolerate spurious wakeups, so signal interruption has no correctness role. This covers every staticpthread_rwlockuser at once (libfiu, OpenSSLCRYPTO_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):
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_rwlocklosing wakeups when the sampling query profiler is enabled (Apple bug FB24027930):pthread_rwlockis now replaced with a signal-robust implementation on Darwin.Version info
26.8.1.435(included in26.8and later)26.7.2.42