#5297·libuv

Data race in uv__close_nocancel/uv__async_stop since v1.53.0

Author: Mno-himeCreated Sep 25, 2026Updated Sep 25, 2026

Since libuv v1.53.0 we register data race on BIND 9.21/9.20 in uv__close_nocancel/uv__async_stop:

WARNING: ThreadSanitizer: data race 
  Write of size 8 at 0x000000000001 by main thread:
    #0 uv__close_nocancel /usr/src/libuv-v1.53.0/src/unix/core.c:614:3 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #1 uv__close_nocheckstdio /usr/src/libuv-v1.53.0/src/unix/core.c:633:8
    #2 uv__close /usr/src/libuv-v1.53.0/src/unix/core.c:650:10
    #3 uv__async_stop /usr/src/libuv-v1.53.0/src/unix/async.c:327:3 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #4 uv__loop_close /usr/src/libuv-v1.53.0/src/unix/loop.c:173:3 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #5 uv_loop_close /usr/src/libuv-v1.53.0/src/uv-common.c:1040:3 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #6 loop_close build/../lib/isc/loop.c:266:10 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #7 isc_loopmgr_destroy build/../lib/isc/loop.c:556:3
    #8 teardown_loopmgr build/../tests/libtest/isc.c:94:2 (BuildId: d41d3509d4ee3209b7f91ce9388cba1d5e3bc0de)
    #9 teardown_test build/../tests/dns/keytable_test.c:57:2 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #10 <null> <null> (BuildId: ace58c5f1cbe8d3e32fd8907c29af9d7e307aa91)
    #11 <null> <null> (BuildId: 5e5b51f4c39af9ac589408c1dc432ba390a273cc)

  Previous read of size 8 at 0x000000000001 by thread T0001 (mutexes: write M0001):
    #0 write <null> (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #1 uv__async_send /usr/src/libuv-v1.53.0/src/unix/async.c:175:9 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #2 uv__async_notify /usr/src/libuv-v1.53.0/src/unix/async.c:91:3
    #3 uv_async_send /usr/src/libuv-v1.53.0/src/uv-common.c:980:3 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #4 loop_destroy build/../lib/isc/loop.c:328:10 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #5 isc_loop_unref build/../lib/isc/loop.c:335:29
    #6 isc_loop_detach build/../lib/isc/loop.c:335:1 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #7 dns__nta_destroy build/../lib/dns/nta.c:99:2 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #8 dns__nta_unref build/../lib/dns/nta.c:107:29
    #9 dns__nta_detach build/../lib/dns/nta.c:107:1 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #10 qp_detach build/../lib/dns/nta.c:672:2
    #11 detach_leaf build/../lib/dns/qp_p.h:896:2 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #12 chunk_free build/../lib/dns/qp.c:699:4
    #13 destroy_guts build/../lib/dns/qp.c:1543:4 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #14 qpmulti_destroy_guts_cb build/../lib/dns/qp.c:1606:2 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #15 call_rcu_thread /usr/src/userspace-rcu-0.15.7/src/./urcu-call-rcu-impl.h:393:5 (BuildId: 1546ed8833a9f2276a70e0cdc8dfb1e89374909d)

  Location is file descriptor 11 created by main thread at:
    #0 eventfd <null> (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #1 uv__async_start /usr/src/libuv-v1.53.0/src/unix/async.c:235:9 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #2 uv_async_init /usr/src/libuv-v1.53.0/src/unix/async.c:75:9
    #3 uv_loop_init /usr/src/libuv-v1.53.0/src/unix/loop.c:97:9 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #4 loop_init build/../lib/isc/loop.c:215:10 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #5 isc_loopmgr_create build/../lib/isc/loop.c:367:3
    #6 setup_loopmgr build/../tests/libtest/isc.c:87:2 (BuildId: d41d3509d4ee3209b7f91ce9388cba1d5e3bc0de)
    #7 setup_test build/../tests/dns/keytable_test.c:50:2 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #8 <null> <null> (BuildId: ace58c5f1cbe8d3e32fd8907c29af9d7e307aa91)
    #9 <null> <null> (BuildId: 5e5b51f4c39af9ac589408c1dc432ba390a273cc)

  Mutex M0001 (0x000000000029) created at:
    #0 pthread_mutex_init <null> (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #1 dns_qpmulti_create build/../lib/dns/qp.c:1518:2 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #2 dns_ntatable_create build/../lib/dns/nta.c:125:2 (BuildId: 9e96dc4f681013a1f4cbe985bf289621618108eb)
    #3 create_tables build/../tests/dns/keytable_test.c:178:2 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #4 loop_test_find build/../tests/dns/keytable_test.c:496:2 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #5 isc__async_cb build/../lib/isc/async.c:110:3 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #6 uv__async_io /usr/src/libuv-v1.53.0/src/unix/async.c:158:5 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #7 uv__io_cb /usr/src/libuv-v1.53.0/src/unix/core.c:912:5 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #8 uv__io_poll /usr/src/libuv-v1.53.0/src/unix/linux.c:1604:11 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #9 uv_run /usr/src/libuv-v1.53.0/src/unix/core.c:460:5 (BuildId: 8c059701a5140e29c27a06a72d0df503493af4a3)
    #10 loop_thread build/../lib/isc/loop.c:305:6 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #11 thread_body build/../lib/isc/thread.c:87:8 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #12 isc_thread_main build/../lib/isc/thread.c:122:2
    #13 isc_loopmgr_run build/../lib/isc/loop.c:474:2 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #14 run_test_find build/../tests/dns/keytable_test.c:489:1 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #15 <null> <null> (BuildId: ace58c5f1cbe8d3e32fd8907c29af9d7e307aa91)
    #16 <null> <null> (BuildId: 5e5b51f4c39af9ac589408c1dc432ba390a273cc)

  Thread T0001 (running) created by main thread at:
    #0 pthread_create <null> (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #1 call_rcu_data_init /usr/src/userspace-rcu-0.15.7/src/./urcu-call-rcu-impl.h:465:8 (BuildId: 1546ed8833a9f2276a70e0cdc8dfb1e89374909d)
    #2 __create_call_rcu_data /usr/src/userspace-rcu-0.15.7/src/./urcu-call-rcu-impl.h:519:2 (BuildId: 1546ed8833a9f2276a70e0cdc8dfb1e89374909d)
    #3 urcu_memb_create_call_rcu_data /usr/src/userspace-rcu-0.15.7/src/./urcu-call-rcu-impl.h:529:9
    #4 isc__thread_initialize build/../lib/isc/thread.c:192:30 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #5 isc_loopmgr_run build/../lib/isc/loop.c:459:2 (BuildId: e91aafbc9ceb2d93c622dfb3f8b1b75d492c55fa)
    #6 run_test_find build/../tests/dns/keytable_test.c:489:1 (BuildId: e4c0313402fec16639384160d998dee3062580d7)
    #7 <null> <null> (BuildId: ace58c5f1cbe8d3e32fd8907c29af9d7e307aa91)
    #8 <null> <null> (BuildId: 5e5b51f4c39af9ac589408c1dc432ba390a273cc)

SUMMARY: ThreadSanitizer: data race /usr/src/libuv-v1.53.0/src/unix/core.c:614:3 in uv__close_nocancel

It's easily reproducible with the keytable unit test.

Here's what LLM says about it:


Since 1.53.0 (ea493f19, 10902ab6), the async busy counter is part of handle->pending. Both its decrement in uv_async_send() and its poll in uv__async_spin() are memory_order_relaxed (src/uv-common.c):

#define uv__pending_load(p) atomic_load_explicit(..., memory_order_relaxed) #define uv__pending_fetch_add(p, v) atomic_fetch_add_explicit(..., memory_order_relaxed)

uv__async_spin() still waits for a concurrent sender to finish, but it no longer synchronizes with it. So the sender's write() to the wakeup eventfd is not ordered before uv__async_stop() close()s it. In 1.52.x the separate seq_cst busy flag provided that edge.

It shows up when a foreign thread sends on an async handle and the loop is woken by another handle after that sender's CAS but before its write(). The loop then closes the handle and uv_loop_close() runs. BIND 9 does this at loop teardown, and TSAN reports:

WARNING: ThreadSanitizer: data race Write (close) uv__close_nocancel ← uv__async_stop ← uv__loop_close ← uv_loop_close Previous read (write) uv__async_send src/unix/async.c:175 ← uv_async_send

A second report is on loop->async_io_watcher.fd (async.c:328 vs :173). Both reproduce reliably with a small test program; 1.52.1 is clean.

Fix: make the decrement memory_order_release and the poll memory_order_acquire. With that, the TSAN reports are gone, libuv's test suite passes unchanged, and BIND 9's TSAN CI is green again.


And here's the LLM-generated fix that works for me with libuv 1.53.0:

diff
diff --git a/src/uv-common.c b/src/uv-common.c
index 4538c152..0816d905 100644
--- a/src/uv-common.c
+++ b/src/uv-common.c
@@ -929,8 +929,13 @@ static void uv__cpu_relax(void) {
  * and the uv_async_cb invocation sequentially consistent — all accesses
  * (reads and writes) before uv_async_send are visible to the callback.
  *
- * uv__pending_load and uv__pending_fetch_add are relaxed; they touch only the
- * busy counter and need not participate in that ordering. */
+ * uv__pending_fetch_add (the busy counter decrement in uv_async_send) is a
+ * release and uv__pending_load (the busy counter poll in uv__async_spin) is
+ * an acquire. Once uv__async_spin observes a zero busy counter, everything
+ * the sender did while busy (notably the write to the wakeup fd)
+ * happens-before the caller's subsequent teardown (notably closing that fd
+ * in uv__async_stop). On MSVC, InterlockedExchangeAdd is a full barrier and
+ * a volatile load has acquire semantics. */
 #ifdef _MSC_VER
 
 static int uv__pending_cas(int* p, int* expected, int desired) {
@@ -954,9 +959,9 @@ static int uv__pending_cas(int* p, int* expected, int desired) {
 }
 
 #define uv__pending_load(p) \
-  atomic_load_explicit((_Atomic int*)(p), memory_order_relaxed)
+  atomic_load_explicit((_Atomic int*)(p), memory_order_acquire)
 #define uv__pending_fetch_add(p, v) \
-  ((void) atomic_fetch_add_explicit((_Atomic int*)(p), (v), memory_order_relaxed))
+  ((void) atomic_fetch_add_explicit((_Atomic int*)(p), (v), memory_order_release))
 #define uv__pending_fetch_or(p, v) \
   ((int) atomic_fetch_or((_Atomic int*)(p), (v)))