Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TlsRandomNumberGenerator() can cause memory leaks #2660

Open
hongweipeng opened this issue May 7, 2024 · 1 comment · May be fixed by #2661
Open

TlsRandomNumberGenerator() can cause memory leaks #2660

hongweipeng opened this issue May 7, 2024 · 1 comment · May be fixed by #2661
Assignees
Labels
bug Something isn't working needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@hongweipeng
Copy link

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 hongweipeng added the bug Something isn't working label May 7, 2024
@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label May 7, 2024
@hongweipeng
Copy link
Author

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

hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 7, 2024
@marcalff marcalff linked a pull request May 7, 2024 that will close this issue
3 tasks
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 7, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 7, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 7, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 8, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 8, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 8, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 8, 2024
hongweipeng added a commit to hongweipeng/opentelemetry-cpp that referenced this issue May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants