diff --git a/src/aws-cpp-sdk-core/source/Aws.cpp b/src/aws-cpp-sdk-core/source/Aws.cpp index 5e7e6832af9..9cf5c0e14cd 100644 --- a/src/aws-cpp-sdk-core/source/Aws.cpp +++ b/src/aws-cpp-sdk-core/source/Aws.cpp @@ -168,13 +168,13 @@ namespace Aws Aws::Config::CleanupConfigAndCredentialsCacheManager(); - Aws::Client::CoreErrorsMapper::CleanupCoreErrorsMapper(); - Aws::CleanupCrt(); if (options.loggingOptions.logLevel != Aws::Utils::Logging::LogLevel::Off) { Aws::Utils::Logging::ShutdownCRTLogging(); Aws::Utils::Logging::ShutdownAWSLogging(); } + Aws::Client::CoreErrorsMapper::CleanupCoreErrorsMapper(); + Aws::CleanupCrt(); #ifdef USE_AWS_MEMORY_MANAGEMENT if(options.memoryManagementOptions.memoryManager) { diff --git a/src/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp b/src/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp index 1a7ce927e71..f68d3bf5df0 100644 --- a/src/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp +++ b/src/aws-cpp-sdk-core/source/utils/logging/CRTLogging.cpp @@ -6,7 +6,9 @@ #include #include #include +#include #include + #include #include #include @@ -22,6 +24,7 @@ namespace Utils namespace Logging { static std::shared_ptr CRTLogSystem(nullptr); +static Aws::Utils::Threading::ReaderWriterLock CRTLogSystemLock; static bool s_CRTLogsRedirectionIsSet(false); static aws_logger s_sdkCrtLogger; @@ -34,16 +37,15 @@ static int s_aws_logger_redirect_log( const char *format, ...) { AWS_UNREFERENCED_PARAM(logger); - // use local variable because global variable can be modified in the middle by another thread. - std::shared_ptr localCRTLogSystem = CRTLogSystem; - if (localCRTLogSystem) + Aws::Utils::Threading::ReaderLockGuard guard(CRTLogSystemLock); + if (CRTLogSystem) { assert(logger->p_impl == &s_sdkCrtLogger); Aws::Utils::Logging::LogLevel logLevel = static_cast(log_level); const char* subjectName = aws_log_subject_name(subject); va_list args; va_start(args, format); - localCRTLogSystem->Log(logLevel, subjectName, format, args); + CRTLogSystem->Log(logLevel, subjectName, format, args); va_end(args); return AWS_OP_SUCCESS; } @@ -64,11 +66,11 @@ static int s_aws_logger_redirect_log( static enum aws_log_level s_aws_logger_redirect_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) { AWS_UNREFERENCED_PARAM(logger); - std::shared_ptr localCRTLogSystem = CRTLogSystem; - if (localCRTLogSystem) + Aws::Utils::Threading::ReaderLockGuard guard(CRTLogSystemLock); + if (CRTLogSystem) { assert(logger->p_impl == &s_sdkCrtLogger); - return (aws_log_level) (localCRTLogSystem->GetLogLevel()); + return (aws_log_level) (CRTLogSystem->GetLogLevel()); } else if (s_CRTLogsRedirectionIsSet) { @@ -82,11 +84,11 @@ static enum aws_log_level s_aws_logger_redirect_get_log_level(struct aws_logger static void s_aws_logger_redirect_clean_up(struct aws_logger *logger) { AWS_UNREFERENCED_PARAM(logger); - std::shared_ptr localCRTLogSystem = CRTLogSystem; - if (localCRTLogSystem) + Aws::Utils::Threading::ReaderLockGuard guard(CRTLogSystemLock); + if (CRTLogSystem) { assert(logger->p_impl == &s_sdkCrtLogger); - return localCRTLogSystem->CleanUp(); + return CRTLogSystem->CleanUp(); } else if (s_CRTLogsRedirectionIsSet) { @@ -99,11 +101,11 @@ static void s_aws_logger_redirect_clean_up(struct aws_logger *logger) static int s_aws_logger_redirect_set_log_level(struct aws_logger *logger, enum aws_log_level log_level) { AWS_UNREFERENCED_PARAM(logger); - std::shared_ptr localCRTLogSystem = CRTLogSystem; - if (localCRTLogSystem) + Aws::Utils::Threading::ReaderLockGuard guard(CRTLogSystemLock); + if (CRTLogSystem) { assert(logger->p_impl == &s_sdkCrtLogger); - localCRTLogSystem->SetLogLevel(static_cast(log_level)); + CRTLogSystem->SetLogLevel(static_cast(log_level)); return AWS_OP_SUCCESS; } else if (s_CRTLogsRedirectionIsSet) @@ -151,16 +153,14 @@ void SetUpCrtLogsRedirection() } void InitializeCRTLogging(const std::shared_ptr& inputCrtLogSystem) { + Aws::Utils::Threading::WriterLockGuard g(CRTLogSystemLock); SetUpCrtLogsRedirection(); - // stop using CRTLogSystem to avoid race condition and lost logs in ~CRTLogSystemInterface - std::shared_ptr tmpCRTLogSystem = std::move(CRTLogSystem); CRTLogSystem = inputCrtLogSystem; } void ShutdownCRTLogging() { - // stop using CRTLogSystem to avoid race condition and lost logs in ~CRTLogSystemInterface - std::shared_ptr tmpCRTLogSystem = std::move(CRTLogSystem); - tmpCRTLogSystem.reset(); // just being explicit + Aws::Utils::Threading::WriterLockGuard g(CRTLogSystemLock); + CRTLogSystem.reset(); } } // namespace Logging