Repository navigation
Fix a permanent hang on macOS in libc time zone functions under profiler signals - #125369
Conversation
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]>
Condense the libc_timezone_shim.c header and narrow the recursive-mutex comment to what recursion actually guarantees. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings; 1 full pass).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261012-092400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261012-083600 |
|
Workflow [PR], commit [fc33df0] Summary: ❌
AI ReviewSummaryThis PR extends the Darwin compatibility shim by force-loading wrappers around the libc Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline. |
CI finish ledger - fc33df0Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:clickhouse-maint-slot-15:20261012-204200 |
Algunenano
left a comment
There was a problem hiding this comment.
I've been talking with Claude about alternatives to avoid playing this whack-a-mole game with Apple's libraries (BTW, they've never replied to the original ticket), and maybe we could replace sampling on Darwin and not use signals.
I think I'll just merge this to reduce the impact (but it's likely some other functions will appear and cause troubles) and experiment with a different sample.
bb844df
Related: #117253
Related: #112267
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a permanent hang of the server on macOS, where threads that format a time (for example while signing S3 requests, reporting an exceeded quota or reloading a dictionary) could wait forever on the C library's internal time zone lock while the query profiler was enabled.
Description
On 26.9 (macOS), a server running queries with the default query profiler can hang permanently inside libc's time functions. Seen in Fast test (arm_darwin) on #117253: three threads wait in
strftime_l->_st_tzset_basic->_pthread_rwlock_lock_slow(S3 request signing,throwQuotaExceed, andExternalLoadercalculateNextUpdateTime), no thread holds the lock, and every laterCREATE/DROP DATABASEblocks behind them.macOS libc guards its time zone state with a private
pthread_rwlock(lcl_rwlock), taken bystrftime,localtime(_r),mktime,strptime,tzsetand a few others. It is the psynch rwlock that loses wakeups when its waiters are interrupted by signals (FB24027930), and the profiler signals every registered thread. #112267 replacedpthread_rwlock_*for code linked into the binary, but libsystem_c binds its own lock to the system implementation through the two-level namespace.base/darwin-compatibility/libc_timezone_shim.cdefines the 16 public libc functions that take that lock; each calls the real one (dlsym(RTLD_NEXT)) under one recursive mutex. It is force-loaded with the rwlock shim, so every caller in the binary (cctz, the AWS SDK and other contribs) binds to it, and at most one of these threads is inside libc's time zone code at a time, so none of them waits on that lock. Calls made inside Apple frameworks are not covered, the same boundary as #112267. Cost: these calls no longer run concurrently on macOS; they take microseconds, and ClickHouse's date and time functions do not use them. Linux is unaffected.There is no test, as for #112267: the hang needs libc's writer path, which no query can trigger, and CI runs only the fast test on macOS. In a cross-built arm64 binary these functions are no longer imported from libSystem (8 before), and the
cctzand AWS SDK call sites branch to the new definitions.Workflow [PR]
Sync PR [sync-upstream/pr/125369]
Version info
26.10.1.2257-master(included in26.10and later)