From c2ce0fb46f3143589792b7511f52382f5a755823 Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Tue, 7 May 2024 15:21:22 +0800 Subject: [PATCH] Fix memory leak in TlsRandomNumberGenerator() constructor #2660 --- sdk/src/common/random.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/src/common/random.cc b/sdk/src/common/random.cc index 77b88cfa2a..a3393331bf 100644 --- a/sdk/src/common/random.cc +++ b/sdk/src/common/random.cc @@ -6,6 +6,7 @@ #include "src/common/platform/fork.h" #include +#include #include OPENTELEMETRY_BEGIN_NAMESPACE @@ -20,13 +21,14 @@ namespace common // https://github.com/opentracing-contrib/nginx-opentracing/issues/52 namespace { +static std::once_flag once; class TlsRandomNumberGenerator { public: TlsRandomNumberGenerator() noexcept { Seed(); - platform::AtFork(nullptr, nullptr, OnFork); + std::call_once(once, [](){ platform::AtFork(nullptr, nullptr, OnFork); }); } static FastRandomNumberGenerator &engine() noexcept { return engine_; }