From 8bf93f864168eba632d41f58e844dad2ff286b3f Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 29 Sep 2025 10:07:26 +0200 Subject: fixed race condition in zen::logging::Get (#519) if two requests for the same logger came in from different threads they could end up creating the same logger twice which causes an exception on registration --- src/zencore/logging.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/zencore/logging.cpp') diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp index 685c79d82..a6697c443 100644 --- a/src/zencore/logging.cpp +++ b/src/zencore/logging.cpp @@ -346,6 +346,8 @@ SetErrorLog(std::string_view NewErrorLoggerId) } } +RwLock g_LoggerMutex; + LoggerRef Get(std::string_view Name) { @@ -355,9 +357,16 @@ Get(std::string_view Name) if (!Logger) { - Logger = Default().SpdLogger->clone(std::string(Name)); - spdlog::apply_logger_env_levels(Logger); - spdlog::register_logger(Logger); + g_LoggerMutex.WithExclusiveLock([&] { + Logger = spdlog::get(std::string(Name)); + + if (!Logger) + { + Logger = Default().SpdLogger->clone(std::string(Name)); + spdlog::apply_logger_env_levels(Logger); + spdlog::register_logger(Logger); + } + }); } return *Logger; -- cgit v1.2.3