This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 44dfa6c1e Fix bthread id butex state races (#3689)
44dfa6c1e is described below

commit 44dfa6c1e5349f2420e569dc6c78083f6e268512
Author: nkysg <[email protected]>
AuthorDate: Thu Nov 12 10:06:00 2026 +0800

    Fix bthread id butex state races (#3689)
    
    * Fix bthread id butex state races
    
    * Make bthread_id_join destroy race test deterministic
    
    * test: add TSAN regression for bthread_id join destroy race
    
    * Fix bthread ID test cleanup
    
    * Fix bthread ID join test cleanup
---
 src/bthread/id.cpp           | 143 +++++++++++++++++++------------
 test/bthread_id_unittest.cpp | 200 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 288 insertions(+), 55 deletions(-)

diff --git a/src/bthread/id.cpp b/src/bthread/id.cpp
index 9a97dc8f9..b8fea9ef4 100644
--- a/src/bthread/id.cpp
+++ b/src/bthread/id.cpp
@@ -20,6 +20,7 @@
 // Date: Sun Aug  3 12:46:15 CST 2014
 
 #include <deque>
+#include "butil/atomicops.h"
 #include "butil/logging.h"
 #include "bthread/butex.h"                       // butex_*
 #include "bthread/mutex.h"
@@ -29,6 +30,11 @@
 
 namespace bthread {
 
+#if defined(UNIT_TEST)
+void (*g_bthread_id_join_before_wait_for_test)(void*) = nullptr;
+void* g_bthread_id_join_before_wait_arg_for_test = nullptr;
+#endif
+
 // This queue reduces the chance to allocate memory for deque
 template <typename T, int N>
 class SmallQueue {
@@ -107,6 +113,15 @@ struct PendingError {
     PendingError() : id(INVALID_BTHREAD_ID), error_code(0), location(nullptr) 
{}
 };
 
+inline uint32_t load_butex(const butil::atomic<int>* butex) {
+    return static_cast<uint32_t>(butex->load(butil::memory_order_relaxed));
+}
+
+inline void store_butex(butil::atomic<int>* butex, uint32_t value,
+                       butil::memory_order order = 
butil::memory_order_relaxed) {
+    butex->store(static_cast<int>(value), order);
+}
+
 struct BAIDU_CACHELINE_ALIGNMENT Id {
     // first_ver ~ locked_ver - 1: unlocked versions
     // locked_ver: locked
@@ -119,17 +134,17 @@ struct BAIDU_CACHELINE_ALIGNMENT Id {
     int (*on_error)(bthread_id_t, void*, int);
     int (*on_error2)(bthread_id_t, void*, int, const std::string&);
     const char *lock_location;
-    uint32_t* butex;
-    uint32_t* join_butex;
+    butil::atomic<int>* butex;
+    butil::atomic<int>* join_butex;
     SmallQueue<PendingError, 2> pending_q;
     
     Id() {
         // Although value of the butex(as version part of bthread_id_t)
         // does not matter, we set it to 0 to make program more deterministic.
-        butex = bthread::butex_create_checked<uint32_t>();
-        join_butex = bthread::butex_create_checked<uint32_t>();
-        *butex = 0;
-        *join_butex = 0;
+        butex = bthread::butex_create_checked<butil::atomic<int> >();
+        join_butex = bthread::butex_create_checked<butil::atomic<int> >();
+        store_butex(butex, 0);
+        store_butex(join_butex, 0);
     }
 
     ~Id() {
@@ -179,7 +194,7 @@ inline bool id_exists_with_true_negatives(bthread_id_t id) {
 uint32_t id_value(bthread_id_t id) {
     Id* const meta = address_resource(get_slot(id));
     if (meta != nullptr) {
-        return *meta->butex;
+        return load_butex(meta->butex);
     }
     return 0;  // valid version never be zero
 }
@@ -199,7 +214,7 @@ void id_status(bthread_id_t id, std::ostream &os) {
         return;
     }
     const uint32_t id_ver = bthread::get_version(id);
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     bool valid = true;
     void* data = nullptr;
     int (*on_error)(bthread_id_t, void*, int) = nullptr;
@@ -229,7 +244,7 @@ void id_status(bthread_id_t id, std::ostream &os) {
             meta->pending_q.push(front);
             pending_q.push(front);
         }
-        butex_value = *butex;
+        butex_value = load_butex(butex);
     } else {
         valid = false;
     }
@@ -333,16 +348,19 @@ static int id_create_impl(
         meta->on_error = on_error;
         meta->on_error2 = on_error2;
         CHECK(meta->pending_q.empty());
-        uint32_t* butex = meta->butex;
-        if (0 == *butex || *butex + ID_MAX_RANGE + 2 < *butex) {
+        butil::atomic<int>* butex = meta->butex;
+        uint32_t butex_value = load_butex(butex);
+        if (0 == butex_value ||
+            butex_value + ID_MAX_RANGE + 2 < butex_value) {
             // Skip 0 so that bthread_id_t is never 0
             // avoid overflow to make comparisons simpler.
-            *butex = 1;
+            store_butex(butex, 1);
+            butex_value = 1;
         }
-        *meta->join_butex = *butex;
-        meta->first_ver = *butex;
-        meta->locked_ver = *butex + 1;
-        *id = make_id(*butex, slot);
+        store_butex(meta->join_butex, butex_value);
+        meta->first_ver = butex_value;
+        meta->locked_ver = butex_value + 1;
+        *id = make_id(butex_value, slot);
         return 0;
     }
     return ENOMEM;
@@ -366,16 +384,19 @@ static int id_create_ranged_impl(
         meta->on_error = on_error;
         meta->on_error2 = on_error2;
         CHECK(meta->pending_q.empty());
-        uint32_t* butex = meta->butex;
-        if (0 == *butex || *butex + ID_MAX_RANGE + 2 < *butex) {
+        butil::atomic<int>* butex = meta->butex;
+        uint32_t butex_value = load_butex(butex);
+        if (0 == butex_value ||
+            butex_value + ID_MAX_RANGE + 2 < butex_value) {
             // Skip 0 so that bthread_id_t is never 0
             // avoid overflow to make comparisons simpler.
-            *butex = 1;
+            store_butex(butex, 1);
+            butex_value = 1;
         }
-        *meta->join_butex = *butex;
-        meta->first_ver = *butex;
-        meta->locked_ver = *butex + range;
-        *id = make_id(*butex, slot);
+        store_butex(meta->join_butex, butex_value);
+        meta->first_ver = butex_value;
+        meta->locked_ver = butex_value + range;
+        *id = make_id(butex_value, slot);
         return 0;
     }
     return ENOMEM;
@@ -409,11 +430,11 @@ int bthread_id_lock_and_reset_range_verbose(
         return EINVAL;
     }
     const uint32_t id_ver = bthread::get_version(id);
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     bool ever_contended = false;
     meta->mutex.lock();
     while (meta->has_version(id_ver)) {
-        if (*butex == meta->first_ver) {
+        if (bthread::load_butex(butex) == meta->first_ver) {
             // contended locker always wakes up the butex at unlock.
             meta->lock_location = location;
             if (range == 0) {
@@ -429,15 +450,16 @@ int bthread_id_lock_and_reset_range_verbose(
             } else {
                 meta->locked_ver = meta->first_ver + range;
             }
-            *butex = (ever_contended ? meta->contended_ver() : 
meta->locked_ver);
+            bthread::store_butex(butex, ever_contended ? meta->contended_ver()
+                                              : meta->locked_ver);
             meta->mutex.unlock();
             if (pdata) {
                 *pdata = meta->data;
             }
             return 0;
-        } else if (*butex != meta->unlockable_ver()) {
-            *butex = meta->contended_ver();
-            uint32_t expected_ver = *butex;
+        } else if (bthread::load_butex(butex) != meta->unlockable_ver()) {
+            const uint32_t expected_ver = meta->contended_ver();
+            bthread::store_butex(butex, expected_ver);
             meta->mutex.unlock();
             ever_contended = true;
             if (bthread::butex_wait(butex, expected_ver, nullptr) < 0 &&
@@ -465,19 +487,21 @@ int bthread_id_about_to_destroy(bthread_id_t id) {
         return EINVAL;
     }
     const uint32_t id_ver = bthread::get_version(id);
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     meta->mutex.lock();
     if (!meta->has_version(id_ver)) {
         meta->mutex.unlock();
         return EINVAL;
     }
-    if (*butex == meta->first_ver) {
+    if (bthread::load_butex(butex) == meta->first_ver) {
         meta->mutex.unlock();
         LOG(FATAL) << "bthread_id=" << id.value << " is not locked!";
         return EPERM;
     }
-    const bool contended = (*butex == meta->contended_ver());
-    *butex = meta->unlockable_ver();
+    const bool contended =
+        (bthread::load_butex(butex) == meta->contended_ver());
+    bthread::store_butex(butex, meta->unlockable_ver(),
+                         butil::memory_order_release);
     meta->mutex.unlock();
     if (contended) {
         // wake up all waiting lockers.
@@ -491,20 +515,21 @@ int bthread_id_cancel(bthread_id_t id) {
     if (!meta) {
         return EINVAL;
     }
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     const uint32_t id_ver = bthread::get_version(id);
     meta->mutex.lock();
     if (!meta->has_version(id_ver)) {
         meta->mutex.unlock();
         return EINVAL;
     }
-    if (*butex != meta->first_ver) {
+    if (bthread::load_butex(butex) != meta->first_ver) {
         meta->mutex.unlock();
         return EPERM;
     }       
-    *butex = meta->end_ver();
-    meta->first_ver = *butex;
-    meta->locked_ver = *butex;
+    const uint32_t next_ver = meta->end_ver();
+    bthread::store_butex(butex, next_ver, butil::memory_order_release);
+    meta->first_ver = next_ver;
+    meta->locked_ver = next_ver;
     meta->mutex.unlock();
     return_resource(bthread::get_slot(id));
     return 0;
@@ -518,15 +543,21 @@ int bthread_id_join(bthread_id_t id) {
         return EINVAL;
     }
     const uint32_t id_ver = bthread::get_version(id);
-    uint32_t* join_butex = meta->join_butex;
+    butil::atomic<int>* join_butex = meta->join_butex;
     while (1) {
         meta->mutex.lock();
         const bool has_ver = meta->has_version(id_ver);
-        const uint32_t expected_ver = *join_butex;
+        const uint32_t expected_ver = bthread::load_butex(join_butex);
         meta->mutex.unlock();
         if (!has_ver) {
             break;
         }
+#if defined(UNIT_TEST)
+        if (bthread::g_bthread_id_join_before_wait_for_test != nullptr) {
+            bthread::g_bthread_id_join_before_wait_for_test(
+                bthread::g_bthread_id_join_before_wait_arg_for_test);
+        }
+#endif
         if (bthread::butex_wait(join_butex, expected_ver, nullptr) < 0 &&
             errno != EWOULDBLOCK && errno != EINTR) {
             return errno;
@@ -540,18 +571,18 @@ int bthread_id_trylock(bthread_id_t id, void** pdata) {
     if (!meta) {
         return EINVAL;
     }
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     const uint32_t id_ver = bthread::get_version(id);
     meta->mutex.lock();
     if (!meta->has_version(id_ver)) {
         meta->mutex.unlock();
         return EINVAL;
     }
-    if (*butex != meta->first_ver) {
+    if (bthread::load_butex(butex) != meta->first_ver) {
         meta->mutex.unlock();
         return EBUSY;
     }
-    *butex = meta->locked_ver;
+    bthread::store_butex(butex, meta->locked_ver);
     meta->mutex.unlock();
     if (pdata != nullptr) {
         *pdata = meta->data;
@@ -569,7 +600,7 @@ int bthread_id_unlock(bthread_id_t id) {
     if (!meta) {
         return EINVAL;
     }
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     // Release fence makes sure all changes made before signal visible to
     // woken-up waiters.
     const uint32_t id_ver = bthread::get_version(id);
@@ -579,7 +610,7 @@ int bthread_id_unlock(bthread_id_t id) {
         LOG(FATAL) << "Invalid bthread_id=" << id.value;
         return EINVAL;
     }
-    if (*butex == meta->first_ver) {
+    if (bthread::load_butex(butex) == meta->first_ver) {
         meta->mutex.unlock();
         LOG(FATAL) << "bthread_id=" << id.value << " is not locked!";
         return EPERM;
@@ -595,8 +626,10 @@ int bthread_id_unlock(bthread_id_t id) {
                                    front.error_text);
         }
     } else {
-        const bool contended = (*butex == meta->contended_ver());
-        *butex = meta->first_ver;
+        const bool contended =
+            (bthread::load_butex(butex) == meta->contended_ver());
+        bthread::store_butex(butex, meta->first_ver,
+                             butil::memory_order_release);
         meta->mutex.unlock();
         if (contended) {
             // We may wake up already-reused id, but that's OK.
@@ -611,8 +644,8 @@ int bthread_id_unlock_and_destroy(bthread_id_t id) {
     if (!meta) {
         return EINVAL;
     }
-    uint32_t* butex = meta->butex;
-    uint32_t* join_butex = meta->join_butex;
+    butil::atomic<int>* butex = meta->butex;
+    butil::atomic<int>* join_butex = meta->join_butex;
     const uint32_t id_ver = bthread::get_version(id);
     meta->mutex.lock();
     if (!meta->has_version(id_ver)) {
@@ -620,14 +653,14 @@ int bthread_id_unlock_and_destroy(bthread_id_t id) {
         LOG(FATAL) << "Invalid bthread_id=" << id.value;
         return EINVAL;
     }
-    if (*butex == meta->first_ver) {
+    if (bthread::load_butex(butex) == meta->first_ver) {
         meta->mutex.unlock();
         LOG(FATAL) << "bthread_id=" << id.value << " is not locked!";
         return EPERM;
     }
     const uint32_t next_ver = meta->end_ver();
-    *butex = next_ver;
-    *join_butex = next_ver;
+    bthread::store_butex(butex, next_ver, butil::memory_order_release);
+    bthread::store_butex(join_butex, next_ver, butil::memory_order_release);
     meta->first_ver = next_ver;
     meta->locked_ver = next_ver;
     meta->pending_q.clear();
@@ -711,14 +744,14 @@ int bthread_id_error2_verbose(bthread_id_t id, int 
error_code,
         return EINVAL;
     }
     const uint32_t id_ver = bthread::get_version(id);
-    uint32_t* butex = meta->butex;
+    butil::atomic<int>* butex = meta->butex;
     meta->mutex.lock();
     if (!meta->has_version(id_ver)) {
         meta->mutex.unlock();
         return EINVAL;
     }
-    if (*butex == meta->first_ver) {
-        *butex = meta->locked_ver;
+    if (bthread::load_butex(butex) == meta->first_ver) {
+        bthread::store_butex(butex, meta->locked_ver);
         meta->lock_location = location;
         meta->mutex.unlock();
         if (meta->on_error) {
diff --git a/test/bthread_id_unittest.cpp b/test/bthread_id_unittest.cpp
index 59352ba5d..e10b74e5a 100644
--- a/test/bthread_id_unittest.cpp
+++ b/test/bthread_id_unittest.cpp
@@ -16,6 +16,10 @@
 // under the License.
 
 #include <iostream>
+#include <atomic>
+#include <cstdio>
+#include <cstdlib>
+#include <thread>
 #include <gtest/gtest.h>
 #include "butil/time.h"
 #include "butil/macros.h"
@@ -26,6 +30,8 @@
 namespace bthread {
 void id_status(bthread_id_t, std::ostream &);
 uint32_t id_value(bthread_id_t id);
+extern void (*g_bthread_id_join_before_wait_for_test)(void*);
+extern void* g_bthread_id_join_before_wait_arg_for_test;
 }
 
 namespace {
@@ -112,6 +118,200 @@ TEST(BthreadIdTest, join_before_destroy) {
     ASSERT_EQ(1UL, non_null_ret);
 }
 
+struct JoinRaceArgs {
+    bthread_id_t id;
+    std::atomic<bool> join_butex_read;
+    std::atomic<bool> allow_wait;
+};
+
+class JoinBeforeWaitHookGuard {
+public:
+    JoinBeforeWaitHookGuard(bthread_id_t id, void (*hook)(void*), void* arg,
+                            std::atomic<bool>* release_waiter)
+        : _id(id)
+        , _release_waiter(release_waiter)
+        , _waiter_started(false)
+        , _waiter_joined(false)
+        , _id_locked(false)
+        , _id_destroyed(false) {
+        bthread::g_bthread_id_join_before_wait_arg_for_test = arg;
+        bthread::g_bthread_id_join_before_wait_for_test = hook;
+    }
+
+    ~JoinBeforeWaitHookGuard() {
+        const int destroy_rc = destroy_id();
+        if (destroy_rc != 0) {
+            std::fprintf(stderr, "Failed to destroy bthread ID: %d\n", 
destroy_rc);
+            std::abort();
+        }
+        release_waiter();
+        const int join_rc = join_waiter();
+        if (join_rc != 0) {
+            std::fprintf(stderr, "Failed to join waiter: %d\n", join_rc);
+            std::abort();
+        }
+        bthread::g_bthread_id_join_before_wait_for_test = nullptr;
+        bthread::g_bthread_id_join_before_wait_arg_for_test = nullptr;
+    }
+
+    int start_waiter(void* (*routine)(void*), void* arg) {
+        const int rc = pthread_create(&_waiter, nullptr, routine, arg);
+        if (rc == 0) {
+            _waiter_started = true;
+        }
+        return rc;
+    }
+
+    int lock_id(void** data) {
+        const int rc = bthread_id_trylock(_id, data);
+        if (rc == 0) {
+            _id_locked = true;
+        }
+        return rc;
+    }
+
+    int destroy_id() {
+        if (_id_destroyed) {
+            return 0;
+        }
+        if (!_id_locked) {
+            const int rc = lock_id(nullptr);
+            if (rc != 0) {
+                return rc;
+            }
+        }
+        const int rc = bthread_id_unlock_and_destroy(_id);
+        if (rc == 0) {
+            _id_locked = false;
+            _id_destroyed = true;
+        }
+        return rc;
+    }
+
+    void release_waiter() {
+        if (_release_waiter != nullptr) {
+            _release_waiter->store(true, std::memory_order_release);
+        }
+    }
+
+    int join_waiter() {
+        if (!_waiter_started || _waiter_joined) {
+            return 0;
+        }
+        const int rc = pthread_join(_waiter, nullptr);
+        if (rc == 0) {
+            _waiter_joined = true;
+        }
+        return rc;
+    }
+
+private:
+    bthread_id_t _id;
+    std::atomic<bool>* _release_waiter;
+    pthread_t _waiter;
+    bool _waiter_started;
+    bool _waiter_joined;
+    bool _id_locked;
+    bool _id_destroyed;
+    DISALLOW_COPY_AND_ASSIGN(JoinBeforeWaitHookGuard);
+};
+
+void pause_join_before_wait(void* arg) {
+    JoinRaceArgs* args = static_cast<JoinRaceArgs*>(arg);
+    args->join_butex_read.store(true, std::memory_order_release);
+    while (!args->allow_wait.load(std::memory_order_acquire)) {
+        std::this_thread::yield();
+    }
+}
+
+void* join_race_waiter(void* arg) {
+    JoinRaceArgs* args = static_cast<JoinRaceArgs*>(arg);
+    EXPECT_EQ(0, bthread_id_join(args->id));
+    return nullptr;
+}
+
+TEST(BthreadIdTest, join_hook_guard_cleans_up_on_scope_exit) {
+    bthread_id_t id;
+    ASSERT_EQ(0, bthread_id_create(&id, nullptr, nullptr));
+    JoinRaceArgs args;
+    args.id = id;
+    args.join_butex_read.store(false, std::memory_order_relaxed);
+    args.allow_wait.store(false, std::memory_order_relaxed);
+    {
+        JoinBeforeWaitHookGuard guard(id, pause_join_before_wait, &args,
+                                      &args.allow_wait);
+        ASSERT_EQ(0, guard.start_waiter(join_race_waiter, &args));
+        while (!args.join_butex_read.load(std::memory_order_acquire)) {
+            std::this_thread::yield();
+        }
+    }
+    EXPECT_EQ(EINVAL, bthread_id_trylock(id, nullptr));
+    EXPECT_EQ(nullptr, bthread::g_bthread_id_join_before_wait_for_test);
+    EXPECT_EQ(nullptr, bthread::g_bthread_id_join_before_wait_arg_for_test);
+}
+
+TEST(BthreadIdTest, join_races_with_destroy) {
+    bthread_id_t id;
+    ASSERT_EQ(0, bthread_id_create(&id, nullptr, nullptr));
+    JoinRaceArgs args;
+    args.id = id;
+    args.join_butex_read.store(false, std::memory_order_relaxed);
+    args.allow_wait.store(false, std::memory_order_relaxed);
+    JoinBeforeWaitHookGuard hook_guard(id, pause_join_before_wait, &args,
+                                       &args.allow_wait);
+
+    ASSERT_EQ(0, hook_guard.start_waiter(join_race_waiter, &args));
+    while (!args.join_butex_read.load(std::memory_order_acquire)) {
+        std::this_thread::yield();
+    }
+    ASSERT_EQ(0, hook_guard.lock_id(nullptr));
+    ASSERT_EQ(0, hook_guard.destroy_id());
+    hook_guard.release_waiter();
+    ASSERT_EQ(0, hook_guard.join_waiter());
+}
+
+struct JoinTsanRaceArgs {
+    bthread_id_t id;
+    std::atomic<bool> waiter_ready;
+    std::atomic<bool> start;
+};
+
+void race_join_with_destroy(void* arg) {
+    JoinTsanRaceArgs* args = static_cast<JoinTsanRaceArgs*>(arg);
+    args->waiter_ready.store(true, std::memory_order_relaxed);
+    while (!args->start.load(std::memory_order_relaxed)) {
+        std::this_thread::yield();
+    }
+}
+
+void* join_tsan_race_waiter(void* arg) {
+    JoinTsanRaceArgs* args = static_cast<JoinTsanRaceArgs*>(arg);
+    EXPECT_EQ(0, bthread_id_join(args->id));
+    return nullptr;
+}
+
+TEST(BthreadIdTest, join_destroy_state_access_is_tsan_safe) {
+    bthread_id_t id;
+    ASSERT_EQ(0, bthread_id_create(&id, nullptr, nullptr));
+    JoinTsanRaceArgs args;
+    args.id = id;
+    args.waiter_ready.store(false, std::memory_order_relaxed);
+    args.start.store(false, std::memory_order_relaxed);
+    JoinBeforeWaitHookGuard hook_guard(id, race_join_with_destroy, &args,
+                                       &args.start);
+
+    ASSERT_EQ(0, hook_guard.start_waiter(join_tsan_race_waiter, &args));
+    while (!args.waiter_ready.load(std::memory_order_relaxed)) {
+        std::this_thread::yield();
+    }
+    ASSERT_EQ(0, hook_guard.lock_id(nullptr));
+    // Do not establish happens-before between destroy's store and the state
+    // load in butex_wait(). TSAN detects a race here if join_butex is plain.
+    args.start.store(true, std::memory_order_relaxed);
+    ASSERT_EQ(0, hook_guard.destroy_id());
+    ASSERT_EQ(0, hook_guard.join_waiter());
+}
+
 struct OnResetArg {
     bthread_id_t id;
     int error_code;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to