From afd6858a3e11b2d230bbe09bfcda9a256f9ffaff Mon Sep 17 00:00:00 2001 From: Sheil Kumar Date: Mon, 22 Jul 2024 13:31:21 -0700 Subject: [PATCH] Add initialization check to prevent re-entry into the lock during TraceLoggingRegisterEx --- .../core/platform/windows/logging/etw_sink.cc | 13 ++++++++++--- .../core/platform/windows/logging/etw_sink.h | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.cc b/onnxruntime/core/platform/windows/logging/etw_sink.cc index 33b2d04d0b0c4..e9cc40d88e90a 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.cc +++ b/onnxruntime/core/platform/windows/logging/etw_sink.cc @@ -143,17 +143,24 @@ EtwRegistrationManager::~EtwRegistrationManager() { EtwRegistrationManager::EtwRegistrationManager() { } -void EtwRegistrationManager::LazyInitialize() { - if (!initialized_) { +void EtwRegistrationManager::LazyInitialize() try { + if (!initialized_ && !initializing_) { std::lock_guard lock(init_mutex_); - if (!initialized_) { // Double-check locking pattern + if (!initialized_ && !initializing_) { // Double-check locking pattern + initializing_ = true; etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr); if (FAILED(etw_status_)) { ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_)); } initialized_ = true; + initializing_ = false; } } +} catch (...) +{ + initialized_ = false; + initializing_ = false; + throw; } void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level, ULONGLONG MatchAnyKeyword, diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.h b/onnxruntime/core/platform/windows/logging/etw_sink.h index 3af45b813a625..19af3e565e095 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.h +++ b/onnxruntime/core/platform/windows/logging/etw_sink.h @@ -97,6 +97,7 @@ class EtwRegistrationManager { mutable OrtMutex provider_change_mutex_; OrtMutex init_mutex_; bool initialized_ = false; + bool initializing_ = false; bool is_enabled_; UCHAR level_; ULONGLONG keyword_;