apache / apache/brpc

依赖 brpc 作为第三方库时使用 Tsan 报错

Open
#2,864 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
17.6k
Forks
4.1k
Avg merge
2d 12h
Merged PRs (30d)
69

Description

* 项目中依赖brpc作为第三方库时出现的报错问题: brpc中的mutex.cpp覆盖了pthread_mutex_lock的实现,疑似导致tsan识别不了bthread内部实现的pthread_mutex_lock,导致出现了告警,请问有办法解决这个warning吗?一个简单的复现方式

```cpp
#include
#include
#include

namespace my::test {

class TsanCheckTest : public ::testing::Test {};

int counter;
std::mutex mutex_std;
pthread_mutex_t mu;
const int kIncrementsPerThread = 100;
void* Locker(void*) {
for (int i = 0; i < kIncrementsPerThread; ++i) {
pthread_mutex_lock(&mu);
counter++;
pthread_mutex_unlock(&mu);
}
return nullptr;
}

TEST_F(TsanCheckTest, MutexUsedInPthread) {
pthread_mutex_init(&mu, nullptr);
const int num_threads = 8;
pthread_t th[num_threads];

counter = 0;
for (int i = 0; i < num_threads; ++i) {
ASSERT_EQ(0, pthread_create(&th[i], nullptr, Locker, nullptr));
}
for (int i = 0; i < num_threads; ++i) {
ASSERT_EQ(0, pthread_join(th[i], nullptr));
}
ASSERT_EQ(counter, num_threads * kIncrementsPerThread);
}

TEST_F(TsanCheckTest, MutexUsedInBthread) {
const int num_threads = 8;
bthread_t th[num_threads];

counter = 0;
for (int i = 0; i < num_threads; ++i) {
ASSERT_EQ(0, bthread_start_urgent(&th[i], nullptr, Locker, nullptr));
}
for (int i = 0; i < num_threads; ++i) {
ASSERT_EQ(0, bthread_join(th[i], nullptr));
}
ASSERT_EQ(counter, num_threads * kIncrementsPerThread);
}

} // namespace my::test
```

![image](https://github.com/user-attachments/assets/9a7bff18-47d7-4f01-86f7-a99b521d07ca)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the warning with the two shown tests, built with ThreadSanitizer, while depending on brpc. Then inspect brpc's mutex.cpp and the interaction between its pthread_mutex_lock override and bthread's internal implementation. Done means the reproduction no longer produces the reported Tsan warning without breaking the pthread and bthread mutex tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.