From dbac3b94787c18f6a0b762525dd5864de4d7232f Mon Sep 17 00:00:00 2001 From: Sheil Kumar Date: Tue, 23 Jul 2024 07:32:56 -0700 Subject: [PATCH] Update logging to honor initialization checks --- .../core/platform/windows/logging/etw_sink.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.cc b/onnxruntime/core/platform/windows/logging/etw_sink.cc index e9cc40d88e90a..c9d908949df4c 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.cc +++ b/onnxruntime/core/platform/windows/logging/etw_sink.cc @@ -137,7 +137,14 @@ void NTAPI EtwRegistrationManager::ORT_TL_EtwEnableCallback( EtwRegistrationManager::~EtwRegistrationManager() { std::lock_guard lock(callbacks_mutex_); callbacks_.clear(); - ::TraceLoggingUnregister(etw_provider_handle); + if (initialized_ || initializing_) { + std::lock_guard init_lock(init_mutex_); + assert(!initializing_); + if (initialized_) { + ::TraceLoggingUnregister(etw_provider_handle); + initialized_ = false; + } + } } EtwRegistrationManager::EtwRegistrationManager() { @@ -166,6 +173,11 @@ void EtwRegistrationManager::LazyInitialize() try { void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled, UCHAR Level, ULONGLONG MatchAnyKeyword, ULONGLONG MatchAllKeyword, PEVENT_FILTER_DESCRIPTOR FilterData, PVOID CallbackContext) { + if (!initialized_) { + // Drop messages until manager is fully initialized. + return; + } + std::lock_guard lock(callbacks_mutex_); for (const auto& callback : callbacks_) { (*callback)(SourceId, IsEnabled, Level, MatchAnyKeyword, MatchAllKeyword, FilterData, CallbackContext);