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

未关闭
#2,864 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
cpp
领域
backend

调研方向

首先,使用依赖 brpc 并通过 ThreadSanitizer 构建的两个所示测试来复现该警告。然后检查 brpc 的 mutex.cpp,以及其中对 pthread_mutex_lock 的覆盖与 bthread 内部实现之间的交互。当复现不再产生报告的 Tsan 警告,且不破坏 pthread 和 bthread mutex 测试时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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

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

主要语言
C++
星标
17.6k
派生
4.2k
平均合并
3 天 17 小时
30 天内合并 PR
72

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/brpc 的其他 Issue

查看 apache/brpc 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。