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 ee79e7c55 Fix ObjectPool block item count race (#3675)
ee79e7c55 is described below

commit ee79e7c55065526e585deb54f08a741b29314604
Author: nkysg <[email protected]>
AuthorDate: Sat Nov 21 16:17:39 2026 +0800

    Fix ObjectPool block item count race (#3675)
    
    * Fix ObjectPool block item count race
    
    * Fix object pool cleanup synchronization
---
 src/brpc/versioned_ref_with_id.h |  2 +-
 src/butil/object_pool_inl.h      | 45 ++++++++++++++++++++++++++--------------
 test/object_pool_unittest.cpp    | 42 +++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/src/brpc/versioned_ref_with_id.h b/src/brpc/versioned_ref_with_id.h
index 9fc5f5266..e9e8d29c0 100644
--- a/src/brpc/versioned_ref_with_id.h
+++ b/src/brpc/versioned_ref_with_id.h
@@ -563,7 +563,7 @@ int 
VersionedRefWithId<T>::ReleaseAdditionalReferenceImpl(bool permanent) {
     do {
         AdditionalRefStatus expect = ADDITIONAL_REF_USING;
         if (_additional_ref_status.compare_exchange_strong(
-                expect, permanent ? ADDITIONAL_REF_RELEASED 
+                expect, permanent ? ADDITIONAL_REF_RELEASED
                                   : ADDITIONAL_REF_RELEASED_ON_FAILURE,
                 butil::memory_order_relaxed, butil::memory_order_relaxed)) {
             CallBeforeAdditionalRefReleased(static_cast<T*>(this));
diff --git a/src/butil/object_pool_inl.h b/src/butil/object_pool_inl.h
index 151932130..3b784c1f8 100644
--- a/src/butil/object_pool_inl.h
+++ b/src/butil/object_pool_inl.h
@@ -139,7 +139,10 @@ public:
     // To support cache-aligned objects, align Block.items by cacheline.
     struct BAIDU_CACHELINE_ALIGNMENT Block {
         BlockItem items[BLOCK_NITEM];
-        size_t nitem;
+        // Written by the owning LocalPool and read by describe_objects().
+        // Publish the count after construction so statistics never observe a
+        // partially constructed object.
+        butil::atomic<size_t> nitem;
 
         Block() : nitem(0) {}
     };
@@ -201,22 +204,29 @@ public:
         }                                                               \
         T* obj = nullptr;                                                  \
         /* Fetch memory from local block */                             \
-        if (_cur_block && _cur_block->nitem < BLOCK_NITEM) {            \
-            auto item = _cur_block->items + _cur_block->nitem;          \
-            obj = new (item->void_data()) T CTOR_ARGS;                  \
-            if (!ObjectPoolValidator<T>::validate(obj)) {               \
-                obj->~T();                                              \
-                return nullptr;                                            \
+        if (_cur_block) {                                                \
+            const size_t nitem =                                        \
+                _cur_block->nitem.load(butil::memory_order_relaxed);    \
+            if (nitem < BLOCK_NITEM) {                                  \
+                auto item = _cur_block->items + nitem;                  \
+                obj = new (item->void_data()) T CTOR_ARGS;              \
+                if (!ObjectPoolValidator<T>::validate(obj)) {           \
+                    obj->~T();                                          \
+                    return nullptr;                                     \
+                }                                                       \
+                /* It's poisoned prior to use. */                       \
+                OBJECT_POOL_ASAN_POISON_MEMORY_REGION(obj);             \
+                _cur_block->nitem.store(nitem + 1,                       \
+                                        butil::memory_order_release);    \
+                return obj;                                             \
             }                                                           \
-            /* It's poisoned prior to use. */                           \
-            OBJECT_POOL_ASAN_POISON_MEMORY_REGION(obj);                 \
-            ++_cur_block->nitem;                                        \
-            return obj;                                                 \
         }                                                               \
         /* Fetch a Block from global */                                 \
         _cur_block = add_block(&_cur_block_index);                      \
         if (_cur_block != nullptr) {                                       \
-            auto item = _cur_block->items + _cur_block->nitem;          \
+            const size_t nitem =                                        \
+                _cur_block->nitem.load(butil::memory_order_relaxed);    \
+            auto item = _cur_block->items + nitem;                      \
             obj = new (item->void_data()) T CTOR_ARGS;                  \
             if (!ObjectPoolValidator<T>::validate(obj)) {               \
                 obj->~T();                                              \
@@ -224,7 +234,8 @@ public:
             }                                                           \
             /* It's poisoned prior to use. */                           \
             OBJECT_POOL_ASAN_POISON_MEMORY_REGION(obj);                 \
-            ++_cur_block->nitem;                                        \
+            _cur_block->nitem.store(nitem + 1,                           \
+                                    butil::memory_order_release);        \
             return obj;                                                 \
         }                                                               \
         return nullptr;                                                    \
@@ -376,7 +387,7 @@ public:
             for (size_t j = 0; j < nblock; ++j) {
                 Block* b = bg->blocks[j].load(butil::memory_order_consume);
                 if (nullptr != b) {
-                    info.item_num += b->nitem;
+                    info.item_num += 
b->nitem.load(butil::memory_order_acquire);
                 }
             }
         }
@@ -451,7 +462,8 @@ private:
                 if (nullptr == b) {
                     continue;
                 }
-                for (size_t k = 0; k < b->nitem; ++k) {
+                const size_t nitem = 
b->nitem.load(butil::memory_order_acquire);
+                for (size_t k = 0; k < nitem; ++k) {
                     asan_unpoison_memory_region((T*)&b->items[k]);
                 }
             }
@@ -559,7 +571,8 @@ private:
                 if (nullptr == b) {
                     continue;
                 }
-                for (size_t k = 0; k < b->nitem; ++k) {
+                const size_t nitem = 
b->nitem.load(butil::memory_order_acquire);
+                for (size_t k = 0; k < nitem; ++k) {
                     T* obj = (T*)&b->items[k];
                     // Unpoison to avoid affecting other allocator.
                     OBJECT_POOL_ASAN_UNPOISON_MEMORY_REGION(obj);
diff --git a/test/object_pool_unittest.cpp b/test/object_pool_unittest.cpp
index b8cd8418f..9555e55f3 100644
--- a/test/object_pool_unittest.cpp
+++ b/test/object_pool_unittest.cpp
@@ -28,6 +28,7 @@
 namespace {
 struct MyObject {};
 struct FreeChunkCountObject {};
+struct DescribeRaceObject {};
 
 template <size_t Size, size_t MaxSize, size_t MaxItem = 1024>
 struct BlockSizeLimitedObject {
@@ -126,6 +127,47 @@ TEST_F(ObjectPoolTest, change_config) {
     std::cout << describe_objects<MyObject>() << std::endl;
 }
 
+struct DescribeObjectsArgs {
+    std::atomic<bool>* started;
+    std::atomic<bool>* done;
+};
+
+const size_t kDescribeRaceObjectCount = 100000;
+
+void* create_objects_while_describing(void* arg) {
+    DescribeObjectsArgs* args = static_cast<DescribeObjectsArgs*>(arg);
+    args->started->store(true, std::memory_order_release);
+    for (size_t i = 0; i < kDescribeRaceObjectCount; ++i) {
+        (void)get_object<DescribeRaceObject>();
+    }
+    args->done->store(true, std::memory_order_release);
+    return nullptr;
+}
+
+TEST_F(ObjectPoolTest, describe_while_creating_objects) {
+    // Keep the pool alive while the worker's LocalPool is being destroyed.
+    DescribeRaceObject* guard = get_object<DescribeRaceObject>();
+    ASSERT_NE(nullptr, guard);
+    std::atomic<bool> started(false);
+    std::atomic<bool> done(false);
+    DescribeObjectsArgs args = { &started, &done };
+    pthread_t tid;
+    ASSERT_EQ(0, pthread_create(&tid, nullptr,
+                                create_objects_while_describing, &args));
+    while (!started.load(std::memory_order_acquire)) {
+    }
+    while (!done.load(std::memory_order_acquire)) {
+        describe_objects<DescribeRaceObject>();
+    }
+    ASSERT_EQ(0, pthread_join(tid, nullptr));
+    // The count assertion catches non-atomic or incomplete publication on
+    // regular builds; TSan additionally detects the concurrent access race.
+    ASSERT_EQ(kDescribeRaceObjectCount + 1,
+              describe_objects<DescribeRaceObject>().item_num);
+    return_object(guard);
+    clear_objects<DescribeRaceObject>();
+}
+
 template <typename T>
 void check_block_capacity(size_t expected_capacity) {
     typedef typename ObjectPool<T>::Block Block;


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

Reply via email to