Skip to content

TlsRandomNumberGenerator() can cause memory leaks #2660

@hongweipeng

Description

@hongweipeng

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.

Metadata

Metadata

Labels

bugSomething isn't workingtriage/acceptedIndicates an issue or PR is ready to be actively worked on.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions