Skip to content

Commit

Permalink
Fix memory leak in TlsRandomNumberGenerator() constructor #2660
Browse files Browse the repository at this point in the history
  • Loading branch information
hongweipeng committed May 7, 2024
1 parent da8e377 commit cdffe88
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sdk/src/common/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "src/common/random.h"
#include "src/common/platform/fork.h"

#include <atomic>
#include <cstring>
#include <random>

Expand All @@ -26,12 +27,18 @@ class TlsRandomNumberGenerator
TlsRandomNumberGenerator() noexcept
{
Seed();
platform::AtFork(nullptr, nullptr, OnFork);
if (flag)
{
platform::AtFork(nullptr, nullptr, OnFork);
flag = false;
}
}

static FastRandomNumberGenerator &engine() noexcept { return engine_; }

private:
static std::atomic<bool> flag;

static thread_local FastRandomNumberGenerator engine_;

static void OnFork() noexcept { Seed(); }
Expand All @@ -44,6 +51,7 @@ class TlsRandomNumberGenerator
}
};

std::atomic<bool> TlsRandomNumberGenerator::flag{true};
thread_local FastRandomNumberGenerator TlsRandomNumberGenerator::engine_{};
} // namespace

Expand Down

0 comments on commit cdffe88

Please sign in to comment.