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

オープン
#2,864 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
cpp
領域
backend

調査の方向性

まず、ThreadSanitizer でビルドし、brpc に依存する、示されている 2 つのテストで警告を再現します。次に、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時間
マージ済み PR(30日)
72

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/brpc のほかの issue

apache/brpc の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。