Merge bitcoin/bitcoin#36556: depends: fix non-atomic reads in capnp debug assertions
What changed, and why it matters
This commit adds two upstream patches to Bitcoin Core's build of the Cap'n Proto library. The patches fix debug-only assertions that read shared memory values without using atomic operations. These non-atomic reads were flagged by ThreadSanitizer as data races once KJ objects communicate across threads. The fix only affects debug/TSan builds and does not change release behavior; it is a correctness and testing-infrastructure improvement rather than a user-facing security bug.
No urgent action required. The change is a build-system patch update. Ensure CI TSan builds pick up the new patches and verify no new warnings appear. Consider updating to a future Cap'n Proto release that includes these fixes natively.
Security signals we found
Fixes ThreadSanitizer-reported data races in dependency debug assertions
Backports upstream capnproto fixes (#2781 and #2843) to 1.x branch
Only affects capnp built without optimization / with debug assertions enabled
No change to release-build atomic synchronization logic
Triggered by cross-thread KJ communication in libmultiprocess
Evidence from the diff
The commit applies two backported Cap’n Proto patches to depends/packages/capnp.mk. The first patch (b91a6ba7c7) replaces a direct read of AtomicRefcounted::refcount in kj::atomicAddRef() debug assertions with __atomic_load_n / MSVC interlocked reads. The second patch (cc13c8c45f) replaces direct reads of Mutex::futex in KJ_DASSERT/KJ_ASSERT with __atomic_load_n. These changes eliminate TSan-reported data races in unoptimized/debug builds where assertions are enabled. The underlying logic in release builds already uses atomic operations; only the assertion-side reads were non-atomic.
Changed components
depends/packages/capnp.mkdepends/patches/capnp/kj_atomicaddref_atomic_read.patchdepends/patches/capnp/kj_mutex_atomic_futex_reads.patchCap'n Proto KJ library (refcount.h, mutex.c++)Inspect captured patch +174 / −1
### depends/packages/capnp.mk
@@ -5,6 +5,8 @@ $(package)_download_file=$(native_$(package)_download_file)
$(package)_file_name=$(native_$(package)_file_name)
$(package)_sha256_hash=$(native_$(package)_sha256_hash)
$(package)_patches=macos_accept_dead_socket.patch
+$(package)_patches += kj_atomicaddref_atomic_read.patch
+$(package)_patches += kj_mutex_atomic_futex_reads.patch
define $(package)_set_vars
$(package)_config_opts := -DBUILD_TESTING=OFF
@@ -14,7 +16,9 @@ define $(package)_set_vars
endef
define $(package)_preprocess_cmds
- patch -p2 < $($(package)_patch_dir)/macos_accept_dead_socket.patch
+ patch -p2 < $($(package)_patch_dir)/macos_accept_dead_socket.patch && \
+ patch -p2 < $($(package)_patch_dir)/kj_atomicaddref_atomic_read.patch && \
+ patch -p2 < $($(package)_patch_dir)/kj_mutex_atomic_futex_reads.patch
endef
define $(package)_config_cmds
### depends/patches/capnp/kj_atomicaddref_atomic_read.patch
@@ -0,0 +1,104 @@
+From b91a6ba7c7dbe1e5f70fff7660a01d7155404618 Mon Sep 17 00:00:00 2001
+From: xyzconstant <263061129+xyzconstant@users.noreply.github.com>
+Date: Sun, 11 Oct 2026 22:10:44 -0300
+Subject: [PATCH] kj: fix atomicAddRef non-atomic read
+
+Backport of capnproto/capnproto#2781 from v2 to 1.x.
+
+Uses __atomic_load_n (and the MSVC interlocked equivalent) because
+kj/atomic.h (#2780) is not available on 1.x. The "Arc concurrent final
+release is synchronized" test is not included because kj::Arc does not
+exist on 1.x.
+---
+ c++/src/kj/refcount-test.c++ | 31 +++++++++++++++++++++++++++++++
+ c++/src/kj/refcount.h | 12 ++++++++++--
+ 2 files changed, 41 insertions(+), 2 deletions(-)
+
+diff --git a/c++/src/kj/refcount-test.c++ b/c++/src/kj/refcount-test.c++
+index c3b39d45..8ea3a2ac 100644
+--- a/c++/src/kj/refcount-test.c++
++++ b/c++/src/kj/refcount-test.c++
+@@ -20,8 +20,12 @@
+ // THE SOFTWARE.
+
+ #include "refcount.h"
++#include "thread.h"
+ #include <kj/compat/gtest.h>
+
++#include <atomic>
++#include <thread>
++
+ namespace kj {
+
+ struct SetTrueInDestructor: public Refcounted {
+@@ -134,4 +138,31 @@ KJ_TEST("RefcountedWrapper") {
+ }
+ }
+
++struct AtomicSetTrueInDestructor: public AtomicRefcounted {
++ AtomicSetTrueInDestructor(bool* ptr): ptr(ptr) {}
++ ~AtomicSetTrueInDestructor() { *ptr = true; }
++
++ bool* ptr;
++};
++
++KJ_TEST("atomicAddRef is safe under concurrent reference-count changes") {
++ bool destroyed = false;
++ auto owner = kj::atomicRefcounted<AtomicSetTrueInDestructor>(&destroyed);
++ auto* object = owner.get();
++ std::atomic<bool> start(false);
++
++ auto addRefs = [&]() noexcept {
++ while (!start.load(std::memory_order_acquire)) {
++ std::this_thread::yield();
++ }
++ for (uint i = 0; i < 1000; ++i) {
++ auto ref = kj::atomicAddRef(*object);
++ }
++ };
++
++ kj::Thread thread1(addRefs);
++ kj::Thread thread2(addRefs);
++ start.store(true, std::memory_order_release);
++}
++
+ } // namespace kj
+diff --git a/c++/src/kj/refcount.h b/c++/src/kj/refcount.h
+index 03b5234d..722fca3f 100644
+--- a/c++/src/kj/refcount.h
++++ b/c++/src/kj/refcount.h
+@@ -204,6 +204,14 @@ private:
+
+ bool addRefWeakInternal() const;
+
++ inline bool hasRefs() const {
++#if _MSC_VER && !defined(__clang__)
++ return KJ_MSVC_INTERLOCKED(Or, acq)(&refcount, 0) > 0;
++#else
++ return __atomic_load_n(&refcount, __ATOMIC_RELAXED) > 0;
++#endif
++ }
++
+ void disposeImpl(void* pointer) const override;
+ template <typename T>
+ static kj::Own<T> addRefInternal(T* object);
+@@ -227,14 +235,14 @@ inline kj::Own<T> atomicRefcounted(Params&&... params) {
+
+ template <typename T>
+ kj::Own<T> atomicAddRef(T& object) {
+- KJ_IREQUIRE(object.AtomicRefcounted::refcount > 0,
++ KJ_IREQUIRE(object.AtomicRefcounted::hasRefs(),
+ "Object not allocated with kj::atomicRefcounted().");
+ return AtomicRefcounted::addRefInternal(&object);
+ }
+
+ template <typename T>
+ kj::Own<const T> atomicAddRef(const T& object) {
+- KJ_IREQUIRE(object.AtomicRefcounted::refcount > 0,
++ KJ_IREQUIRE(object.AtomicRefcounted::hasRefs(),
+ "Object not allocated with kj::atomicRefcounted().");
+ return AtomicRefcounted::addRefInternal(&object);
+ }
+--
+2.54.0 (Apple Git-157)
+
### depends/patches/capnp/kj_mutex_atomic_futex_reads.patch
@@ -0,0 +1,65 @@
+From cc13c8c45fa25de7872709ca8d2009da8abc7773 Mon Sep 17 00:00:00 2001
+From: xyzconstant <263061129+xyzconstant@users.noreply.github.com>
+Date: Sun, 11 Oct 2026 22:13:18 -0300
+Subject: [PATCH] kj: Fix non-atomic reads of futex state
+
+Backport of capnproto/capnproto#2843 from v2 to 1.x.
+
+Uses __atomic_load_n because kj/atomic.h (#2780) is not available on 1.x.
+---
+ c++/src/kj/mutex.c++ | 13 ++++++++-----
+ 1 file changed, 8 insertions(+), 5 deletions(-)
+
+diff --git a/c++/src/kj/mutex.c++ b/c++/src/kj/mutex.c++
+index edccdf40..68d01935 100644
+--- a/c++/src/kj/mutex.c++
++++ b/c++/src/kj/mutex.c++
+@@ -345,7 +345,8 @@ bool Mutex::lock(Exclusivity exclusivity, Maybe<Duration> timeout, LockSourceLoc
+ void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) {
+ switch (exclusivity) {
+ case EXCLUSIVE: {
+- KJ_DASSERT(futex & EXCLUSIVE_HELD, "Unlocked a mutex that wasn't locked.");
++ KJ_DASSERT(__atomic_load_n(&futex, __ATOMIC_RELAXED) & EXCLUSIVE_HELD,
++ "Unlocked a mutex that wasn't locked.");
+
+ #ifdef KJ_CONTENTION_WARNING_THRESHOLD
+ auto acquiredLocation = releasingExclusive();
+@@ -427,7 +428,8 @@ void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) {
+ }
+
+ case SHARED: {
+- KJ_DASSERT(futex & SHARED_COUNT_MASK, "Unshared a mutex that wasn't shared.");
++ KJ_DASSERT(__atomic_load_n(&futex, __ATOMIC_RELAXED) & SHARED_COUNT_MASK,
++ "Unshared a mutex that wasn't shared.");
+ uint state = __atomic_sub_fetch(&futex, 1, __ATOMIC_RELEASE);
+
+ // The only case where anyone is waiting is if EXCLUSIVE_REQUESTED is set, and the only time
+@@ -446,13 +448,14 @@ void Mutex::unlock(Exclusivity exclusivity, Waiter* waiterToSkip) {
+ }
+
+ void Mutex::assertLockedByCaller(Exclusivity exclusivity) const {
++ // Contenders can update the futex even while the caller holds the lock.
+ switch (exclusivity) {
+ case EXCLUSIVE:
+- KJ_ASSERT(futex & EXCLUSIVE_HELD,
++ KJ_ASSERT(__atomic_load_n(&futex, __ATOMIC_RELAXED) & EXCLUSIVE_HELD,
+ "Tried to call getAlreadyLocked*() but lock is not held.");
+ break;
+ case SHARED:
+- KJ_ASSERT(futex & SHARED_COUNT_MASK,
++ KJ_ASSERT(__atomic_load_n(&futex, __ATOMIC_RELAXED) & SHARED_COUNT_MASK,
+ "Tried to call getAlreadyLocked*() but lock is not held.");
+ break;
+ }
+@@ -568,7 +571,7 @@ void Mutex::induceSpuriousWakeupForTest() {
+
+ uint Mutex::numReadersWaitingForTest() const {
+ assertLockedByCaller(EXCLUSIVE);
+- return futex & SHARED_COUNT_MASK;
++ return __atomic_load_n(&futex, __ATOMIC_RELAXED) & SHARED_COUNT_MASK;
+ }
+
+ void Once::runOnce(Initializer& init, LockSourceLocationArg location) {
+--
+2.54.0 (Apple Git-157)
+Why this scored 29/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.