macOS: uv_cond_destroy hidden drain pairs the condition with a foreign mutex type — psynch half-range stall / SIGKILL under PTHREAD_MUTEX_USE_ULOCK
Summary
On Darwin, uv_cond_destroy() performs a hidden "drain": it creates a temporary pthread_mutex_t via pthread_mutex_init(NULL), locks it, and calls pthread_cond_timedwait_relative_np(cond, &tmp, 1ns) before destroying the condition. This pairs the condition variable — for one wait operation — with a mutex whose type (and therefore kernel wait implementation) can differ from the mutex the condition was actually waited on. We observe two failure modes from this mismatch: a threadpool stall with an exact psynch half-range generation divergence, and a reproducible SIGKILL when destroying conditions paired with mismatched mutex implementations.
Environment
- macOS 26.6.2 (25G83), arm64
- Node.js v24.21.0, vendored libuv 1.52.1 (also reproduced on v24.11 / uv 1.51)
- Reproducible with and without
PTHREAD_MUTEX_USE_ULOCK=1; the flag changes which failure mode is observable
Observed symptom (production process)
A long-running Node.js process intermittently wedges its libuv threadpool: all four workers sit in __psynch_cvwait while the work queue is non-empty and the completion queue stays empty. The JS event loop remains alive; HTTP endpoints that do not touch the pool still respond, so health checks can pass while every async FS/DNS/crypto operation silently stalls. In the strongest captured sample, the psynch generation difference between the oldest and newest waiting worker is exactly 0x80000000 — half of the 32-bit generation range (2^23 steps of 256).
Root cause hypothesis
The drain's temporary mutex is DEFAULT type; under the Apple opt-in PTHREAD_MUTEX_USE_ULOCK=1 it resolves to the ulock path, while uv_mutex_init uses PTHREAD_MUTEX_ERRORCHECK (psynch path) and consumers may hold raw DEFAULT mutexes directly. Pairing a condition with a mutex of a different implementation than its waiters' appears to age/corrupt the condition's kernel registration so subsequent waiters park on a stale generation — the half-range divergence above.
Laboratory evidence
- Aging a condition through
pthread_cond_timedwaiton a different mutex than its waiters reproduces the same half-range signature: workers park in__psynch_cvwait, work queue non-empty, no completions. - Routing the pool mutex to the ulock path avoids the stall in the lab.
- Destroying a condition that was waited on via a raw DEFAULT/ulock mutex through the hidden-drain path crashes the process with SIGKILL, 5/5 runs.
- Direct
pthread_cond_destroy(no drain) passes the full lifecycle matrix on this host, including signal/broadcast-without-waiter and same-address cond → contended mutex → cond reuse.
Honest limitation: the lab ages the condition via timeout waits; the production workload presumably ages it via ordinary work/signal cycles. The initiating kernel transition has never been captured live. Timeout-aging is not signal-aging; treat the mechanism as strongly evidenced, not proven.
Proposed patch
Three files vs node-v24.21.0 vendored libuv 1.52.1 (applies to libuv v1.x):
src/unix/thread.c: split the drain into a privateuv__cond_destroy_with_mutex(cond, mutex)that drains with the caller's mutex — the same mutex the condition was waited on. Publicuv_cond_destroy()becomes a directpthread_cond_destroy+ abort on all platforms, removing the hidden foreign-mutex wait.src/unix/internal.h: declare the helper (Darwin only).src/threadpool.c: on Darwin, initialize the private pool mutex with rawpthread_mutex_init(NULL)(DEFAULT — eligible for the ulock path when the process opts in) instead ofuv_mutex_init's ERRORCHECK, and destroy the pool condition viauv__cond_destroy_with_mutex(&cond, &mutex)beforeuv_mutex_destroy, so the drain uses the same mutex the workers waited on.
--- a/deps/uv/src/threadpool.c
+++ b/deps/uv/src/threadpool.c
@@ -183,8 +183,14 @@
if (threads != default_threads)
uv__free(threads);
+#if defined(__APPLE__) && defined(__MACH__)
+ /* Preserve the private condition's wait mode through its final drain. */
+ uv__cond_destroy_with_mutex(&cond, &mutex);
uv_mutex_destroy(&mutex);
+#else
+ uv_mutex_destroy(&mutex);
uv_cond_destroy(&cond);
+#endif
threads = NULL;
nthreads = 0;
@@ -228,7 +234,14 @@
if (uv_cond_init(&cond))
abort();
+#if defined(__APPLE__) && defined(__MACH__)
+ /* Allow Darwin's opt-in ulock path only for this private work queue.
+ * Keep the public uv_mutex_init error-checking contract unchanged.
+ */
+ if (pthread_mutex_init(&mutex, NULL))
+#else
if (uv_mutex_init(&mutex))
+#endif
abort();
uv__queue_init(&wq);
--- a/deps/uv/src/unix/internal.h
+++ b/deps/uv/src/unix/internal.h
@@ -82,6 +82,13 @@
# endif
#endif
+#if defined(__APPLE__) && defined(__MACH__)
+/* Caller owns an unlocked, live mutex compatible with this condition's waits;
+ * all waiters must already have joined. The mutex itself is not destroyed.
+ */
+void uv__cond_destroy_with_mutex(uv_cond_t* cond, uv_mutex_t* mutex);
+#endif
+
/*
* Define common detection for active Thread Sanitizer
* - clang uses __has_feature(thread_sanitizer)
--- a/deps/uv/src/unix/thread.c
+++ b/deps/uv/src/unix/thread.c
@@ -766,36 +766,37 @@
#endif /* defined(__APPLE__) && defined(__MACH__) */
-void uv_cond_destroy(uv_cond_t* cond) {
#if defined(__APPLE__) && defined(__MACH__)
+void uv__cond_destroy_with_mutex(uv_cond_t* cond, uv_mutex_t* mutex) {
/* It has been reported that destroying condition variables that have been
* signalled but not waited on can sometimes result in application crashes.
* See https://codereview.chromium.org/1323293005.
*/
- pthread_mutex_t mutex;
struct timespec ts;
int err;
-
- if (pthread_mutex_init(&mutex, NULL))
- abort();
- if (pthread_mutex_lock(&mutex))
- abort();
+ uv_mutex_lock(mutex);
ts.tv_sec = 0;
ts.tv_nsec = 1;
- err = pthread_cond_timedwait_relative_np(cond, &mutex, &ts);
+ err = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
if (err != 0 && err != ETIMEDOUT)
abort();
- if (pthread_mutex_unlock(&mutex))
- abort();
+ uv_mutex_unlock(mutex);
- if (pthread_mutex_destroy(&mutex))
+ if (pthread_cond_destroy(cond))
abort();
+}
#endif /* defined(__APPLE__) && defined(__MACH__) */
+void uv_cond_destroy(uv_cond_t* cond) {
+ /* No Darwin drain here: the historical workaround performed a hidden timed
+ * wait on a temporary mutex, which can pair the condition with a different
+ * wait implementation than the consumer's mutex and crash DEFAULT/ulock
+ * consumers. The private pool still drains with its own mutex through
+ * uv__cond_destroy_with_mutex. */
if (pthread_cond_destroy(cond))
abort();
}Trade-off for discussion
Public consumers lose the 2015 "destroying a signalled-but-never-waited condition can crash" workaround (https://codereview.chromium.org/1323293005). On macOS 26.6.2 the direct destroy passed every lifecycle case including signal/broadcast without waiter, but older supported OS versions may still need the drain. If the drain must stay for public consumers, it should at minimum use a mutex of the same type the consumer waits with — which the current API cannot know, arguing for the private-helper split regardless.
Validation (this host)
- Condition lifecycle matrix: 60/60 PASS (was 6x SIGKILL on the hidden-drain variant)
- Public contract: 8/8 — misuse still aborts via SIGABRT
- Pool witness: ulock control + boundary progress; psynch boundary retains the known half-range FAIL signature (contrast intact)
- Full Node async FS/DNS/crypto/Worker lifecycle: PASS with and without the ulock flag, natural exit 0
- Application-level contract suite against an isolated backend: PASS in both modes
- 2-hour live trial of a patched runtime under real load: no stall; browser load of the affected UI clean
Limitations
- Tested only on macOS 26.6.2 arm64; no claim for older macOS or other platforms (all changes are Darwin-gated)
- The live production trigger was never captured in-kernel; the causal chain is laboratory-proven, field-correlated
PTHREAD_MUTEX_USE_ULOCKis an Apple opt-in; the stall mechanism is psynch-specific, but the mismatched-pairing hazard exists without it- This is a candidate for discussion, not a finished production fix
Evidence bundle
Test harness sources, raw receipts (RED/GREEN matrices, compile/archive/link provenance), and live-trial records: https://gist.github.com/bookfoods/d643c28cb0d3940896125a8d3f9ab15b
Source: libuv/libuv