open-telemetry / opentelemetry-cpp

The OpenTelemetry C++ Client
https://opentelemetry.io/
Apache License 2.0
850 stars 403 forks source link

TlsRandomNumberGenerator() can cause memory leaks #2660

Closed hongweipeng closed 2 months ago

hongweipeng commented 4 months ago

In my program, I found that the memory would continue to increase under multi-threading, and I suspected a memory leak.

In order to verify this problem, I clone this repository and wrote a gtest test case:

#include <vector>
#include <memory>
#include <thread>
void gen_traceid() {
  for (int i = 0; i < 10; ++i){
    Random::GenerateRandom64();
  }
}
TEST(RandomTest, MemoryMultiThreadGenerateRandom64) {
  printf("memory leak test");
  while (1)
  {
    std::vector<std::shared_ptr<std::thread>> vcths;
    for (int i=0; i< 10; ++i)
    {
      std::shared_ptr<std::thread> th(new std::thread(gen_traceid));
      vcths.push_back(th);
    }
    for (int i = 0; i < 10; ++i) {
      vcths[i]->join();
    }
    vcths.clear();
  }
}

I wrote it in sdk/test/common/random_test.cc, compiled it to generate random_test, and used ./random_test to run it. Then I used the top command to observe that its memory usage will continue to increase.

The reason for this problem is that there is

static thread_local TlsRandomNumberGenerator random_number_generator{};

in the code. When a new thread is created, a new TlsRandomNumberGenerator() instance will be created, and the constructor of this class will execute ::pthread_atfork(prepare , parent, child); Memory continues to increase due to multiple executions of pthread_atfork .

I tried to delete platform::AtFork in the TlsRandomNumberGenerator() constructor function, and the memory usage was normal and would not increase.

hongweipeng commented 4 months ago

I'm new here, I will open pull requests soon.