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 e20a3e0e2 Fix ListOfABAFreeId::add() dropping new id after GC (#3776)
e20a3e0e2 is described below

commit e20a3e0e25685e22bb4ee21383b7ebd25701482c
Author: Qiu <[email protected]>
AuthorDate: Thu Nov 26 11:46:20 2026 +0800

    Fix ListOfABAFreeId::add() dropping new id after GC (#3776)
    
    * Fix ListOfABAFreeId::add() dropping new id after GC
    
    * test: tighten EAGAIN check in AbaListTest.TestGcPreservesAddedIds
---
 src/bthread/list_of_abafree_id.h |  7 +++++-
 test/abalist_unittest.cc         | 46 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/src/bthread/list_of_abafree_id.h b/src/bthread/list_of_abafree_id.h
index 73c732556..5372c80e5 100644
--- a/src/bthread/list_of_abafree_id.h
+++ b/src/bthread/list_of_abafree_id.h
@@ -180,7 +180,12 @@ int ListOfABAFreeId<Id, IdTraits>::add(Id id) {
             }
         }
 
-        return rc;
+        if (rc != 0) {
+            return rc;
+        }
+        _cur_block->ids[_cur_index] = id;
+        forward_index();
+        return 0;
     }
     // The list is considered to be "crowded", add a new block and scatter
     // the conflict identifiers by inserting an empty entry after each of
diff --git a/test/abalist_unittest.cc b/test/abalist_unittest.cc
index cdbbda206..3a3d9b762 100644
--- a/test/abalist_unittest.cc
+++ b/test/abalist_unittest.cc
@@ -4,7 +4,10 @@
 
 #include <gtest/gtest.h>
 
+#include <cerrno>
+#include <cstdlib>
 #include <unordered_set>
+#include <vector>
 
 #include "bthread/list_of_abafree_id.h"
 
@@ -42,5 +45,48 @@ TEST(AbaListTest, TestGc) {
     size_t cnts[1];
     aba_list.get_sizes(cnts, 1);
     EXPECT_EQ(cnts[0], (size_t)96);
+    TestIdTraits::id_set.clear();
+}
+
+void VerifyGcPreservesAddedIds(bool churn) {
+    TestIdTraits::id_set.clear();
+    ListOfABAFreeId<int, TestIdTraits> aba_list;
+    std::vector<int> accepted;
+    unsigned int seed = 12345;
+    size_t max_cap = 0;
+    for (int id = 1000; id < 2000; ++id) {
+        size_t before_cap = 0;
+        aba_list.get_sizes(&before_cap, 1);
+        if (before_cap > max_cap) {
+            max_cap = before_cap;
+        }
+        TestIdTraits::id_set.insert(id);
+        int rc = aba_list.add(id);
+        if (before_cap > TestIdTraits::MAX_ENTRIES && rc == EAGAIN) {
+            TestIdTraits::id_set.erase(id);
+            continue;
+        }
+        ASSERT_EQ(rc, 0) << "unexpected add() failure at id=" << id
+                         << ", before_cap=" << before_cap;
+        accepted.push_back(id);
+        if (churn && (rand_r(&seed) % 4 == 0) && !accepted.empty()) {
+            TestIdTraits::id_set.erase(accepted[rand_r(&seed) % 
accepted.size()]);
+        }
+        std::unordered_set<int> seen;
+        aba_list.apply([&](int x) { seen.insert(x); });
+        for (int a : accepted) {
+            if (TestIdTraits::id_set.count(a) > 0) {
+                ASSERT_EQ(seen.count(a), 1u) << "lost live id=" << a << " 
after add(" << id << ")";
+            }
+        }
+    }
+    const size_t init_gc_size = TestIdTraits::INIT_GC_SIZE;
+    EXPECT_GT(max_cap, init_gc_size);
+    TestIdTraits::id_set.clear();
+}
+
+TEST(AbaListTest, TestGcPreservesAddedIds) {
+    VerifyGcPreservesAddedIds(false);
+    VerifyGcPreservesAddedIds(true);
 }
 } // namespace bthread
\ No newline at end of file


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

Reply via email to